游戏:双骰子

双骰子是一项非常流行于赌场的骰子游戏。编写程序实现这个游戏的变种,如下所示:

投掷两枚骰子。每个骰子有6个面,分别为1,2,3,4,5,6.检视一下两枚骰子的数量和。如果和是2,3或者12(叫做双骰),你输。如果和为7或者11(叫做自然),你赢;如果和是其他值,那么称这个和为点数。继续投掷两枚骰子,直到你掷出7(你输)或者掷出刚才的点数(你赢)。

下面是一些运行样例:

样例1:

You rolled 5 + 6 = 11

You win

样例2:

You rolled 1 + 2 = 3

You lose

样例3:

You rolled 4 + 4 = 8

point is 8

You rolled 6 + 2 = 8

You win

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int touZi()
{
    srand(time(0));
   int  num1 = rand()%6+1;
   int  num2 = rand()%6+1;
    cout<<"You rolled "<<"num1 "<<"+"<<"num2 = "<<num1+num2<<endl;
    return num1+num2;

}
int main()
{
    int m = touZi();
    if(m==7||m==11)
       {
           cout<<"You win";
           return 0;
       }
    else if(m==2||m==3||m==12)
        {
            cout<<"You lose";
            return 0;
        }
   int point = m;
    cout << "point is " << point << endl;
    do
    {
        m = touZi();
    }
    while (m != 7 && m != point);

    if (m == 7)
        cout << "You lose" << endl;
    else
        cout << "You win" << endl;

    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的骰子游戏的 Java 代码示例: ```java import java.util.Random; public class DoubleDiceGame { public static void main(String[] args) { Random random = new Random(); int dice1 = 0, dice2 = 0, total = 0; int point = 0; boolean isWin = false; // 一直循环直到玩家赢或输 while (!isWin) { dice1 = random.nextInt(6) + 1; dice2 = random.nextInt(6) + 1; total = dice1 + dice2; System.out.println("你出了 " + dice1 + " 和 " + dice2 + ",总点数为 " + total); if (point == 0) { // 第一次骰子 if (total == 7 || total == 11) { isWin = true; System.out.println("你赢了!"); } else if (total == 2 || total == 3 || total == 12) { isWin = false; System.out.println("你输了!"); } else { point = total; System.out.println("你的点数是 " + point + ",再来一次骰子!"); } } else { // 不是第一次骰子 if (total == point) { isWin = true; System.out.println("你赢了!"); } else if (total == 7) { isWin = false; System.out.println("你输了!"); } else { System.out.println("你的点数是 " + point + ",再来一次骰子!"); } } } } } ``` 在这个程序中,我们使用了一个 `Random` 对象来生成骰子点数,然后根据点数判断玩家是赢还是输。在第一次骰子时,如果点数为 7 或 11 则玩家赢,如果点数为 2、3 或 12 则玩家输,否则玩家会得到一个点数并继续骰子。在后续的骰子中,如果点数等于第一次出的点数则玩家赢,如果点数为 7 则玩家输,否则继续骰子直到赢或输。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值