Chapter 6 Question 30 (Game: craps) (Game: craps)

Chapter 6 Question 30 (Game: craps) (Game: craps)

  • **6.30 (Game: Craps Gambling) Craps game is a very popular dice game in casinos. Write a program to play a variant of this game, as described below: Hold two dice. Each dice has six sides, representing values ​​1, 2,..., 6. Check the sum of these two dice. If the sum is 2, 3, or 12 (called crap), you lose; if the sum is 7 or 11 (called natural), you win; but if the sum is another number ( For example: 4, 5, 6, 8, 9 or 10), a point is determined. Continue to roll the dice until you roll a 7 or roll the same number as before. If you roll a 7, you lose. If the number of points rolled is the same as the number of points you rolled last time, you win. The program plays an independent player.
    Here are some running examples:
    You rolled 5 + 6 = 11
    You win
    You rolled 1 + 2 = 3
    You lose
    You rolled 4 + 4 = 8
    point is 8
    You rolled 6 + 2 = 8
    You win
    You rolled 3 + 2 = 5
    point is 5
    You rolled 2 + 5 = 7
    You lose
    **6.30(Game: craps)Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows:Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player.
    Here are some sample runs.
    You rolled 5 + 6 = 11
    You win
    You rolled 1 + 2 = 3
    You lose
    You rolled 4 + 4 = 8
    point is 8
    You rolled 6 + 2 = 8
    You win
    You rolled 3 + 2 = 5
    point is 5
    You rolled 2 + 5 = 7
    You lose
  • Reference Code:
package chapter06;

public class Code_30 {
    
    
    public static void main(String[] args) {
    
    
        int point;
        int firstDie = rollDie();
        int secondDie = rollDie();
        int sumOfTwoDice = firstDie + secondDie;

        if(sumOfTwoDice == 2 || sumOfTwoDice == 3 || sumOfTwoDice == 12)
        {
    
    
            System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice);
            System.out.println("You lose");
        }
        else if(sumOfTwoDice == 7 || sumOfTwoDice == 11)
        {
    
    
            System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice);
            System.out.println("You win");
        }
        else
        {
    
    
            point = sumOfTwoDice;
            System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice);
            System.out.printf("point is %d\n", point);
            do {
    
    
                firstDie = rollDie();
                secondDie = rollDie();
                sumOfTwoDice = firstDie + secondDie;
            }while(sumOfTwoDice !=7 && sumOfTwoDice != point);

            System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice);
            if(sumOfTwoDice == point)
                System.out.println("You win");
            else if(sumOfTwoDice == 7)
                System.out.println("You lose");
        }
    }
    public static int rollDie() {
    
    
        return (int)(Math.random() * 6 + 1);
    }
}

  • The results show that:
You rolled 4 + 5 = 9
point is 9
You rolled 1 + 6 = 7
You lose

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109211310