Question & Answer: You are to create the framework for a Battleship game. You need to create a 5×5 grid that represents the gameboard. "+" signs repre…..

JAVA LANGUAE ONLY!

You are to create the framework for a Battleship game. You need to create a 5×5 grid that represents the gameboard. “+” signs represent spaces that haven’t been bombed. “O” represents spaces that have been bombed, but nothing was hit. “X” represents spaces where the player has bombed and hit something.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: You are to create the framework for a Battleship game. You need to create a 5×5 grid that represents the gameboard. "+" signs repre…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

For example:

+++++

++++O

+++X+

+++X+

+++++

The user will enter a grid space by entering two integers that represent the X and the Y coordinates on the grid.

You will create a battleship that class that represents the ship. It will contain the data about it’s own hits and will know when all of it’s spaces have been hit and it is sunk.

JAVA LANGAUGE

Expert Answer

 

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.august;

import java.util.ArrayList;

/**
*
* @author Sam
*/
class BattleShip {
public enum Orientation {
HORIZONTAL,
VERTICAL
}
private final int startX;
private final int startY;
private final int length;
private final Orientation orientation;

public BattleShip(int startX, int startY, int length, Orientation orientation) {
this.startX = startX;
this.startY = startY;
this.length = length;
this.orientation = orientation;
if (orientation == null)
throw new IllegalArgumentException(“orientaion cannot be null”);
if (startX < 0 || startY < 0)
throw new IllegalArgumentException(“x and y cannot be less than zero”);
if (orientation == Orientation.HORIZONTAL)
if (startX + length > BattleShipGame.maxX)
throw new IllegalArgumentException(“ship exceeds board limit”);
if (orientation == Orientation.VERTICAL)
if (startY + length > BattleShipGame.maxY)
throw new IllegalArgumentException(“ship exceeds board limit”);

}

boolean isIn(int x, int y) {
if (orientation == Orientation.HORIZONTAL) {
if (startY == y && x >= startX && x < startX + length)
return true;
}
else if (orientation == Orientation.VERTICAL) {
if (startX == x && y >= startY && y < startY + length)
return true;
}
return false;
}

boolean overlaps(BattleShip other) {
int maxX = (orientation == Orientation.HORIZONTAL)?startX+length:startX;
int maxY = (orientation == Orientation.VERTICAL)?startY+length:startY;
for (int i = startX; i < maxX ; i++)
for (int j = startY; j < maxY; j++)
if (other.isIn(j, j))
return true;
return false;
}
}

class BattleShipGame {
static final int maxX = 5;
static final int maxY = 5;
private final char [][] gameBoard;
private final ArrayList<BattleShip> battleShips;

public BattleShipGame(ArrayList<BattleShip> battleShips) {
gameBoard = new char[maxX][maxY];
for (int i = 0; i < maxX; i++)
for (int j = 0; j < maxY; j++)
gameBoard[i][j] = ‘-‘;
this.battleShips = new ArrayList<>();
for (BattleShip battleShip: battleShips) {
for (BattleShip addedShips: this.battleShips)
if (battleShip.overlaps(addedShips))
throw new IllegalArgumentException(“Ships overlapping other ships”);
this.battleShips.add(battleShip);
}
}

boolean fire(int x, int y) {
if (gameBoard[x][y] != ‘-‘) {
System.err.println(“You cannot hit a slot more than once”);
return false;
}
for (BattleShip battleShip:battleShips)
if (battleShip.isIn(x,y)) {
gameBoard[x][y] = ‘X’;
return true;
}
gameBoard[x][y] = ‘O’;
return false;
}
}

This sould work well. Do you need a main method too?

Still stressed from student homework?
Get quality assistance from academic writers!