Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So this is basic BattleShip game written in C++ language. it should use a 6*6 bo

ID: 3816308 • Letter: S

Question

So this is basic BattleShip game written in C++ language.

it should use a 6*6 board. This is a single-player game where the player tries to guess where computer placed 2 ships. (size 2 and 3)

Basic explanation and requirement are printed below.

It must use arrays and no strings.

Please don't use vector please.

Thank you very much.

Learning objectives: The intent of this programing project is to allow you the opportunity to demonstrate your ability to solve problems using procedural C++ programming. This project will focus on using loops, functions, random numbers, and arrays in the implementation of a modified version of the board game Battleship Program Description: In this project, you will write a C++ program that simulates a modified version of the board game Battleship. In this version there will only be one player who will try to find the two ships, a Destroyer and a Submarine, that the computer has randomly placed somewhere on the 6x6 grid board (i.e. the ocean) The Destroyer take-up two positions in the 6x6 grid whereas the Submarine takes up three positions. Ships can only be place horizontally or vertically on the 6x6 grid. Destroyer Submarine

Explanation / Answer

package com.napier.test;
import java.util.Random;
import java.util.Scanner;

public class BattleShipTest {

public static void main(String[] args) {
int[][] boardArr = new int[6][6];
int[][] shipArr = new int[4][2];
int[] shoot = new int[2];
int attempts=0,
shotHit=0;
  
initboardArr(boardArr);
initShipArr(shipArr);
  
System.out.println();
  
do{
showBoardArr(boardArr);
shoot(shoot);
attempts++;
  
if(hit(shoot,shipArr)){
hint(shoot,shipArr,attempts);
shotHit++;
}
else
hint(shoot,shipArr,attempts);
  
changeboardArr(shoot,shipArr,boardArr);
  

}while(shotHit!=3);
  
System.out.println(" Battleship Java game finished! You hit 3 shipArr in "+attempts+" attempts");
showBoardArr(boardArr);
}
  
public static void initboardArr(int[][] boardArr){
for(int row=0 ; row < 5 ; row++ )
for(int column=0 ; column < 5 ; column++ )
boardArr[row][column]=-1;
}
  
public static void showBoardArr(int[][] boardArr){
System.out.println(" 1 2 3 4 5");
System.out.println();
  
for(int row=0 ; row < 5 ; row++ ){
System.out.print((row+1)+"");
for(int column=0 ; column < 5 ; column++ ){
if(boardArr[row][column]==-1){
System.out.print(" "+"~");
}else if(boardArr[row][column]==0){
System.out.print(" "+"*");
}else if(boardArr[row][column]==1){
System.out.print(" "+"X");
}
  
}
System.out.println();
}

}

public static void initShipArr(int[][] shipArr){
Random random = new Random();
  
for(int ship=0 ; ship < 3 ; ship++){
shipArr[ship][0]=random.nextInt(5);
shipArr[ship][1]=random.nextInt(5);
  
for(int last=0 ; last < ship ; last++){
if( (shipArr[ship][0] == shipArr[last][0])&&(shipArr[ship][1] == shipArr[last][1]) )
do{
shipArr[ship][0]=random.nextInt(5);
shipArr[ship][1]=random.nextInt(5);
}while( (shipArr[ship][0] == shipArr[last][0])&&(shipArr[ship][1] == shipArr[last][1]) );
}
  
}
}

public static void shoot(int[] shoot){
Scanner input = new Scanner(System.in);
  
System.out.print("Row: ");
shoot[0] = input.nextInt();
shoot[0]--;
  
System.out.print("Column: ");
shoot[1] = input.nextInt();
shoot[1]--;
  
}
  
public static boolean hit(int[] shoot, int[][] shipArr){
  
for(int ship=0 ; ship<shipArr.length ; ship++){
if( shoot[0]==shipArr[ship][0] && shoot[1]==shipArr[ship][1]){
System.out.printf("You hit a ship located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
return true;
}
}
return false;
}

  

public static void changeboardArr(int[] shoot, int[][] shipArr, int[][] boardArr){
if(hit(shoot,shipArr))
boardArr[shoot[0]][shoot[1]]=1;
else
boardArr[shoot[0]][shoot[1]]=0;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote