Hi.

Let me start off by saying I've done no programming in my life aside from the 2 weeks of Intro to C++ class I've had so far, so I've got little idea of what I'm doing. Our assignment is to write a program for the 'game of NIM', essentially. The traditional game of NIM contains rows, but our program only has to have one row. We are allowed to assume that the user takes their turn first everytime the program is run. The user is allowed to input any number 9 or larger to indicate the number of sticks that will be in the pile. Then, the user is asked to enter a number 1-4 to indicate the number of sticks being removed from the pile. The computer then has to "take its turn" and generate a random number 1-4 to remove that number of sticks from the pile. This process is repeated until the last stick is 'picked up'. The person(or computer) to get the last stick is declared the winner. I've got the majority of the coding done, it only needs a little fine tuning. Anyone with any suggestions/notices any errors...any input is really helpful seeing as I'm basically crawling around in the dark. What I am having problems with specifically is in the comments at the end of the code.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main ( ){
    int num, sticks, comp_choice;
    
//This statement says that the winner is false until declared later in the program. (When there are no sticks left)
bool winner = false;



	    cout << "Welcome to the game of NIM! Please enter an integer 9 or greater\n"
             << "to indicate the number of sticks used in this game." << endl;
        cin  >> sticks;

//If the user enters a number less than 9, they will continually be prompted to enter a number
//9 or greater until they do so.
   while(sticks < 9){
        cout << "This is not an acceptable value.\n"
             << "Please enter a number 9 or greater." << endl;
        cin  >> sticks;
                    }
      //While there is no winner, the body of the loop is run.              
      while (!winner){

            //If  the number of sticks is 9 or greater, the user will be prompted to choose the amount to remove between
            //1 and 4.
        
            if(sticks >= 9){
               cout << "There are " << sticks << " sticks in the pile.\n"
                    << "Please enter the amount(between 1 and 4) that you would like to remove from the pile." << endl;
               cin  >> num;
                           }
            //If the number entered is a number 1 through 4, the user will be told the amount of sticks they removed
            //followed by the amount of sticks left in the pile. 
            if((num >= 1) && (num <= 4)){
               cout << "You have removed " << num << " sticks from the pile." << endl;
            //This subtracts the number removed from the total number of sticks in the pile.
               sticks -= num;
               cout << "There are " << sticks << " left in the pile." << endl;
                              }
               
            //If the number entered is less than 1 or greater than 4, the user will continually be prompted
            //to enter a number from 1 to 4 until they do so.                  
              if((num < 1) || (num > 4)){
	             cout << "This is not an acceptable value.\n"
                      << "Please enter an amount between 1 and 4." << endl;
                  cin >> num;
         //If the amount of sticks reaches 0 during the user's turn, the user will be declared winner.
         if(sticks == 0){
                  winner = true;
                  cout << "Yay!!! You won the game of NIM! Congratulations!" << endl;
                       }
                                  }
             //If the user tries to enter a removable number of sticks that is larger than the number
             //left in the pile, then they are prompted to enter a more acceptable amount.
             else if (sticks - num < 0){
                cout << "Oops! There aren't enough sticks left to take this many from the pile.\n"
                     << "Please enter an acceptable amount." << endl;
                 cin >> num;
                                    }
        //This part of code is the computer's turn. The computer is allowed to remove a random number of sticks
        //1 through 4.  The amount taken away by the computer is then shown to the user, followed by the amount
        //of sticks total left in the pile.
        else{
          srand((unsigned)time(NULL)); 
          comp_choice = rand() % 4; 
        //This subtracts the computer's choice of removal sticks from the total sticks in the pile.
        sticks -= comp_choice;
                 cout << "The computer has chosen to take away " << sticks << " sticks from the pile.\n"
                      << "There are " << sticks << " sticks left in the pile." << endl;
        //If the total number of sticks in the pile reaches 0 during the computer's turn, the computer is declared winner.         
        if(sticks == 0){
                  winner = true;
                  cout << "Awwww darn.  The computer won. Better luck next time!" << endl;
                  }
             }
        }
      
        
       

        
   
system ("PAUSE");
return 0;
 }  


//The problems I'm having now are that the user is only allowed to input a number of sticks to remove once,
//when they should be prompted after every computer turn. The program is just continually subtracting the number
//first entered by the user.  Also, the computer is not generating a number 1 through 4 to remove like it should be.

Recommended Answers

All 6 Replies

The problems I'm having now are that the user is only allowed to input a number of sticks to remove once

I believe this problem may be due to an incorrect (and easily fixable) test condition in line #32.

Also, the computer is not generating a number 1 through 4 to remove like it should be.

Line #70 is currently generating a pseudo-random number between 0 and 3. Try this:

comp_choice = (rand()%4) + 1;

I believe this problem may be due to an incorrect (and easily fixable) test condition in line #32.

Line #70 is currently generating a pseudo-random number between 0 and 3. Try this:

comp_choice = (rand()%4) + 1;

Your help is greatly appreciated!!!
I fixed line 32! Which has helped with the 'taking turns' issue. But, the random code is still generating a number greater than 4.

Edit: Also just noticed that when the computer 'takes a turn', the number chosen is not being subtracted from the total pile? I thought that was what my

sticks -= comp_choice

was doing...do you have any suggestions for this?

In line #8 you have a bunch of uninitialized variables which may be causing unpredictable behavior. Try initializing these variables to correct game starting values.

In line #8 you have a bunch of uninitialized variables which may be causing unpredictable behavior. Try initializing these variables to correct game starting values.

I went back and intialized all the variables to 0, ran the program a few times with different inputs...still the same problem. I've tried changing around the equation for the comp_choice...I'm not sure where the problem is. When I run the program, after the user gives the input for the 1-4 value and the computer takes a turn, not only is the value not subtracted off the total sticks(during computer turn), but a random line is showing up from line 43. I'm trying to look around there and see if I notice anything strange, let me know if you see anything. Thank you again so much for your help. I know it can be frustrating to help a newb. XD

I phrased my previous recommendation carefully by stating, "...initialize all variables to appropropriate game start values" which may or may not be zero. The var I am speaking of specifically is int sticks.

It would also be nice if you'd look at the code posted on the forum and reformat it for your next post so we can follow what is happening. Pretty code is much easier to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.