Help with infinite loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • algorithm92
    New Member
    • Nov 2011
    • 4

    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:
    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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Without serious debugging, are you sure this code:

    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.

    Comment

    • algorithm92
      New Member
      • Nov 2011
      • 4

      #3
      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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.
        Last edited by weaknessforcats; Nov 11 '11, 04:21 PM. Reason: added more info

        Comment

        • algorithm92
          New Member
          • Nov 2011
          • 4

          #5
          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!

          Comment

          Working...