我有一个骰子游戏
int userGame()
{
cout << " User turn --- Press 2 to roll" << endl;
cin >> userInput;
if ( userInput == 2 )
{
Dice ();
cout << "The user rolled Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
cout << "Total = " << die1 + die2 << endl;
}
else {
cout << "Wrong input. Try again";
//userGame();
}
return (die1 + die2);
}
现在在 int main 中,我有 -
int main ()
{
// set the seed
srand(time(0));
userGame();
while (true)
{
if (userGame() == 7 || userGame() == 11)
{
cout << "You won" << endl;
break;
}
else if (userGame() == 2)
{
cout << "You loose" <<endl;
break;
}
else
{
break;
}
}
return 0;
骰子 ();
#include<iostream>
#include<ctime> // for the time() function
#include<cstdlib> // for the srand() and rand() functions
using namespace std;
int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int Dice ()
{
// roll the first die
die1 = (rand() % 6 ) + 1;
// roll the second die
die2 = (rand() % 6 ) + 1;
}
但是由于某种原因,输出显示不正确。一旦在输出为 7 时显示用户获胜,其他时间则继续游戏。
我在用 main() 中的循环做什么?
谢谢