骰子游戏,要求我们在每次掷骰子前对回合进行编号并加*
Dice game that requires us to number our turns along with adding * before every dice roll
访问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;
// ^
相关文章:
- 如何在C++中高效地构造随机骰子
- C++:为什么我的掷骰子函数在掷骰子数量时只返回偶数整数?
- 掷2个骰子1000次
- C++ 简单的掷骰子 - 如何返回多个不同的随机数
- 如何创建一个在C++中生成 3 个掷骰子的函数
- DiceSum - 如果骰子总和不是某个数字,如何扩展 if/if-else 语句以重新掷骰子
- 使用C++模拟单掷骰子
- 数组的掷骰子
- C++掷多个骰子
- C ++骰子游戏模拟掷两个骰子
- 掷骰子(基于文本的RPG)C++不需要的数字和字母序列输出
- 骰子游戏,要求我们在每次掷骰子前对回合进行编号并加*
- C++ 小直(如 yahtzee 或扑克).5 掷骰子 (1234)/(2345)/(3456)
- C++我的数组没有保存我的掷骰子
- C ++掷骰子分析仪(2D阵列)
- 掷骰子模拟器C++
- 如何从掷骰子中找到最小值和最大值
- 掷骰子 - 随机数未正确生成C++
- c++对一个数字进行除法处理,每次掷骰子最多显示60个字符
- 为什么掷骰子的结果如此均匀