Nim is a strategy game between two players.
Write a program that allows two human players to play Nim against each other. The program should detect when the last counter has been taken and declare a winner.
At first, don't worry about detecting cheating. That is one of the bonus options.
Here is an example game, with starting piles of 3, 4, and 5 counters.
Player 1, enter your name: Alice Player 2, enter your name: Bob A: 3 B: 4 C: 5 Alice, choose a pile: A How many to remove from pile A: 2 A: 1 B: 4 C: 5 Bob, choose a pile: C How many to remove from pile C: 3 A: 1 B: 4 C: 2 Alice, choose a pile: B How many to remove from pile B: 1 A: 1 B: 3 C: 2 Bob, choose a pile: B How many to remove from pile B: 1 A: 1 B: 2 C: 2 Alice, choose a pile: A How many to remove from pile A: 1 A: 0 B: 2 C: 2 Bob, choose a pile: B How many to remove from pile B: 1 A: 0 B: 1 C: 2 Alice, choose a pile: C How many to remove from pile C: 2 A: 0 B: 1 C: 0 Bob, choose a pile: B How many to remove from pile B: 1 A: 0 B: 0 C: 0 Alice, there are no counters left, so you WIN!
For +30 bonus points, prevent the users from doing anything bad:
...a game already in progress. A: 0 B: 1 C: 0 Bob, choose a pile: A Nice try, Bob. That pile is empty. Choose again: B How many to remove from pile B: 0 You must choose at least 1. How many? 1 A: 0 B: 0 C: 0
And what about this?
A: 1 B: 4 C: 5 Bob, choose a pile: C How many to remove from pile C: 8 Pile C doesn't have that many. Try again: 3 A: 1 B: 4 C: 2
And don't forget this:
A: 1 B: 4 C: 5 Bob, choose a pile: C How many to remove from pile C: -2 You must choose at least 1. How many? 3 A: 1 B: 4 C: 2
For +10 bonus points, make your program detect when there is only one counter left and declare the winner one turn earlier.
...a game already in progress. A: 0 B: 2 C: 2 Bob, choose a pile: B How many to remove from pile B: 1 A: 0 B: 1 C: 2 Alice, choose a pile: C How many to remove from pile C: 2 A: 0 B: 1 C: 0 Bob, you must take the last remaining counter, so you lose. Alice wins!
For +15 bonus points, visually display the counters in rows instead of just showing a number. You must use loops for this.
A: *** B: **** C: ***** Alice, choose a pile: A How many to remove from pile A: 2 A: * B: **** C: ***** Bob, choose a pile: C How many to remove from pile C: 3 A: * B: **** C: **
For +25 bonus points, visually display the counters in columns. You must use a loop for this.
This is quite difficult.
* * * * * * * * * * * * A B C Alice, choose a pile: A How many to remove from pile A: 2 * * * * * * * * * * A B C Bob, choose a pile: C How many to remove from pile C: 3 * * * * * * * A B C
For +50 bonus points, allow one human player to play against a computer opponent. The computer must attempt to win and not break any rules.
It is possible to make a computer player that ALWAYS wins if it goes first. The Wikipedia article for Nim explains the theory.
However, your program does not need to use a winning strategy to earn bonus points: it merely must make only legal moves.
©2013–2015 Graham Mitchell
This assignment is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.