2201_75507744 2023-01-29 12:19 采纳率: 100%
浏览 72
已结题

用c++一个投骰子的游戏

两点玩家掷骰子。 骰子决定了他们得到多少分。 第一个获得 100 点的玩家获胜。 规则如下:
每一轮,主动玩家面临一个决定(投掷或持有):
掷骰子。
Roll: 如果是点数1:你失去了你的回合,没有回合总分加到你的累计。换另一个玩家掷骰子。
如果点数是2-6:您掷出的数字将添加到您的回合总数中。
Hold:您的回合总数将添加到您的总计中。 现在是
轮到下一个玩家。
Quit: 退出游戏。

  • 写回答

3条回答 默认 最新

  • 社区专家-Monster-XH 2023-01-29 12:32
    关注

    回答不易,请采纳一下,讨个赏:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int rollDice() {
        return rand() % 6 + 1;
    }
    
    int main() {
        srand(time(0));
    
        int player1Score = 0, player2Score = 0;
        int currentPlayer = 1;
    
        while (true) {
            cout << "Player " << currentPlayer << ": " << endl;
            cout << "1. Roll" << endl;
            cout << "2. Hold" << endl;
            cout << "3. Quit" &#8203;`oaicite:{"index":0,"invalid_reason":"Malformed citation << endl;\n\n        int choice;\n        cin >>"}`&#8203; choice;
    
            if (choice == 1) {
                int roll = rollDice();
                if (roll == 1) {
                    cout << "You rolled a 1! Your turn is over and you don't earn any points." << endl;
                    currentPlayer = (currentPlayer == 1) ? 2 : 1;
                } else {
                    cout << "You rolled a " << roll << "!" << endl;
                    if (currentPlayer == 1) {
                        player1Score += roll;
                    } else {
                        player2Score += roll;
                    }
                }
            } else if (choice == 2) {
                if (currentPlayer == 1) {
                    player1Score += player1Score;
                    currentPlayer = 2;
                } else {
                    player2Score += player2Score;
                    currentPlayer = 1;
                }
            } else if (choice == 3) {
                break;
            }
    
            if (player1Score >= 100) {
                cout << "Player 1 wins!" << endl;
                break;
            } else if (player2Score >= 100) {
                cout << "Player 2 wins!" << endl;
                break;
            }
        }
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月6日
  • 已采纳回答 1月29日
  • 创建了问题 1月29日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”