Hello,
Please take a look at the below code -
#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 ()
{
// set the seed
srand(time(0));
// roll the first die
die1 = (rand() % 6 ) + 1;
// roll the second die
die2 = (rand() % 6 ) + 1;
}
int compGame()
{
Dice ();
cout << "The computer rolled Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
cout << "Total = " << die1 + die2 << endl;
}
int userGame()
{
cout << " User turn --- Press 2 to roll";
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();
}
}
int main ()
{
compGame();
userGame();
if (compGame() == 7 || compGame() == 11)
{
cout <<"Computer won";
}
else if (compGame() == 2)
{
cout << "Computer lost";
}
else if (userGame() == 7 || userGame() == 11)
{
cout <<"User won";
}
else if (userGame() == 2)
{
cout << "User lost";
}
else
{
main();
}
return 0;
}
I know the main() is not right there. What I need to have there is, if computer rolled 7 or 11 (die1 + die 2) then computer wins and the same for the user. And if either of them rolls 2 (die1 + die 2), the respective player rules.
So what kind of loop should go in main() to make the program right?