SHARE
TWEET

Untitled

a guest Feb 27th, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dicegames;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CrapsPlay {
  6.     private Die die1, die2;
  7.     private Scanner scan;
  8.     private boolean won;
  9.     private boolean finished = false;
  10.     private int rolls;
  11.     private int firstRoll;
  12.     private int sumOfRoll;
  13.     private int wonGames;
  14.     private int gamesPlayed;
  15.  
  16.     public CrapsPlay() {
  17.         die1 = new Die();
  18.         die2 = new Die();
  19.         scan = new Scanner(System.in);
  20.     }
  21.  
  22.     /**
  23.      * Print out a neat welcome message to the player.
  24.      */
  25.     private void welcomeToGame() {
  26.         System.out.println("Velkommen til spillet Craps.");
  27.         System.out.println("Spillets gang");
  28.         System.out.println("Spillet består af en række kast med to terninger. " + "Spilleren kaster to terninger. "
  29.                 + "Det første kast kaldes ‘come out roll’. "
  30.                 + "Spilleren vinder med det samme, hvis det første kast er 7 eller 11 og "
  31.                 + "taber med det samme, hvis han opnår 2, 3 eller 12. "
  32.                 + "Hvis spillerens første kast er 4, 5, 6, 8, 9 eller 10, " + "etableres dette tal som hans ‘point’. "
  33.                 + "Spilleren bliver derefter ved med at kaste, indtil han opnår sit ‘point’ igen. "
  34.                 + "Opnår han 7 før han opnår sit ‘point’, har han tabt.");
  35.  
  36.     }
  37.  
  38.     /**
  39.      * End the current game and print out whether it was a win or a loss.
  40.      */
  41.     public void gameOver() {
  42.         if (won) {
  43.             System.out.println("Tillykke du har vundet.");
  44.             wonGames++;
  45.         } else
  46.             System.out.println("Du har tabt.");
  47.         gamesPlayed++;
  48. //      scan.close();
  49.     }
  50. /**
  51.  * End the game and close the scanner.
  52.  * Print number of won games as well as number of lost games.
  53.  */
  54.     public void endGame() {
  55.         System.out.println("Tak for denne gang.");
  56.         System.out.println("Du vandt " + wonGames + " spil.");
  57.         System.out.println("Du tabte " + (gamesPlayed - wonGames) + " spil.");
  58.         scan.close();
  59.     }
  60.    
  61. /**
  62.  * Roll the dice and check if the roll results in a win or a loss.
  63.  */
  64.     public void takeTurn() {
  65.         die1.roll();
  66.         die2.roll();
  67.         sumOfRoll = die1.getFaceValue() + die2.getFaceValue();
  68.         checkWin();
  69.         checkLoss();
  70.         if (rolls == 0)
  71.             firstRoll = sumOfRoll;
  72.         rolls++;
  73.         System.out.println("Du har kastet: " + sumOfRoll);
  74.     }
  75.  
  76.     /**
  77.      * Check if the current roll results in a win, and end the current game if it did.
  78.      */
  79.     public void checkWin() {
  80.         if (rolls == 0 && sumOfRoll == 7 || rolls == 0 && sumOfRoll == 11) {
  81.             won = true;
  82.             finished = true;
  83.         } else if (sumOfRoll == firstRoll) {
  84.             won = true;
  85.             finished = true;
  86.         }
  87.  
  88.     }
  89.  
  90.     /**
  91.      * Check if the current roll results in a loss, and end the current game if it did.
  92.      */
  93.     public void checkLoss() {
  94.         if (rolls == 0 && sumOfRoll == 2 || rolls == 0 && sumOfRoll == 3 || rolls == 0 && sumOfRoll == 12) {
  95.             won = false;
  96.             finished = true;
  97.         } else if (sumOfRoll == 7 && rolls > 0) {
  98.             won = false;
  99.             finished = true;
  100.         }
  101.     }
  102.     /**
  103.      * Launch Craps and print a welcome message before the first game.
  104.      * Reset relevant variables in between games.
  105.      */
  106.     public void startGame() {
  107.  
  108.         if (gamesPlayed == 0) {
  109.             welcomeToGame();
  110.         }
  111.  
  112.         finished = false;
  113.         firstRoll = 0;
  114.         rolls = 0;
  115.  
  116.         while (!finished) {
  117.             System.out.println("Vil du kaste en terning? Angiv Ja eller Nej: ");
  118.             String goOn = scan.nextLine();
  119.             if (goOn.equalsIgnoreCase("Nej")) {
  120.                 finished = true;
  121.             } else {
  122.                 takeTurn();
  123.             }
  124.         }
  125.         gameOver();
  126.     }
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top