我目前正在做一个骰子游戏。用户首先掷一对骰子,假设他掷骰子 1 = 2 和骰子 2 = 3。所以现在总数是 5。现在,他需要再次获得 5(总数)才能获胜,如果他在下一步中没有获得 5,那么他会再次滚动并继续比赛。但是,如果在任何时候,他一共掷了两个,他就输了。
所以,请告诉我如何存储第一轮的价值并将其与下一步行动进行比较。我尝试了一些东西,但它似乎不起作用。
#include<iostream>
#include<ctime> // for the time() function
#include<cstdlib> // for the srand() and rand() functions
using namespace std;
// Declare variables
//int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int dieTotal = 0;
int Dice ()
{
// roll the first die
die1 = (rand() % 6 ) + 1;
// roll the second die
die2 = (rand() % 6 ) + 1;
}
// iniating a second two pair dice function.
int compDice()
{
Dice();
dieTotal = die1 + die2;
return (dieTotal);
}
// User Rolling the dice and calucalting the total here
int userGame()
{
cout << "\nUser turn --- Press 2 to roll" << endl;
cin >> userInput;
if ( userInput == 2 )
{
Dice ();
cout << "\nThe user rolled Dice 1 = " << die1 << " and Dice 2 = " << die2 << endl;
cout << "Total = " << die1 + die2 << endl;
}
else {
cout << "Wrong input.";
//userGame();
}
return (die1 + die2 );
}
int checkForWin ()
{
while (true)
{
int result1 = compDice();
int result = userGame();
// int finalResult = dieTotal;
if (result == result1 )
{
cout << "\nUser won. Computer looses....m " << endl;
break;
}
else if (result == 2)
{
cout << "\nUser looses. Computer won." <<endl;
break;
}
else
{
}
}
}
// Calling for the checkForWin() function in main and the srand.
int main ()
{
cout << "This is the Dice game. " << endl;
// set the seed
srand(time(0));
checkForWin(); // Initiating the game.
return 0;
}