Nim Game C++ Programming Question

Posted on 2010-01-04
Last Modified: 2013-12-14
I am not a C++ expert, however I was asked to provide a solution to what I believe is rudimentary problem.  Your help in solving this issue would be much appreciated.

Problem Statement:

I needd to create a two-player version of the game of Nim. In the game, players take turns removing from 1 to 4 sticks from a pile of 13. The player who picks up the last stick wins the game. Your application should validate the input from the players. This means that the application should continue to ask a player for the number of sticks the player wishes to take as long as any of the following are true:
           "      The number of sticks the player asks to take is greater than the number of sticks left.
           "      The number of sticks the player asks to take is not greater than 4 (the maximum number that he or she is allowed to take).
            "      The number of sticks the players asks to take is not less than 1 (the minimum number that he or she is allowed to take)
An attempt to create the applicationis is below, however it works for one player only and not two.  

Given the short fuse and inconvience caused, the award for a solution is 500 well deserved points and my sincere thanks.

//Nim -- two-player game

#include <cstdlib>

#include <iostream>

#include <string>

using namespace std;

int main()


    //total sticks

    const int MAX_STICKS = 13;


    //declare variables

    string player1, player2;

    int sticks;


    cout << "\tWelcome to Nim!" << endl << endl;


    cout << "Players take turns picking up from 1 to 4 sticks from a pile of 13." << 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;




         if ( sticks < 13 )

         { cout << "There are " << (MAX_STICKS - sticks) << " stick<s> left." << endl;

           cout << "Player 2 - how many will you take? ";

           cin >> sticks;

           cout << endl; }


         if ( sticks < 13 )

         { 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);




    return EXIT_SUCCESS;


Open in new window

Question by:dcfreyer
    LVL 1

    Author Comment

    The question is an assignment for a close friend's daughter, and if I have over-stepped the submission rules please withdraw the question immediately.  The professionalism and integrity of Expert's Exchange is much more important than a rudimentary programming question.

    Thank you sir for your professionalism.


    LVL 3

    Accepted Solution

    1) You need a varibla which indicate how many sticks left after each move (int STICKS, for example).
    2) Decrement STICKS = STICKS - sticks; each time when player take a sticks.
    3) Add condition when you must exit application:
    if( STICKS == 0 )
      cout << "The winner is player number:" << WinnerPlayer << endl;
    LVL 9

    Assisted Solution

    Each time you take a stick, use this:
    int sticksTaken;
    This will prevent the cin refresh the data in sticks and keep the game moving forward. Hope this is clear for you.

    P.S. thanks evilrix for your reminder
    LVL 53

    Expert Comment

    With the edvice given by the others, you can fix the current code (it doesn't exactly work correctly for one player).

    >>  it works for one player only and not two.  

    Once it is working fine for one player, it's easy to add a second player. Make sure that you have a variable that indicates the current player, and for each iteration of the loop, you switch players (ie. you modify that variable so it refers to the other player).

    You can then add a message to indicate whose turn it is, ask how many sticks the player would like to take, check the value, subtract that amount from the current amount of sticks left, check if there's a winner, and continue with the next iteration of the loop.
    LVL 53

    Expert Comment

    >> edvice

    should have been 'advice' obviously heh.
    LVL 1

    Author Closing Comment

    Both solutions when taken together served as the complete fix.  Please accept my sincere thanks for your efforts, and hope 250 pts each is sufficient for your efforts.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now