RocketShip Java Code: Complete every method and filled the main method. The board will have letters representing the columns and numbers representing the rows. The
RocketShip Java Code:
Complete every method and filled the main method. The board will have letters representing the columns and numbers representing the rows. The coordinate a7 will mean the a (column) and 7 (row). Your code should treat a7 and A7 as the same spot. Rows will start from 1 not 0. Treat the top left corner as a1. Should be able to read file to get the inputs of dimensions and players ships. The placements of the ships will be guaranteed to be valid, and there can be a varying number of ship locations (however player 1 and 2 will have the same number). Example:
gameExample.txt
5 // board will be 5x5
b3 b4 a1 a4 a3 c2 c5 e3 // player 1s ships locations
c4 c3 e5 e3 a2 b3 d1 d2 // player 2 ship locations
Guidelines:
The game should start by printing what player 1 sees (a grid of player 2s board) and how many hits they have remaining.
Legend:
~: Unguessed
X: Hit
O: Miss.
A scanner should record the users guess through the cmd (command line). If successful, print Hit! and change the board location from ~ to X. Otherwise, print Miss! and change the board location from ~ to O. Re-print the board after each guess. Repeat for player 2. The game should continue interchanging turns until one player has hit all of the others ships.
Additional Information:
initilizeRocket (): will take in parameter n and create a 2D char array of size n x n. Returns the initialized char[][].
viewRocket (): this method prints the board array, treat the top left corner as [0][0].
Missiles (): this method will take in a players board, the targetSpace string of the location being fired at, a Locations string array of where the ships were originally placed, and hits representing the number of hits the player firing needs to hit left. If the selected target location has already been hit, print to the command line targetSpace (a5) has already been chosen! and should still be counted as a turn. Returns how many hits left the firing player has left after calling this Missiles () method.
TrueorFalse(): targetSpace is the string coordinate of where on the board to look, Locations is a string array of where the ships were originally placed. Returns whether the location is a ship or not.
conversion(): takes in a string coor (c1) and converts this to an int array of the corresponding indices of the board e.g. {0, 5} (represented by c1). Note that the row must be the first element and the column the second
main(): the main method is where the code to play will be. Also will ask the players to make a console input of a coordinate. The input should rotate between the two players. Use all of the methods above inside your main method.
Code to Complete:
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.Random;
public class RocketShip {
public static char[][] initilizeRocket(int n) {
// initializes a board of size n * n
return null;
}
public static void viewRocket(int playerTurn, char[][] board,
int hits) {
// print the board and player information
}
public static int Missiles(char[][] board, String targetSpace,
String[] Locations, int hits) {
// updates the board given the missile target and returns if a ship is hit
return 0;
}
public static boolean TrueorFalse(String targetSpace, String[] Locations) {
// returns if location is ship
return false;
}
public static int[] conversion(String coor) {
// returns the integer indices corresponding with the coordinate string
return null;
}
public static void main(String[] args) {
int fileIndex = (args.length > 0) ? Integer.parseInt(args[0])
: new Random().nextInt(4);
String filename = "game" + fileIndex + ".txt";
Scanner input = new Scanner(System.in);
Scanner fileReader = new Scanner(new File(filename));
// execute with methods
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started