COMP 142: Project 1
You will write a program that simulates the two-player game
Nim. Nim is a simple game that where two players alternate removing objects
from piles according to certain rules. The goal of the game is sometimes to remove
the last object, or avoid removing the last object.
Here are the rules for our version of the game.
- The game begins with three piles, labeled A, B, and C. Pile A starts with 5 objects,
B with 4, and C with 3.
- Player 1 moves first, and is allowed to remove any number of objects (> 0) from a single pile.
- Player 2 then moves, and is allowed to make a similar choice: they choose a pile, and remove
any positive number of objects from that pile.
- The two players alternate turns until one player is forced to take the last object. That player loses the game,
and the other player wins.
What you need to do
- Create a C++ program that allows two players to play a game of Nim, starting from the
5/4/3 situation described above.
- While your program is running, it should clearly show who's turn it is, the state
of the piles of objects, and prompt each player for a pile (a character) and a number of objects
to remove from that pile (an integer).
- Your program should not allow a user to enter an improper pile character (only A, B, and C should
be allowed), nor an invalid number of objects to remove from that pile (i.e., negative numbers should
not be allowed, nor a number of objects greater than the number left in that pile).
- At the end of the game (whenever the last object is removed), announce the winner.
- You should use good programming style when writing your program, except that you don't need to use
functions in C++ because we haven't learned them yet. You may read ahead and use them if you so desire.
All other style guidelines, including proper indentation and comments, should be followed.
Testing your program
- You should test your program thoroughly to make sure it works.
- Your program should
gracefully handle situations where either the pile character is mistyped (ask for it again),
or the number of objects to removed is wrong (ask for it again).
- You may assume the players will never enter a non-number for the number of objects
to remove.
Sample interactions
Run 1:
It's player 1's turn.
Here is the board:
A B C
5 4 3
Enter the pile: A
How many sticks would you like to remove from pile A? 3
It's player 2's turn.
Here is the board:
A B C
2 4 3
Enter the pile: B
How many sticks would you like to remove from pile B? 3
It's player 1's turn.
Here is the board:
A B C
2 1 3
Enter the pile: C
How many sticks would you like to remove from pile C? 2
It's player 2's turn.
Here is the board:
A B C
2 1 1
Enter the pile: C
How many sticks would you like to remove from pile C? 1
It's player 1's turn.
Here is the board:
A B C
2 1 0
Enter the pile: A
How many sticks would you like to remove from pile A? 2
It's player 2's turn.
Here is the board:
A B C
0 1 0
Enter the pile: B
How many sticks would you like to remove from pile B? 1
The winner is player 1!
Run 2:
It's player 1's turn.
Here is the board:
A B C
5 4 3
Enter the pile: A
How many sticks would you like to remove from pile A? 5
It's player 2's turn.
Here is the board:
A B C
0 4 3
Enter the pile: C
How many sticks would you like to remove from pile C? 6
Invalid number of sticks; please re-enter number: -2
Invalid number of sticks; please re-enter number: 0
Invalid number of sticks; please re-enter number: 2
It's player 1's turn.
Here is the board:
A B C
0 4 1
Enter the pile: D
Invalid pile; please re-enter the pile: Z
Invalid pile; please re-enter the pile: B
How many sticks would you like to remove from pile B? 4
It's player 2's turn.
Here is the board:
A B C
0 0 1
Enter the pile: C
How many sticks would you like to remove from pile C? 1
The winner is player 1!
What to turn in
Through Moodle, turn in your code as a file called nim.cpp.
Challenge Problems
From time to time, I will offer "challenge problems" on assignments. These problems
are designed to have little (but some) impact on your grade whether you do them or not.
You should think of these problems as opportunities to work on something interesting and optional,
rather than a way to raise your grade through "extra credit."
Policy on challenge problems:
-
Challenge problems will typically allow you to get 2-5% of additional credit on an assignment, yet they
will typically be much more difficult than this credit amount suggests.
-
You should not attempt a challenge problem until you have finished the rest of an assignment.
- The tutors will not provide help on challenge problems. The instructor
will provide minimal assistance only, as these problems are optional and are designed
to encourage independent thought.
- Challenge problems may be less carefully specified and less
carefully calibrated for how difficult or time-consuming they are.
- If you solve a challenge problem, include a comment at the top of your program detailing what you did.
Challenge problems for this assignment (upload in a separate file called nim_challenge.cpp; also turn in your
regular nim.cpp file without the challenges):
- Add a computer player that plays randomly and let the user choose whether or not they want to play the
computer or another player.
- Make the computer player use some sort of intelligent strategy (but not cheat).
- Let the two players repeatedly play against each other. Report some statistics about the series
of games at the end (how many wins for each player, most popular opening moves, etc).