CS
Department, Fall 1997, Java Programming
Battleship: stage 2
The next stage is to add user capability for:
-
Allowing the user to place the five ships anywhere on his or her grid.
-
Starting and stopping the game
-
Playing a new game
-
Making a guess
-
An indicator showing whose turn it is
You should choose a method for 1. that you think will be easy to use. Techniques
you might consider are:
-
clicking on the two end squares for the ship - you will have to catch squares
that are too far apart too close to do this
-
entering the start and end squares as grid references into a dialog - again
there is error checking to do
-
you might use a start square and an orientation (horizontal or vertical)
- at least this way the only checking to do is whether the choice puts
the ship either off the board, or overlapping another ship.
-
some other way that gets round all the problems - creativity needed!
2 and 3 are best done with either buttons or menus. We will cover both
in class.
4 is obvious. When it is your turn to make a guess, a click in the square
will convey the position of the guess. Processing this click is called
hit-testing. If your board is a grid of canvases, then you will
need to identify the source of the mouse click in an event handler for
the frame that holds them. If you board is one canvas, then you will have
to figure out which square has been clicked on from the mouse coordinates
at the time of the click.
5 could be done with a Label that changes from "your turn" to "computer's
turn" although there are many ways to implement this idea. You can use
the methods hide and show for components to make the items appear and disappear.
The Labels could change color (implement this in the paint method for the
container that hodls them).
Behind the scenes (not part of the user interface) you will also need
(not now, but soon) :
-
A way for the computer to place its ships using a random number generator
-
A way to record the computer's responses on the scoring grid. i.e. whether
your gueses were reported as hit or miss by the computer.