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.

What you need to do

Testing your program

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 for this assignment (upload in a separate file called nim_challenge.cpp; also turn in your regular nim.cpp file without the challenges):