I'm writing a program for the game of "Thirteen stones", one of the rules of the game is if there is 3 or less stones left and it is the computer's turn the computer will take the rest and win the game. The program is not doing that. It causes an infinite loop when all the stones are gone, unless the random number happens to be the right amount of stones left. Thanks in advance.
Here is my code:
Here is my code:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;
void WelcomeScreen();
void TotalStones(int&, int&, int&);
void UserStones(int&, int&);
void Credits();
int main()
{
int stonesToTake; // How many stones the user will take each time.
int userStones;
int compStones;
int stonesLeft;
char playAgain = 'Y';
do
{
stonesToTake = 0;
userStones = 0;
compStones = 0;
stonesLeft = 13;
srand(time(0));
int num = (rand() % 3) + 1;
WelcomeScreen();
system ("CLS");
while (stonesLeft != 0)
{
if (stonesLeft == 1 || num == 1)
compStones ++;
else if (stonesLeft == 2 || num == 2)
compStones += 2;
else
compStones += 3;
cout << "The computer took " << num << " stones this turn!" << endl;
TotalStones(userStones, compStones, stonesLeft);
if (stonesLeft == 0)
{
cout << "The computer took the remaining stones!\nYou lose!" << endl;
_getch();
break;
}
UserStones(stonesToTake, userStones);
if (stonesLeft == 0)
{
cout << "Congratulations! You won the game!" << endl;
_getch();
break;
}
system ("CLS");
}
system ("CLS");
cout << "Would you like to play again? [Y or N]: ";
cin >> playAgain;
system("CLS");
while (playAgain != 'y' && playAgain != 'Y' && playAgain != 'N' && playAgain != 'n')
{
cout << "Invalid choice! \n Would you like to play again? [Y or N]: ";
cin >> playAgain;
}
}
while (playAgain == 'Y' || playAgain == 'y');
return 0;
}
void WelcomeScreen()
{
cout << endl << endl;
cout << "\t\tWelcome to the game of Thirteen Stones!" << endl << endl;
cout << "In this game you will be against the computer. First, the computer will" << endl;
cout << "draw 1 to 3 stones from a pile of 13 stones. Then you will choose how many" << endl;
cout << "stones you want to pick up. The player who picks up the last stone wins!" << endl << endl;
cout << "Press any key to continue. . .";
_getch();
}
void TotalStones(int &userStones, int &compStones, int &stonesLeft)
{
int countStones = 0;
char stones = 2; // Visual for stones
int maxStones = 13;
cout << "Your stones: ";
while (userStones != countStones)
{
cout << stones << ' ';
countStones ++;
}
countStones = 0;
cout << endl << "Computer's stones: ";
while (compStones != countStones)
{
cout << stones << ' ';
countStones ++;
}
stonesLeft = maxStones - compStones - userStones;
countStones = 0;
cout << endl << "Stones left: ";
while (stonesLeft != countStones)
{
cout << stones << ' ';
countStones ++;
}
cout << endl;
}
void UserStones(int &stonesToTake, int &userStones)
{
cout << "How many stones do you want to take from the pile[1-3]?" << endl;
cin >> stonesToTake;
while (stonesToTake < 1 || stonesToTake > 3)
{
system ("CLS");
cout << "Invalid choice!\n";
cout << "How many stones do you want to take from the pile[1-3]?" << endl;
cin >> stonesToTake;
}
userStones += stonesToTake;
}
Comment