CS 110: Introduction
to Computing with Java
Lab 5
Do the following tasks
before coming to lab. You will not be able to work on the lab if you have
not already completed these tasks.
Read the description of the
lab. Notice that you will be writing a class with one method called bestMove.
Write (in English or pseudo code) a description of the algorithm you will
implement in the lab. This is an important exercise. The better you
understand what you are doing, the quicker you will get through the lab.
We continue working on the
NIM Game introduced in Lab 3. Our goal here is to write a class that
controls how a computer player chooses to make moves. Essentially, we are
writing the “brains” of the computer player.
This lab gives you some
experience writing a small class. You will also learn how to use the API
documentation from other classes to work out how to use their services.
We have provided for you
most of what you need to build a graphical application to play the NIM game Lab5.zip. As always, download and unzip the files on
your removable media and create/save a Dr Java Project file in your
directory. The documentation for all the
project classes can be found in the Javadoc directory. Here is a summary of the names of the source
files with an explanation of their purpose.
The details are in the doc files.
SimpleNIMGUI,java – the user
interface to the game
SimpleNIM.java– the game
itself
Player.java– common code for
player objects (human or computer)
HumanPlayer.java– represents
the human player
MachinePlayer.java–
represents the computer player
NIMStrategy.java- you need to write this class.
This class needs only one method:
·
public
int bestMove(Player player, SimpleNIM game) – returns the number of
sticks the computer player takes given the current state of the game.
The parameters mean:
· player – the computer player
· game – the current game.
Try different strategies for
the computer player:
· Start by having the computer player take one stick no matter what the configuration of the game. This will allow you to play and see that the game works.
· Have the computer play the best strategy where, if possible, take the number of sticks such that after the move, the number of remaining sticks is one more than a multiple of 4. If it is not possible to leave one more than a multiple of 4 sticks, then take one stick.
For example, if there are 18 sticks remaining, the computer should take one stick.
You will need to use at
least one method of the SimpleNIM class in order to write your bestMove
method.
There is no sample output
for this assignment.
Before you leave,
have your TA check off that you completed the lab. Make sure each person
saves a copy of your work. You may complete the code on your own, but the TA
must certify that most of your work was done in the lab.
Write a document describing
your experiences. Your lab must be printed (not handwritten).
Answer the following
questions related to what you did in this week’s lab.
What is wrong with the strategy of having the
computer player always take two sticks with every move? Explain.
What method(s) of the SimpleNIM class did you
need to use in writing your method?
Comment on the usefulness of the Javadco
documentation for learning how to use a method of the SimpleNim class.
Describe what you learned doing this lab.
Explain what was difficult and what was easy.
Attach a listing of your completed NIMStrategy class. You only need to turn in your final implementation for the
best strategy.
Note: You should work
alone on the lab report.
Note: The
assignment is due at the BEGINNING of your next lab. No late assignments
will be accepted. Emailed assignments will not be accepted. If you
are not going to be in lab on the due date, you can turn the assignment ahead
of time to the CS110 TA box in the CS department office.