Problem written by Danielle Kain and Chris Piech.
Worked Examples:
E=MC2,
Fibbonacci
Day2: Nimm.java
Nimm is an ancient game of strategy that is named after the old German word for "take." It is also called Tiouk Tiouk in West Africa and Tsynshidzi in China. Players alternate taking stones until there are zero left. The game of Nimm goes as follows:
Write a program to play Nimm. To make your life easier we have broken the problem down into smaller milestones. You have a lot of time for this program. Take it slowly, piece by piece.
Start with 20 stones. Repeat the process of removing stones and printing out how many stones are left until there are zero. Don't worry about whose turn it is. Don't worry about making sure only one or two stones are removed. Use the method readInt(msg)
which prints msg and waits for the user to enter a number.
Create a variable of type int
to keep track of whose turn it is (remember there are two players). Tell the user whose turn it is. Each time someone picks up stones, change the player number.
Make sure that each turn only one or two stones are removed. After you read a number of stones to remove from a user (their input), you can use the following pattern to check if it was valid and keep asking until it is valid.
while(input is invalid) {
input = readInt("Please enter 1 or 2: ");
}
Announce the winner.
Can you write an AI opponent? You can start with a dummy AI that always plays a random number. Then try to make one that plays intelligently...
Some other extension ideas: