Trying the Game of NIM.

I have an assignment for my Intro to C++ class that I am having trouble with. I've got the majority of the code done, it just needs a little fine tuning. The assignment is to create the 'Game of NIM' (we are not using multiple 'rows'. There is only one row in our game) where the user inputs a value larger than 9 to indicate the number of sticks in the game, and then is allowed to choose between 1 and 4 sticks to remove from the pile. The computer and user take turns removing sticks from the pile until the last one is taken, and the person to take the last stick is the winner.

In my code, the computer is supposed to randomly generate a number between 1 and 4(the amount of sticks the computer will choose to remove). However, everytime my program gets to the computer's turn, it just..quits. I can't figure out why, so if someone could point out where my error is and give some helpful hints, that would be great. Also, another part of the assignment is that the computer must try to leave the user with a number of sticks in the pile that is a multiple of 5(to ensure that the computer will win). I have not yet figured out a way to implement this. Any other random errors you find in the code would be greatly appreciated if pointed out. I have done NO programming outside of this class(and we have only had this class for like 2 weeks so far, with only like 2 assignments), so this is all completely new to me. Any help is greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

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

using namespace std;

int main ( ){
    int user_turn, num, sticks, comp_choice;
    
    
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;

   while(sticks < 9){
        cout << "This is not an acceptable value.\n"
             << "Please enter a number 9 or greater." << endl;
        cin  >> sticks;
                    }
                    
   while (!winner){


        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((num >= 1) && (num <= 4)){
             cout << "You have removed " << num << " sticks from the pile." << endl;
             sticks -= num;
             cout << "There are " << sticks << " left in the pile." << endl;
                                    }
        if((num < 1) || (num > 4)){
	     cout << "This is not an acceptable value.\n"
              << "Please enter an amount between 1 and 4." << endl;
         cin >> num;
                                  }
        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;
                                    }
        if(sticks == 1){
                  winner = true;
                  cout << "Yay!!! You won the game of NIM! Congratulations!" << endl;
                       }
        else{
             {
    srand((unsigned)time(NULL)); 
    comp_choice = rand() % 4; 
    return comp_choice;
    }
        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(sticks == 1){
                  winner = true;
                  cout << "Awwww darn.  The computer won. Better luck next time!" << endl;
                  }
             }
        }
        
        
       

        
   
system ("PAUSE");
return 0;
 }  

Your issue is line 57: return comp_choice;

This will return from main(), ending your program...I'm not even sure why that's there, TBH.

Also, your indentation is very hard to read, I think you might have some other logical errors in there somewhere but I can't tell.
You have:

1
2
3
4
5
6
if((num >= 1) && (num <= 4))
sticks -= num;

//later....

 else if (sticks - num <= 0)


the last check you have here should come before you
sticks -= num;

Also by your explanation of the game the check should be

if( sticks - num < 0)

This is because if the sticks = zeros them someone has just won rather than taken too many sticks...


so the condition for victory should be
if(sticks == 1)



I think you should start with that. See if that fixes any problems.



Edit: I have summarized some logic errors, but firedraco seems to be right about what is causing your program to just exit. In the case of main, return basically means return control to the operating system.
Last edited on
Thanks for that guys. I've removed the return function from line 57, which fixed the exiting issue. Also changed around the checks you mentioned, bgreeson. A new problem has arised. When the program is run, the user can only input once how many sticks they would like to remove, when they should be prompted after every computer turn. Any suggestions on how to fix this?

Edit: Also noticed that when the computer chooses, the number isn't being subtracted from the total. D:
Last edited on
Post the updated code, and I'll check it out when I get a chance.
Alright, thank you so much!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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. 
Topic archived. No new replies allowed.