骰子游戏,要求我们在每次骰子滚动前加上*的数字

时间:2016-05-10 09:13:42

标签: c++ function dice

访问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;
}

1 个答案:

答案 0 :(得分:0)

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

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;
//       ^