Java

Battleship

28 points

 

Complete Program

I made all instance variables public for this project. There are 4 cooperating classes, and having the variables public makes it easier to access a variable that is stored inside another class. You give the name of the class where the variable is stored, followed by the variable name. For example: bShip.readyToStartGame.

 

Starting Point

You can download the files BattleShip.java, CellsPanel.java, Cell.java, Player.java, and BShip.html to use as starting points in writing this program. Instructions for writing code are given in these Java files.

Note: This program is based on the classic board game "Battleship".

Here are some screenshots of the game being played:

First both players are asked their names:

Then 1 player has exclusive use of the screen while he positions his ships:

This is what it looks like after player 1 has finished positioning his ships:

Then it is the other player's turn to position his ships:

Here the game is about to begin, and player 1 will take his first shot:

A yellow dot appears where the player clicks, before he makes the move final by clicking the button:

After he clicks the button, a white dot tells him that he missed player 2's boats:

On his second move, a red dot tells him that he hit one of player 2's boats:

This screen shows that player 1 has sunk one of player 2's boats:

This screen shows that player 2 has sunk one of player 1's boats:

This screen shows that player 2 has won the game:

 

Demo

A JAR file for Battleship is now available. Download the JAR file, double-click on it and the game should run.