CS Department, Fall 1997, Java Programming
Battleship: stage 3
Your next stage is to play an actual game against the computer. This will involve completing work on the interface, if necessary, and implementing a strategy that the computer can use in making guesses. Here are some issues you may want to think about:
At the end of this stage your program should:
Looking ahead to stage 4
You may want to look ahead when you write the code for stage 3 to the two-player version in stage 4. The protocol that your program should follow in order to play a game with another player on another machine is now posted. This protocol will be processed by a class that runs in a separate thread from your main program. We can use the produce/consumer idea from the textbook. Assume that your stage 3 code contains a mouse handler for guesses that looks like this (this is a sketch, not real code you can copy):
boolean action or boolean mouseDown {
processMyGuess(); // from the position of the mouse click,
// determine hit or miss from the computer's fleet position
getComputerGuess(); // the computer generates a guess
processComputerGuess(); // determine hit or miss on my fleet position
}
There is no change to the sequence above, just in what it does:
boolean action or boolean mouseDown {
processMyGuess(); // from the position of the mouse click,
// put the guess into a shared object for
// the protocol thread to send to the other player
getOpponentGuess(); // retrieve the other player's guess (wait until it arrives)
processOpponentGuess(); // determine hit or miss on my fleet position
}