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: -
#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;
-
}
-
I compiled and ran your code using a debugger and almost right away I got maxStones == 13, compstones == 9, userStones == 6 so that line code calculates -2 stonesLeft. Then you drop into a loop to display the -2 stonesLeft and off you go.
So there is an error in the calulation of either or both of userStones and compStones since compStones and userStones add up to 15 stones when only 13 are available.
4 1740
Without serious debugging, are you sure this code: - stonesLeft = maxStones - compStones - userStones;
-
does not result in a negative value?
I suggest an assert right after this calculation. Let me know what happened.
I added the assert after it, the only time it doesn't fail (not turn negative) is when the computer randomly takes the right amount of stones left.
So I'm very confident now that the problem is with the if statements for the computer taking the remaining stones. I'm not exactly sure what's wrong with them though.
I compiled and ran your code using a debugger and almost right away I got maxStones == 13, compstones == 9, userStones == 6 so that line code calculates -2 stonesLeft. Then you drop into a loop to display the -2 stonesLeft and off you go.
So there is an error in the calulation of either or both of userStones and compStones since compStones and userStones add up to 15 stones when only 13 are available.
I got it working, had to add two variables to hold the real totals for user and computer. I had some logic issues, but after going over it I fixed it. Thanks for taking the time and helping!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gremlin |
last post by:
If you are not familiar with the halting problem, I will not go into
it in detail but it states that it is impossible to write a program
that can tell if a loop is infinite or not. This is a...
|
by: mailpitches |
last post by:
Hello,
Is there any way to kill a Javascript infinite loop in Safari without
force-quitting the browser?
MP
|
by: LOPEZ GARCIA DE LOMANA, ADRIAN |
last post by:
Hi all,
I have a question with some code I'm writting:
def main():
if option == 1:
function_a()
|
by: na1paj |
last post by:
here's a simple linked list program. the DeleteNode function is
producing an infinit loop i think, but i can't figure out where..
#include <stdio.h>
typedef struct
{
char *str; //str is a...
|
by: alxasa |
last post by:
Hi, I have a setInterval which executes its command every 10 seconds in
a infinite loop.
I've got something real basic like:
var processes=0;
function startme(){
if(stopthisloop>1)
|
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= |
last post by:
www.authoring.stylesheets]
Dear fellow CSS colleagues and web authors in alt.html discussion forum,
I would like to ask you to help me confirm that there is a serious bug
in IE 7 final release...
|
by: Sunbags |
last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
|
by: James Watt |
last post by:
can anyone tell me how to do an infinite loop in C/C++, please ?
this is not a homework question .
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |