473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with infinite loop

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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. void WelcomeScreen();
  9. void TotalStones(int&, int&, int&);
  10. void UserStones(int&, int&);
  11. void Credits();
  12.  
  13. int main()
  14. {
  15.     int stonesToTake; // How many stones the user will take each time.
  16.     int userStones;
  17.     int compStones;
  18.     int stonesLeft;
  19.     char playAgain = 'Y';
  20.  
  21.  
  22.     do
  23.     {
  24.         stonesToTake = 0;
  25.         userStones = 0;
  26.         compStones = 0;
  27.         stonesLeft = 13;
  28.         srand(time(0));
  29.         int num = (rand() % 3) + 1;
  30.  
  31.         WelcomeScreen();
  32.         system ("CLS");
  33.         while (stonesLeft != 0)
  34.         {
  35.             if (stonesLeft == 1 || num == 1)
  36.                 compStones ++;
  37.             else if (stonesLeft == 2 || num == 2)
  38.                 compStones += 2;
  39.             else
  40.                 compStones += 3;
  41.  
  42.             cout << "The computer took " << num << " stones this turn!" << endl;
  43.             TotalStones(userStones, compStones, stonesLeft); 
  44.             if (stonesLeft == 0)
  45.             {
  46.                 cout << "The computer took the remaining stones!\nYou lose!" << endl;
  47.                 _getch();
  48.                 break;
  49.             }
  50.             UserStones(stonesToTake, userStones);
  51.             if (stonesLeft == 0)
  52.             {
  53.                 cout << "Congratulations! You won the game!" << endl;
  54.                 _getch();
  55.                 break;
  56.             }
  57.             system ("CLS");
  58.         }
  59.         system ("CLS");
  60.         cout << "Would you like to play again? [Y or N]: ";
  61.         cin >> playAgain;
  62.         system("CLS");
  63.         while (playAgain != 'y' && playAgain != 'Y' && playAgain != 'N' && playAgain != 'n')
  64.         {
  65.             cout << "Invalid choice! \n Would you like to play again? [Y or N]: ";
  66.             cin >> playAgain;
  67.         }
  68.     }
  69.     while (playAgain == 'Y' || playAgain == 'y');
  70.  
  71.     return 0;
  72. }
  73.  
  74. void WelcomeScreen()
  75. {
  76.     cout << endl << endl;
  77.     cout << "\t\tWelcome to the game of Thirteen Stones!" << endl << endl;
  78.     cout << "In this game you will be against the computer. First, the computer will" << endl;
  79.     cout << "draw 1 to 3 stones from a pile of 13 stones. Then you will choose how many" << endl;
  80.     cout << "stones you want to pick up. The player who picks up the last stone wins!" << endl << endl;
  81.     cout << "Press any key to continue. . .";
  82.     _getch();
  83. }
  84.  
  85. void TotalStones(int &userStones, int &compStones, int &stonesLeft)
  86. {
  87.     int countStones = 0;
  88.     char stones = 2; // Visual for stones
  89.     int maxStones = 13;
  90.  
  91.     cout << "Your stones: ";
  92.     while (userStones != countStones)
  93.     {
  94.         cout << stones << ' ';
  95.         countStones ++;
  96.     }
  97.  
  98.     countStones = 0;
  99.     cout << endl << "Computer's stones: ";
  100.     while (compStones != countStones)
  101.     {
  102.         cout << stones << ' ';
  103.         countStones ++;
  104.     }
  105.     stonesLeft = maxStones - compStones - userStones;
  106.     countStones = 0;
  107.     cout << endl << "Stones left: ";
  108.  
  109.     while (stonesLeft != countStones)
  110.     {
  111.         cout << stones << ' ';
  112.         countStones ++;
  113.     }
  114.     cout << endl;
  115. }
  116.  
  117. void UserStones(int &stonesToTake, int &userStones)
  118. {
  119.  
  120.     cout << "How many stones do you want to take from the pile[1-3]?" << endl;
  121.     cin >> stonesToTake;
  122.  
  123.     while (stonesToTake < 1 || stonesToTake > 3)
  124.     {
  125.         system ("CLS");
  126.         cout << "Invalid choice!\n";
  127.         cout << "How many stones do you want to take from the pile[1-3]?" << endl;
  128.         cin >> stonesToTake;
  129.     }
  130.     userStones += stonesToTake;
  131. }
  132.  
Nov 10 '11 #1

✓ answered by weaknessforcats

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
weaknessforcats
9,208 Expert Mod 8TB
Without serious debugging, are you sure this code:

Expand|Select|Wrap|Line Numbers
  1. stonesLeft = maxStones - compStones - userStones;
  2.  
does not result in a negative value?

I suggest an assert right after this calculation. Let me know what happened.
Nov 10 '11 #2
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.
Nov 10 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 11 '11 #4
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!
Nov 13 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

43
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...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
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()
13
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...
2
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)
10
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...
13
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...
44
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 .
0
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,...
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.