CS Department, Fall 1997, Java Programming

Battleship: stage 2

The next stage is to add user capability for:

  1. Allowing the user to place the five ships anywhere on his or her grid.
  2. Starting and stopping the game
  3. Playing a new game
  4. Making a guess
  5. 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: 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) :

  1. A way for the computer to place its ships using a random number generator
  2. 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.