Thread: Making Nim Game - Need some help and ideas.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Making Nim Game - Need some help and ideas.

    Hello Everyone. I was given problem.. Write a program play the following game: There are 21sticks. Two players take turns picking the sticks. Each player should take atleast 1 sticks and a maximum of 4 sticks at each turn. The player who picks thelast sticks is the winner. But encountered different problem, one of this is that when the Player 1 gives a value of 4, it's working fine... 21-4 will get 17. But when the next player gives a value of 3, it will add up to 17 which makes the MAX_STICKS to 18 that it should be 14. So far I have this code.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        const int MAX_STICKS = 21;
        
        
        string player1, player2;
        int winnerplayer;
        int sticks;
        
        cout << "\tWelcome to Nim!" << endl << endl;
        
        cout << "Players take turns picking up from 1 to 4 sticks from a pile of 21." << endl;
        cout << "Whoever picks up the last stick wins." << endl << endl;
        
        cout << "There are " << MAX_STICKS << " stick<s> left." << endl;
        cout << "Player 1 - how many will you take? ";
        cin >> sticks;
        
        do
        {
             if ( sticks < 21 )
             { cout << "There are " << (MAX_STICKS - sticks) << " stick<s> left." << endl;
               cout << "Player 2 - how many will you take? ";
               cin >> sticks;
               cout << endl; }
               
             if ( sticks < 21 )
             { cout << "There are " << (MAX_STICKS - sticks) << " stick<s> left." << endl;
               cout << "Player 1 - how many will you take? ";
               cin >> sticks;
               cout << endl; }
               
               }while (sticks > 0);
               
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Any suggestions? I am totally a newbie in dev c, help would be highly be appreciated. Thanks in advance.
    Last edited by Salem; 03-15-2011 at 07:37 AM. Reason: Added [code][/code] tags

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    nim

    first Use the code tags! (choose go advanced for an easy way to find them)

    Code:
    if ( sticks < 21 )
    replace the number 21 with MAX_STICKS, you have already defined the constant

    some tips:

    Also you should be stopping the user from selecting more than 4 sticks, so as it stands, 'sticks' will always be less than 21....

    Use another variable like 'sticks_left' to keep total of how many have been removed so far, each time a player takes a turn, remove the value picked up from the sticks_left variable
    Code:
    sticks_left = MAX_STICKS;
    
    //start loop
    //user plays
    
    sticks_left -= sticks;
    you
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Hello, thanks a lot for the idea, I got it. Can I asked one last thing?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        const int MAX_STICKS = 21;
        
        
        string player1, player2;
        int winnerplayer;
        int sticks;
        int sticksLeft;
        sticksLeft = MAX_STICKS;
        
        cout << "\tWelcome to Nim!" << endl << endl;
        
        cout << "Players take turns picking up from 1 to 4 sticks from a pile of 21." << endl;
        cout << "Whoever picks up the last stick wins." << endl << endl;
        
        cout << "There are " << MAX_STICKS << " stick<s> left." << endl;
        cout << "Player 1 - how many will you take? ";
        cin >> sticks;
        sticksLeft -= sticks;
        
        do 
        {
            
             
             if ( sticks < MAX_STICKS )
              {cout << "There are " << sticksLeft << " stick<s> left." << endl;
               cout << "Player 2 - how many will you take? ";
               cin >> sticks;
               sticksLeft -= sticks;
               cout << endl; }
               
                
               if ( sticks < MAX_STICKS )
              {cout << "There are " << sticksLeft << " stick<s> left." << endl;
               cout << "Player 1 - how many will you take? ";
               cin >> sticks;
               sticksLeft -= sticks;
               cout << endl; }
         } while ( sticks == 0 );
         
        
         
          
           
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    I can only have 3 inputs, let's say, player1 gives a value of 4, player gives a value of 3, and goes back to player one which will give a value of 3.. then the loops stops... I think is shouldn't cause sticks value is not 0...

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    .

    It fails because the while loop never starts:

    Code:
    while ( sticks == 0 );
    sticks is never equal to 0, so because it is a do..while loop, the if statements inside are evaluted once...and then the program flow just exits the loop, because sticks != 0

    so your three inputs that you see at the moment are (1) before the loop starts, (2) first IF statement in loop, (3) 2nd IF statement in loop. y nada mas!

    Code:
     if ( sticks < MAX_STICKS )
    This still makese no sense, why are you asking this here? sticks is ALWAYS less than MAX_STICKS! by definition

    You should instead be putting in some kind of checks to see that the user (A) cannot pick up more than 4 sticks, and (B) That the user cannot pick up a quantity of sticks that mean there are none remaining (if that is the rule?) EG, there are four sticks left so user cannot pick up four as that would leave 0...but i do not know the rules properly so forgive me if that is not needed.

    Indeed with those rules then where only four sticks are left then it is a guaranteed win for the player who's turn it is anyway, so he may as well be allowed to pick all four up!.
    Last edited by rogster001; 03-15-2011 at 10:08 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed