Question: This is my Player.java, can you create a PlayerTester.java. Make sure to include Java doc and comments, please see if Player.Java is written correctly, grade

This is my Player.java, can you create a PlayerTester.java. Make sure to include Java doc and comments, please see if Player.Java is written correctly, grade 12 level coding with java only.

import java.util.Scanner;

public class Player { private static final int[] SHIP_LENGTHS = {2, 3, 3, 4, 5}; private Grid playerGrid; private Grid opponentGrid; private static final int NUM_SHIPS = 5; public Scanner reader;

public Player(Scanner x, Grid compGrid) { reader = x; playerGrid = new Grid(); opponentGrid = RandGrid; }

public void printMyShips() { playerGrid.printShips(); }

public void printOpponentGuesses() { playerGrid.printStatus(); }

public void printMyGuesses() { opponentGrid.printStatus(); }

public int recordOpponentGuess(int row, int col) { if(playerGrid.hasShip(row, col) == true) { playerGrid.markHit(row, col); return 1; } else { playerGrid.markMiss(row, col); return 0; } }

public int recordMyGuess(int row, int col) { if(opponentGrid.hasShip(row, col) == true) { opponentGrid.markHit(row, col); return 1; } else { opponentGrid.markMiss(row, col); return 0; } }

public boolean chooseShipLocation(Ship s, int row, int col, int direction) { s.setLocation(row, col); s.setDirection(direction); return playerGrid.addShip(s); }

public void askForShips() { int shipRow = 0; int shipCol = 0; int shipDirection = 0;

for (int i = 0; i < NUM_SHIPS; i++) { System.out.println("Your current grid of ships."); printMyShips(); System.out.println();

System.out.println("Now you need to place a ship of length " + Player.SHIP_LENGTHS[i] + "."); System.out.print("Which row? (1-10) "); shipRow = reader.nextInt()-1; System.out.println("Which column? (1-10) "); shipCol = reader.nextInt()-1; System.out.println("Which direction? (0 aka horizontal or 1 aka verticle) "); shipDirection = reader.nextInt(); Ship s = new Ship(SHIP_LENGTHS[i]); boolean check = chooseShipLocation(s, shipRow, shipCol, shipDirection); if(check == false) { System.out.println("Invalid location. Pick new location: "); i--; } } } }

Now that we have written Ship, Location, and Grid, now it is time to write the Player class.

Player will handle the logic for our two players, the user player and the computer player.

Each player has a few key characteristics.

They have a list of 5 ships. Each ship is of a set length. You have one ship of length 2, two ships of length 3, one ship of length 4 and one ship of length 5.

Putting these values in an array will be very handy.

// These are the lengths of all of the ships. private static final int[] SHIP_LENGTHS = {2, 3, 3, 4, 5};

So each Player should have a list of five ships. How can you store this?

Then each Player also will have 2 Grids. One grid represents that players grid, and the other grid represents their opponents grid. On the players grid, they will mark the location of their ships, as well as record the guesses of their opponent. On the opponents grid, the player will record their own guesses of where they think the battleships are.

In the constructor you should set up the instance variables.

For testing purposes here you may also want a method like

public void chooseShipLocation(Ship s, int row, int col, int direction)

This will set a ships row, column and direction and add it to the current players grid. You can use this to hard code in testing placement of battleships.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!