Some of you may be familiar with the game of Nim. This is a simple game for two players, played with objects separated into piles. Players take turns removing objects from piles. On each turn a player removes any positive number of items from a single pile. The object is to force the opponent into taking the last item.
For this assignment you will write a Java class that plays the game, in collaboration with the game apparatus that is provided for you. Your code will have to interact with this apparatus via a defined interface called NimPlayer.
You may work with a partner for this assignment if you adhere to the guidelines.
Specification
You should download the starter package to begin. This contains a number of files you will not need to change, including a few .class files that are already compiled from source. You may study the .java files to see how the code works. Compile everything at once using a wildcard:
javac *.java
You can then play the game against an opponent with a simple random strategy.
java HumanVsRandom 1 2 3 4 5
The numbers at the end of the line give the initial pile setup, so you can play different variants of the game. (If you are feeling up for a challenge, you can also play HumanVsGenius. Can you beat the genius? It is possible, though not easy!)
The game state is represented as an array of integers, where each integer in the array is one pile. Once a pile has been reduced to zero elements, it is no longer possible to remove anything from it. Your class, when it is asked to make a move, should take the state provided and return a new state with items removed from one of the piles. For example, if the input state was [0, 2, 0, 4, 3] then one possible valid move would be [0, 2, 0, 2, 3] representing the removal of two items from the fourth pile. The state [0, 1, 0, 3, 3] would not be a valid move because you would need to remove items from two different piles to get there from the original state, and that takes at least two moves.
Take a look at NimPlayer.java and HumanPlayer.java. Your assignment is to create a new class that will play the game automatically, without human input. It will need to implement the two required functions in the NimPlayer interface. The minimum passing criterion is to write a class that successfully implements the interface and compiles cleanly. The next step is to ensure that it always makes a legal move. For a bonus to your grade, it should also be able to beat the random player more than 50% of the time on average.
To Submit
- MyPlayer.java containing your game player class
- HumanVsMyPlayer.java which sets up a game with your player
- MyPlayerVsRandom.java which sets up a game between your player and the random player
- readme.txt containing your self-reflection on this assignment