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

用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日

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错