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

PLEASE HELP today 10/29/17 Can someone solve this in solve this in C++. There is

ID: 3604622 • Letter: P

Question

PLEASE HELP today 10/29/17

Can someone solve this in solve this in C++. There is a .h file screenshoted at the bottom and the instructions in the .cpp

The first two images are the questions.

The first class you will implement is called battle Ship. The battleShip.h file has been made available on Moodle. You are also given the the beginning of a main file for Battle Ship: BattleShip_main.cpp Download these files and place them into the same folder on your computer. Open them with CodeBlocks. You will implement the battleShip class and fill out the main file for part 1 of the recitation. You will create your class file battle Ship.cpp. In battleShip.cpp you will define the methods. (Remember: methods define what your object does). How many methods do you see in your header file? It will be your job to fill in these methods so that they work.The constructor always has the same name as your class file, and it never has a data type. Make sure to import the proper class file "battleShip.h" and libraries string> etc. in the header of your battleShip.cpp file. See the BattleShip_main.cpp for further instructions. You will need to create 3 instances of your class, set up some of the methods, and expand on the logic of the while loop In the game of battleship, a ship is sunk once the number of hits it has received is equal to its size. So if a ship is size 3 and has received 3 hits, it has been sunk. When you have finished, implementing these methods, show your TA to check off your work. Then move onto the next part.

Explanation / Answer

import java.util.*;

public class BattleShipGame {

    public static void main(String[] args) {
        int[][] board = new int[10][10];
        int[][] ships = new int[10][10];
        int[] shoot = new int[10];
        int attempts=0,
            shotHit=0;
      
        initBoard(board);
        initShips(ships);
      
        System.out.println();
      
        do{
            showBoard(board);
            shoot(shoot);
            attempts++;
          
            if(hit(shoot,ships)){
                hint(shoot,ships,attempts);
                shotHit++;
            }              
            else
                hint(shoot,ships,attempts);
          
            changeboard(shoot,ships,board);
          

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

    }

    public static void initShips(int[][] ships){
        Random random = new Random();
      
        for(int ship=0 ; ship < 10; ship++){
            ships[ship][0]=random.nextInt(10);
            ships[ship][1]=random.nextInt(10);
          
          
            for(int last=0 ; last < ship ; last++){
                if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
                    do{
                        ships[ship][0]=random.nextInt(5);
                        ships[ship][1]=random.nextInt(5);
                    }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[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[][] ships){
      
        for(int ship=0 ; ship<ships.length ; ship++){
            if( shoot[0]==ships[ship][0] && shoot[1]==ships[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 hint(int[] shoot, int[][] ships, int attempt){
        int row=0,
            column=0;
      
        for(int line=0 ; line < ships.length ; line++){
            if(ships[line][0]==shoot[0])
                row++;
            if(ships[line][1]==shoot[1])
                column++;
        }
      
        System.out.printf(" Hint %d: Row %d -> %d ships " +
                                 "Column %d -> %d ships ",attempt,shoot[0]+1,row,shoot[1]+1,column);
    }

    public static void changeboard(int[] shoot, int[][] ships, int[][] board){
        if(hit(shoot,ships))
            board[shoot[0]][shoot[1]]=1;
        else
            board[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