0% found this document useful (0 votes)
9 views2 pages

BattleShipGame Code Explanation

Oop in java

Uploaded by

minasetilayee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

BattleShipGame Code Explanation

Oop in java

Uploaded by

minasetilayee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

BattleShipGame.

java Code Explanation


/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package com.oop2024.battelship;

/**
* The author of this code is 'yeabs'.
*/

import java.util.Random; // This allows us to create random numbers


import java.util.Scanner; // This allows us to get user input

public class BattleShipGame {

This line starts the BattleShipGame class. All of the code for the game will be inside this
class.

public static Random rnd = new Random();


This line creates a Random object that will help us generate random numbers in the game.

private int sizeOfGrid = 7;


This line sets the size of the game grid (7x7). Each square on the grid is a space where a ship
could be.

private int sizeOfShip = 3;


This line defines the size of each ship as 3 spaces.

private int numOfShip = 3;


This line sets the number of ships in the game to 3.

private int[][] grid = new int[sizeOfGrid][sizeOfGrid];


This line creates a 2D array for the game grid where each position can be empty, have a
ship, or show a hit or miss.

private Ship[] battleShips = new Ship[numOfShip];


This line makes an array to hold all the ships in the game.

private boolean[] sankShips = new boolean[numOfShip];


This line makes a boolean array to track if each ship has sunk.
private int score = sizeOfGrid * sizeOfGrid;
This line initializes the player score. The score will decrease with each guess.

BattleShipGame(){
this.initGrid();
}
This is a constructor that calls initGrid to set up the grid when a new game is created.

BattleShipGame(int numOfShip){
this.numOfShip = numOfShip;
this.initGrid();
}
This is another constructor allowing the number of ships to be customized.

BattleShipGame(int sizeOfGrid, int sizeOfShip, int numOfShip){


this.sizeOfGrid = sizeOfGrid;
this.sizeOfShip = sizeOfShip;
this.numOfShip = numOfShip;
this.initGrid();
}
This constructor allows customizing the grid size, ship size, and number of ships.

You might also like