骰子游戏,要求我们在每次掷骰子前对回合进行编号并加*

Dice game that requires us to number our turns along with adding * before every dice roll

本文关键字:编号 掷骰子 游戏 我们      更新时间:2023-10-16

访问http://voyager.deanza.edu/~bentley/ass5.html

我的目标是尝试匹配链接上的示例输出。我似乎唯一无法克服的障碍是如何在每一次掷骰子之前添加你的回合以及"*"。

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

int roll();
int turn();

int main ()
{
srand(time(0));
int gameTotal = 0;
while (gameTotal < 100)
{
    gameTotal += turn();
    cout << "Your total is now " << gameTotal << endl << endl;;
 }

 }
int turn()
{
int turnTotal = 0;
int temp;

for (int i = 0; i < 3; i++)
{
    temp = roll();
    if (temp == 7) break;
    turnTotal += temp;
}
cout << "You scored " << turnTotal << " points for this turn" << endl;
return turnTotal;
}
int roll()
{
int die1 = rand() % 6 + 1;
int die2 = rand() % 6 + 1;
int sum = die1 + die2;

 cout << "You rolled a " << die1 << " and " << die2 << ".  " << "That's " << sum << endl;
return sum;
}

只需添加一个变量来存储播放的回合数,并在每个回合更新它。添加*甚至更简单:

int main ()
{
    srand(time(0));
    int gameTotal = 0;
    int turns = 0;
    while (gameTotal < 100)
    {
        // update number of turns and output it
        ++turns;
        cout << "This is your turn #" << turns << endl;
        gameTotal += turn();
        cout << "*** Your total is now " << gameTotal << endl << endl;
        //       ^^^ easy
    }
}

其他功能的类似变化:

cout << "** You scored " << turnTotal << " points for this turn" << endl;
//       ^^

cout << "* You rolled a " << die1 << " and " << die2 << ".  " << "That's " << sum << endl;
//       ^