- Filename: Nim.java

Nim is a strategy game between two players.

- Start by placing counters (coins or toothpicks or something) into 3 piles.
- Player #1 picks a pile, then removes one or more counters from that pile. (It's okay to take the whole pile.)
- Player #2 picks a pile, then removes one or more counters from that pile.
- Player #1 plays again. (It's okay to choose a different pile this time.)
- Whichever player is forced to take the last counter is the
**LOSER**.

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:AlicePlayer 2, enter your name:BobA: 3 B: 4 C: 5Alice, choose a pile:AHow many to remove from pileA:2A: 1 B: 4 C: 5Bob, choose a pile:CHow many to remove from pileC:3A: 1 B: 4 C: 2Alice, choose a pile:BHow many to remove from pileB:1A: 1 B: 3 C: 2Bob, choose a pile:BHow many to remove from pileB:1A: 1 B: 2 C: 2Alice, choose a pile:AHow many to remove from pileA:1A: 0 B: 2 C: 2Bob, choose a pile:BHow many to remove from pileB:1A: 0 B: 1 C: 2Alice, choose a pile:CHow many to remove from pileC:2A: 0 B: 1 C: 0Bob, choose a pile:BHow many to remove from pileB:1A: 0 B: 0 C: 0Alice, 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: 0Bob, choose a pile:ANice try,Bob. That pile is empty. Choose again:BHow many to remove from pileB:0You must choose at least 1. How many?1A: 0 B: 0 C: 0

And what about this?

A: 1 B: 4 C: 5Bob, choose a pile:CHow many to remove from pileC:8PileCdoesn't have that many. Try again:3A: 1 B: 4 C: 2

And don't forget this:

A: 1 B: 4 C: 5Bob, choose a pile:CHow many to remove from pileC:-2You must choose at least 1. How many?3A: 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: 2Bob, choose a pile:BHow many to remove from pileB:1A: 0 B: 1 C: 2Alice, choose a pile:CHow many to remove from pileC:2A: 0 B: 1 C: 0Bob, you must take the last remaining counter, so you lose.Alicewins!

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:AHow many to remove from pileA:2A: * B: **** C: *****Bob, choose a pile:CHow many to remove from pileC:3A: * 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 CAlice, choose a pile:AHow many to remove from pileA:2* * * * * * * * * * A B CBob, choose a pile:CHow many to remove from pileC: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.