Nim Game C++ Programming Question

Posted on 2010-01-04
Medium Priority
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

Author Comment

ID: 26173539
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.



Accepted Solution

pchela733 earned 750 total points
ID: 26174275
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;

Assisted Solution

magicdlf earned 750 total points
ID: 26178103
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 53

Expert Comment

ID: 26178546
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

ID: 26178549
>> edvice

should have been 'advice' obviously heh.

Author Closing Comment

ID: 31672497
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

807 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