This battlship game is a Java program based on the board game of the same name. The game as it is supports 2 players taking turns on 1 screen. Players start by placing their ships and then take turns marking spots on a grid to try and hit all of the opponents ships. It was built as a group project in ICS111. It runs on the EZJava framework that was a core component of that class; you can find more about that here.
My role in the group was to develop the grid and all of its assets;
Our pre development design
My source code for the board.
public class BattleshipBoard{
//-------------MEMEBER VARIABLES--------------
protected Cell[][] Board; //2D array of cells that act as game board
protected int Xpos; //grid x begin
protected int Ypos; //grid y begin
protected int Size; //size of grid
protected int Spacing; //spacing/sizeof cells (in pixels)
ArrayList<EZImage> Markers; //array list of images for the markers on the board
//-------------CONSTRUCTORS--------------
public BattleshipBoard(int xpos,int ypos,int size, int spacing){
Xpos = xpos; //xpos start x of board
Ypos = ypos; //ypos start y of board
Size = size; //size of the board; size x size
Spacing = spacing; //spacing of the cells; each would be "spacing" pixels
Board = new Cell[Size][Size]; //initialize size x size array
Markers = new ArrayList<EZImage>();
for(int i = 0;i < Size;i++){ //for loop that fills 2D cell array
for(int j = 0;j < Size;j++)
Board[i][j] = new Cell(Xpos + i*Spacing, Ypos + j*Spacing); //spaces out each cell by spacing x i
}
}
//---------------METHODS------------------
//hides all cell markers
public void hideMarkers(){
for(int i = 0;i < Size;i++){
for(int j = 0;j < Size;j++){
Board[i][j].hideMarker();
}
}
}
//shows all cell markers
public void showMarkers(){
for(int i = 0;i < Size;i++){
for(int j = 0;j < Size;j++){
Board[i][j].showMarker();
}
}
}
//--------------ACCESSORS----------------
//returns grid top left xpos
public int getXpos(){
return Xpos;
}
//returns grid top left ypos
public int getYpos(){
return Ypos;
}
//returns spacing of cells
public int getSpacing(){
return Spacing;
}
//returns size of grid (size x size)
public int getSize(){
return Size;
}
//returns the x position of the cell image
public int getCellImgX(int indexX, int indexY){
return Board[indexX][indexY].getCellImgX();
}
//returns the y position of the cell image
public int getCellImgY(int indexX, int indexY){
return Board[indexX][indexY].getCellImgY();
}
}//END BOARD
/* Cell class that stores the data and visual representation of each cell of the gameboard
*
*/
class Cell{
EZImage Img; //cell image
EZImage Marker; //marker on the cell(red for hit, white for miss)
boolean Attacked; //holds if the ship has been attacked
boolean Hit; //if a ship on this position was hit
//-----CONSTRUCTORS-----
Cell(int xpos,int ypos){
Img = EZ.addImage("Cell.png", xpos, ypos); //add cell img
Marker = EZ.addImage("Empty.png", xpos, ypos); //initialize empty img
Attacked = false; //start not attacked
Hit = false; //start not hit
}
//------METHODS------
//attack the cell if its filled then Hit becomes true
boolean attack(boolean hit){
if(Attacked){
return false; // if false is returned that means its already been attacked
}else{
Attacked = true; //make Attacked true
Hit = hit; //set Hit equal to hit which you get from Ships;
placeMarker(); //place marker
return true; //return true; the cell is attacked
}
}
//places marker at cell position based on state
void placeMarker(){
if(Attacked && !Hit)// attacked but no ship hit; miss marker
Marker = EZ.addImage("Miss.png", Img.getXCenter(), Img.getYCenter());
else if(Attacked && Hit) //attacked and hit; hit marker
Marker = EZ.addImage("Hit.png", Img.getXCenter(), Img.getYCenter());
}
//hides the marker image
void hideMarker(){
Marker.hide();
}
//shows the marker image
void showMarker(){
Marker.show();
}
//hides the cell img
void hideCell(){
Img.hide();
}
//shows the cell img
void showCell(){
Img.show();
}
//-------ACCESSORS------
//returns Attacked
boolean getAttacked(){
return Attacked;
}
//returns Hit
boolean getHit(){
return Hit;
}
//returns state of Attacked and Hit in binary state form
int getState(){
if(!Attacked && !Hit)
return 00; //no marker state; not attacked
else if(Attacked && !Hit)
return 10; //white marker state; attacked but no ship hit
else if(Attacked && Hit)
return 11; //red marker state; attacked and ship hit
else
return 01; // something went wrong if we are in this state
}
//returns center of the cell x
int getCellImgX(){
return Img.getXCenter();
}
//returns center of the cell y
int getCellImgY(){
return Img.getYCenter();
}
}//END Cell
Battleship uses the EZJava framework and which this zipped version of the game comes with. You will also need to install java if you haven’t already. The game itself can be found here. When you unzip make sure to maintain the content structure as it is in the zipped file.
This version comes with a precompiled jar executable, if your system can run those you an just double click the ‘battleship.jar’ file in the ‘classes’ folder. Otherwise you can compile the program in command line using javac and run it.