第六章第三十题(游戏:双骰子赌博)(Game: craps)

第六章第三十题(游戏:双骰子赌博)(Game: craps)

  • **6.30(游戏:双骰子赌博)执双骰子游戏是赌场中非常流行的骰子游戏。编写程序,玩这个游戏的一个变种,如下所描述: 执两个骰子。每个骰子有六个面,分别表示值1,2,…,6。检查这两个骰子的和。如果和为2、3或12(称为掷骰子(crap)),你就输了;如果和是7或者11(称作自然(natural)),你就赢了;但如果和是其他数字(例如:4、5、6、8、9或者10),就确定了一个点。继续掷骰子,直到掷出一个7或者掷出和刚才相同的点数。如果掷出的是7,你就输了。如果掷出的点数和你前一次掷出的点数相同,你就赢了。程序扮演一个独立的玩家。
    下面是一些运行示例:
    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
  • 参考代码:
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);
    }
}

  • 结果显示:
You rolled 4 + 5 = 9
point is 9
You rolled 1 + 6 = 7
You lose

Process finished with exit code 0

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Craps是一种骰子游戏,玩家在游戏中下注并掷骰子Craps的胜率取决于玩家下注的类型和掷骰子的结果。以下是一些常见的下注类型和它们的胜率: 1. Pass Line Bet(赢线赌注):这是最常见的下注类型,玩家在第一次掷骰子时下注。如果掷出7或11,则玩家获胜;如果掷出2、3或12,则玩家输掉赌注;如果掷出4、5、6、8、9或10,则这个数字成为“点数”,玩家需要在后续的掷骰子中再次掷出这个点数才能获胜。胜率约为49.29%。 2. Don't Pass Bet(输线赌注):这是与Pass Line Bet相反的下注类型,如果第一次掷骰子时掷出2或3,则玩家获胜;如果掷出7或11,则玩家输掉赌注;如果掷出12,则平局;如果掷出4、5、6、8、9或10,则这个数字成为“点数”,玩家需要在后续的掷骰子中掷出7才能获胜。胜率约为47.96%。 3. Come Bet(进场赌注):这是类似于Pass Line Bet的下注类型,但是可以在任何时候下注。如果掷出7或11,则玩家获胜;如果掷出2、3或12,则玩家输掉赌注;如果掷出4、5、6、8、9或10,则这个数字成为“点数”,玩家需要在后续的掷骰子中再次掷出这个点数才能获胜。胜率约为49.29%。 4. Don't Come Bet(反进场赌注):这是类似于Don't Pass Bet的下注类型,但是可以在任何时候下注。如果掷出2或3,则玩家获胜;如果掷出7或11,则玩家输掉赌注;如果掷出12,则平局;如果掷出4、5、6、8、9或10,则这个数字成为“点数”,玩家需要在后续的掷骰子中掷出7才能获胜。胜率约为47.96%。 需要注意的是,Craps是一种纯粹的运气游戏,没有任何技巧可以提高玩家的胜率。以上胜率仅供参考,实际胜率可能会因为不同的规则和下注类型而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值