JAVA
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.
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. Similar to how we did the TicTacToe assignment.
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.
Expert Answer
Code:
import java.util.*;
public class battleship {
char[][] myboard, ships;
public battleship(){
myboard = new char[5][5]; // User’s game board
int i, j;
// Initializing game
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++)
myboard[i][j] = ‘+’;
}
ships = setGame(); // Setting ships on board
}
public void display(char[][] myboard){
// To display board
int i,j;
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++)
System.out.print(myboard[i][j] + ” “);
System.out.println();
}
}
public void update(int row, int column, char c){
myboard[row][column] = c; // Updating user’s board after each round
}
public boolean gameover(){ // To check if game’s over
int i, j;
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++){
if(ships[i][j] == ‘b’ && myboard[i][j] != ‘X’){ // If any location of ship has not been hit yet
return false;
}
}
}
return true;
}
public void play(){
int row, column;
Scanner sc = new Scanner(System.in);
display(myboard);
while(!gameover()){ // Iterates until game’s over
System.out.print(“Enter row no.: “);
row = sc.nextInt() – 1;
System.out.print(“Enter column no.: “);
column = sc.nextInt() – 1;
while(!(row >= 0 && row <= 4 && column >= 0 && column <= 4)){ // If user enters wrong location details
System.out.println(“Location doesn’t exist. Please try again.n”);
System.out.print(“Enter row no.: “);
row = sc.nextInt() – 1;
System.out.print(“Enter column no.: “);
column = sc.nextInt() – 1;
}
if(ships[row][column] == ‘b’) // If it is a hit
update(row, column, ‘X’);
else
update(row, column, ‘O’);
display(myboard);
}
System.out.println(“Game Overn”);
}
public char[][] setGame()
{
char[][] shipBoard = new char[5][5];
int i, j, k, range = 5, alignment, startRow, startCol, len;
boolean isfree;
for(i = 0; i < 5; i++)
for(j = 0; j < 5; j++)
shipBoard[i][j] = ‘*’;
Random rand = new Random();
// Setting ships on board randomly
for(i = 1; i <= 5; i++)
{
alignment = rand.nextInt(2); // 0 – row wise, 1 – column wise
if(alignment == 0) // Set ship row wise on board
{
startRow = rand.nextInt(range); // Select row
startCol = rand.nextInt(range – 1); // Select column so that ship can be of length of 2 blocks
len = rand.nextInt((range – startCol – 1 – 1) + 1) + 1; // Select length so that ship can be of 2 blocks at least
if((startCol + len) >= range) // Checking if there’s place for ship
isfree = false;
else{
isfree = true;
for(j = startCol; j <= startCol + len; j++)
{
if(shipBoard[startRow][j] == ‘b’) // Checking if the place has already been booked
{
isfree = false;
break;
}
}
}
if(isfree){ // If there’s place, then place the ship
for(j = startCol; j <= startCol + len; j++)
shipBoard[startRow][j] = ‘b’;
}
else
i–; // Otherwise iterate one more item
}
else // For column wise alignment of ship on board
{
startCol = rand.nextInt(range); // Select start column
startRow = rand.nextInt(range – 1); // Select start row so that ship can be of at least 2 blocks length
len = rand.nextInt((range – startRow – 1 – 1) + 1) + 1; // Choose length so that ship can be of 2 blocks length
if((startRow + len) >= range) // Checking if there’s place for placing ship
isfree = false;
else{
isfree = true;
for(j = startRow; j <= startRow + len; j++)
{
if(shipBoard[j][startCol] == ‘b’){ // If the place has been booked or not
isfree = false;
break;
}
}
}
if(isfree){ // If there’s place, then place ship
for(j = startRow; j <= startRow + len; j++)
shipBoard[j][startCol] = ‘b’;
}
else
i–;
}
}
return shipBoard;
}
public static void main(String args[]){
battleship board = new battleship(); // Create object
board.play(); // Start the game
}
}
Output: