如何使计算机每次在猪游戏中进行随机变量
How to make computer take random variable each time in pig game
所以这是我的代码,我的问题是,无论何时滚动"骰子",它都会持续滚动,直到达到限制,我设置了它成为。
对于人类用户而言,情况并非如此,而是用于计算机使用的情况。我该如何解决此问题,以便计算机每次在1-6之间进行随机变量。
int main ()
{
int humanTurn=0;
int humanTotalScore=0;
int computerTotalScore=0;
int computerTurn=3;
float diceRoll = 0;
int score = 0;
int computerScore = 0;
int answer = 0;
int humanScore = 0;
int pause = 0;
float computerDiceRoll = 0;
srand(time(0));
cout << "Welcome to the game of pig, the first player to reach to 100 wins."<< endl;
cout << "if you roll a one no score will be recorded and your turn will go the computer"<< endl;
cout << " If you choose to hold, the total score accumilated will be recorded" << endl;
while ((humanTotalScore < 100) && (computerTotalScore < 100))
{
cout << endl;
cout << endl;
cout << "press 1 to roll or 2 to save your score: ";
cin >> answer;
for (int i=0; i<1; i++)
{
diceRoll = rand() % 6 + 1;
}
if ((diceRoll > 1) && ( answer == 1))
{
cout << endl;
cout << endl;
cout << "your current roll is: " << diceRoll << endl;
cout << endl;
cout << endl;
score += diceRoll;
cout << "Score for this turn is: " << score << endl;
cout << "your total score is: " << humanTotalScore << endl;
cout << endl;
cout << endl;
}
else if (answer == 2)
{
cout << "your score of: " << score << " will be saved" << endl;
humanTotalScore += score;
score = 0;
}
else
{
cout << endl;
cout << endl;
cout << "you rolled a ONE. You lose your turn and any points that were with it" <<endl;
cout << endl;
cout << endl;
cout << "enter any key to continue." << endl; // necessary to let user know that there point were discontinued
cin >> pause;
score = 0;
computerTurn = score;
}
if ((computerTurn == 0) || (answer ==2))
{
cout << "it is now the Computers turn" << endl;
for (int i=0; i<1; i++)
{
srand(time(0));
computerDiceRoll = rand() % 6 + 1;
}
while ((computerDiceRoll > 1)&& (computerScore <= 20))
{
cout << endl;
cout << endl;
cout << "computers roll is : " << diceRoll << endl;
cout << endl;
cout << endl;
computerScore += diceRoll;
computerTotalScore += computerScore;
cout << "computer current score is : " << computerScore << endl;
cout << "computer total score is: " << computerTotalScore << endl;
cout << endl;
cout << endl;
if (diceRoll == 1)
{
computerScore = 0;
}
}
}
}
return 0;
}
'for (int i=0; i<1; i++)'
那条线没有用。这条线没有道理:
'while ((computerDiceRoll > 1)&& (computerScore <= 20)){'
在此行:
cout << "computers roll is : " << diceRoll << endl;
您打印了最后的"人类"卷。您的计算机滚动位于" diceroll"中的" ComputerDiceroll"
相关文章:
- 将正态随机变量与任意 RHO(corrcoef) 相关联
- 为什么变量的打印地址在每次执行时都会打印随机值,即使它是 C 中的逻辑地址?
- 如何让变量随机输出四个单词之一
- 如何生成随机变量或类名并在以后使用?
- 尝试生成随机变量时获得"-nan(ind)"
- 为什么在 sync_with_stdio(false) 之前使用 cin 为下一个 i/p 变量提供随机值
- 如何使计算机每次在猪游戏中进行随机变量
- 如何通过Mersenne Twister生成不重复的随机变量
- C 中的Bernoulli随机变量
- 快速在C 中添加随机变量
- 为什么这个随机变量没有均匀分布?
- 具有QuantLib的泊松随机变量
- 生成n个随机变量,并用c++将它们相乘
- C++中的Marsaglia正态随机变量
- 随机变量总是生成5
- 随机变量名称,可能吗?
- 用相同的公式生成0和1之间的两个随机变量
- 如何最有效地防止正态分布的随机变量为零
- 如何在没有for循环的情况下请求boost::normaldistribution生成一个大的随机变量向量
- 有没有办法在不计算 CDF 的情况下从非标准分布生成随机变量