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/ 5
/*
* 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 declaration for project organization
package com.oop2024.battelship;
// Author's name (metadata for code ownership)
/** * * @author yeabs */
// Import Random for random number generation
import java.util.Random; // Import Scanner for user input handling import java.util.Scanner;
// Define the main game class
public class BattelShipGame { // Random generator instance for game use public static Random rnd= new Random(); // Define the size of the game grid private int sizeOfGrid=7; // Define the default size of each ship private int sizeOfship=3; // Number of ships in the game private int numOfShip=3; // Game grid represented as a 2D array private int[][] grid=new int[sizeOfGrid][sizeOfGrid]; // Array to hold all ship objects private Ship[] battleShips= new Ship[numOfShip]; // Track which ships are sunk private boolean[] sankShips= new boolean[numOfShip]; // Initial score based on the grid area private int score=sizeOfGrid*sizeOfGrid;
// Method to initialize and populate the game grid
private void initGrid(){ // Reset the grid array with current grid size this.grid=new int[this.sizeOfGrid][this.sizeOfGrid]; // Reset battleShips array to store ships this.battleShips= new Ship[this.numOfShip]; // Reset sankShips array to track each ship's status this.sankShips= new boolean[this.numOfShip]; // Reset score to grid area value this.score=this.sizeOfGrid*this.sizeOfGrid; // Loop to place ships on the grid int i=0; do{ // Generate a random new location for the ship int[][] sl=this.newlocation(); // Check if this location is already taken boolean taken=this.takenLocation(sl); // If location is free, create and add the ship if(!taken){ Ship s= new Ship(this.sizeOfship,sl); this.battleShips[i]=s; i++; // Mark grid positions with the ship's location this.markLocation(sl); } }while(i<this.battleShips.length); }
// Method to get the current score
public int getScore(){ return score; } // Method to mark grid positions occupied by a ship private void markLocation(int[][] nloc){ for (int[] loc: nloc) { this.grid[loc[0]][loc[1]]=1; } }
// Method to check if a location is already taken by another ship
// Method to generate a new random location for a ship
public int[][] newlocation(){ int rc=0; int rr=0; // Decide randomly if ship is placed horizontally or vertically int hv= rnd.nextInt(0, 2); if(hv==0){ // Set column starting point for horizontal ship placement rc=rnd.nextInt(0, this.sizeOfGrid-this.sizeOfship+1); rr=rnd.nextInt(0, this.sizeOfGrid); } else{ // Set row starting point for vertical ship placement rc=rnd.nextInt(0, this.sizeOfGrid); rr=rnd.nextInt(0, this.sizeOfGrid-this.sizeOfship+1); } // Initialize array to hold ship's coordinates int[][] sl=new int[this.sizeOfship][2]; if(hv==0){ // Generate coordinates for horizontal placement for(int i=0;i<sl.length;i++){ sl[i][0]=rr; sl[i][1]=rc+i; } } else{ // Generate coordinates for vertical placement for(int i=0;i<sl.length;i++){ sl[i][0]=rr+i; sl[i][1]=rc; } } return sl; }
// Print the current grid with symbols representing ship/hit/sunk status
void printgrid(){ for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { // Display S for ships, O for hits, X for sunk, and empty for others switch (this.grid[i][j]) { case 1: System.out.print("| S |"); break; case 2: System.out.print("| O |"); break; case 3: System.out.print("| X |"); break; default: System.out.print("| |"); break; } } System.out.println(" ___________________________________"); } }
// Main game loop to play the game
public int playGame(){ int sankShip=0; while(this.score!=0){ // Prompt user to input row and column for attack Scanner ui=new Scanner(System.in); System.out.print("\n number between 0-"+this.sizeOfGrid+" for the row: "); int r=(ui.nextInt())%this.sizeOfGrid; System.out.print("\n number between 0-"+this.sizeOfGrid+" for the column: "); int c=(ui.nextInt())%this.sizeOfGrid; int[] guess={r,c}; // Decrement score on each attempt this.score--; // Loop through each ship to check if guess hit a ship for (int i=0;i<this.sankShips.length;i++) { if(this.sankShips[i]==false){ Ship battleShip=this.battleShips[i]; boolean hit = battleShip.checkStatus(guess); if (hit) { System.out.println("You have hit One of the ships!"); this.grid[r][c]=2; boolean sank = battleShip.getKilled(); if(sank){ this.grid[r][c]=3; this.sankShips[i]=true; sankShip++; System.out.println("You have sank One of the ships!"); if(sankShip==this.numOfShip){ System.out.println("You Won!! \n score="+this.score); return 1; } } } else { System.out.println("Missed :("); } } } } System.out.println("No more tries! sorry :-("); return 0; }
// Reset the game by reinitializing the grid and ships