import java.util.Scanner;public class Main { public static in.pdf
1. import java.util.Scanner;
public class Main {
public static int gridArray1[][] = null;
public static int gridArray2[][] = null;
public static boolean playerTurn = true;
public static int count = 0;
public static void setGridDimensions(int x_max, int y_max) {
Main.gridArray1 = new int[x_max][y_max];
Main.gridArray2 = new int[x_max][y_max];
for (int i = 0; i < x_max; i++) {
for (int j = 0; j < y_max; j++) {
gridArray1[i][j] = -1;
gridArray2[i][j] = -1;
}
}
}
static void placeShip(int starting_x, int starting_y, int length, int direction) {
count++;
int ships[] = new int[10];
int battleshipArrayPositions1[] = new int[4];
int cruiserArrayPositions1[] = new int[3];
int submarineArrayPositions1[] = new int[3];
int destroyerArrayPositions1[] = new int[2];
int carrierArrayPositions2[] = new int[5];
int battleshipArrayPositions2[] = new int[4];
int cruiserArrayPositions2[] = new int[3];
int submarineArrayPositions2[] = new int[3];
int destroyerArrayPositions2[] = new int[2];
if (playerTurn) {
if (direction == 0) {
for (int i = starting_x; i < starting_x + length; i++) {
2. Main.gridArray1[i][starting_y] = 0;
}
} else {
for (int i = starting_y; i < starting_y + length; i++) {
Main.gridArray1[starting_x][i] = 0;
}
}
} else {
if (direction == 0) {
for (int i = starting_x; i < starting_x + length; i++) {
Main.gridArray2[i][starting_y] = 0;
}
} else {
for (int i = starting_y; i < starting_y + length; i++) {
Main.gridArray2[starting_x][i] = 0;
}
}
}
}
boolean isConflictingShipPlacement(int starting_x, int starting_y, int length, int direction) {
return false;
}
static int shoot(int x, int y) {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (Main.gridArray2[i][j] == 0) {
return 0;
}
}
}
} else {
}
return 0;
}
boolean hasBeenAttempted(int x, int y) {
3. return false;
}
static void displayGrid(boolean showShips) {
int count = 0;
System.out.print(" 0 1 2 3 4 5 6 7 8 9");
if (showShips) {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray1[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 0) {
System.out.print("@ ");
} else if (Main.gridArray1[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
} else {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray2[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray2[i][j] == 0) {
System.out.print("@ ");
} else if (Main.gridArray2[i][j] == 1) {
4. System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
}
} else {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray1[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 0) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
} else {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray2[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray2[i][j] == 0) {
5. System.out.print("- ");
} else if (Main.gridArray2[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
}
}
System.out.println("");
}
public static void main(String[] args) {
int carrierLength = 5, battleshipLength = 4, cruiserLength = 3, submarineLength = 3,
destroyerLength = 2,
countPlayer1 = 5, countPlayer2 = 5, x, y, carrierDirectionNumber,
battleshipDirectionNumber,
cruiserDirectionNumber, submarineDirectionNumber, destroyerDirectionNumber,
shotReturn;
String carrier, battleship, cruiser, submarine, destroyer, carrierDirection,
battleshipDirection,
cruiserDirection, submarineDirection, destroyerDirection, shot;
setGridDimensions(10, 10);
Scanner scan = new Scanner(System.in);
for (int i = 0; i <= 1; i++) {
if (playerTurn) {
System.out.println("PLAYER 1 TURN");
displayGrid(true);
System.out.println("Place your Carrier (length 5): ");
carrier = scan.nextLine();
String carrierArray[] = carrier.split(",");
x = Integer.valueOf(carrierArray[0]);
y = Integer.valueOf(carrierArray[1]);
System.out.println("Choose direction (d/r): ");
carrierDirection = scan.nextLine();
if (carrierDirection.equals("d")) {
6. carrierDirection = "0";
} else {
carrierDirection = "1";
}
carrierDirectionNumber = Integer.valueOf(carrierDirection);
placeShip(x, y, carrierLength, carrierDirectionNumber);
displayGrid(true);
System.out.println("Place your Battleship (length 4): ");
battleship = scan.nextLine();
String battleshipArray[] = battleship.split(",");
x = Integer.valueOf(battleshipArray[0]);
y = Integer.valueOf(battleshipArray[1]);
System.out.println("Choose direction (d/r): ");
battleshipDirection = scan.nextLine();
if (battleshipDirection.equals("d")) {
battleshipDirection = "0";
} else {
battleshipDirection = "1";
}
battleshipDirectionNumber = Integer.valueOf(battleshipDirection);
placeShip(x, y, battleshipLength, battleshipDirectionNumber);
displayGrid(true);
System.out.println("Place your Cruiser (length 3): ");
cruiser = scan.nextLine();
String cruiserArray[] = cruiser.split(",");
x = Integer.valueOf(cruiserArray[0]);
y = Integer.valueOf(cruiserArray[1]);
System.out.println("Choose direction (d/r): ");
cruiserDirection = scan.nextLine();
if (cruiserDirection.equals("d")) {
cruiserDirection = "0";
} else {
cruiserDirection = "1";
}
cruiserDirectionNumber = Integer.valueOf(cruiserDirection);
placeShip(x, y, cruiserLength, cruiserDirectionNumber);
7. displayGrid(true);
System.out.println("Place your Submarine (length 3): ");
submarine = scan.nextLine();
String submarineArray[] = submarine.split(",");
x = Integer.valueOf(submarineArray[0]);
y = Integer.valueOf(submarineArray[1]);
System.out.println("Choose direction (d/r): ");
submarineDirection = scan.nextLine();
if (submarineDirection.equals("d")) {
submarineDirection = "0";
} else {
submarineDirection = "1";
}
submarineDirectionNumber = Integer.valueOf(submarineDirection);
placeShip(x, y, submarineLength, submarineDirectionNumber);
displayGrid(true);
System.out.println("Place your Destroyer (length 2): ");
destroyer = scan.nextLine();
String destroyerArray[] = destroyer.split(",");
x = Integer.valueOf(destroyerArray[0]);
y = Integer.valueOf(destroyerArray[1]);
System.out.println("Choose direction (d/r): ");
destroyerDirection = scan.nextLine();
if (destroyerDirection.equals("d")) {
destroyerDirection = "0";
} else {
destroyerDirection = "1";
}
destroyerDirectionNumber = Integer.valueOf(destroyerDirection);
placeShip(x, y, destroyerLength, destroyerDirectionNumber);
displayGrid(true);
playerTurn = false;
} else {
System.out.println("PLAYER 2 TURN");
displayGrid(true);
System.out.println("Place your Carrier (length 5): ");
8. carrier = scan.nextLine();
String carrierArray[] = carrier.split(",");
x = Integer.valueOf(carrierArray[0]);
y = Integer.valueOf(carrierArray[1]);
System.out.println("Choose direction (d/r): ");
carrierDirection = scan.nextLine();
if (carrierDirection.equals("d")) {
carrierDirection = "0";
} else {
carrierDirection = "1";
}
carrierDirectionNumber = Integer.valueOf(carrierDirection);
placeShip(x, y, carrierLength, carrierDirectionNumber);
displayGrid(true);
System.out.println("Place your Battleship (length 4): ");
battleship = scan.nextLine();
String battleshipArray[] = battleship.split(",");
x = Integer.valueOf(battleshipArray[0]);
y = Integer.valueOf(battleshipArray[1]);
System.out.println("Choose direction (d/r): ");
battleshipDirection = scan.nextLine();
if (battleshipDirection.equals("d")) {
battleshipDirection = "0";
} else {
battleshipDirection = "1";
}
battleshipDirectionNumber = Integer.valueOf(battleshipDirection);
placeShip(x, y, battleshipLength, battleshipDirectionNumber);
displayGrid(true);
System.out.println("Place your Cruiser (length 3): ");
cruiser = scan.nextLine();
String cruiserArray[] = cruiser.split(",");
x = Integer.valueOf(cruiserArray[0]);
y = Integer.valueOf(cruiserArray[1]);
System.out.println("Choose direction (d/r): ");
cruiserDirection = scan.nextLine();
9. if (cruiserDirection.equals("d")) {
cruiserDirection = "0";
} else {
cruiserDirection = "1";
}
cruiserDirectionNumber = Integer.valueOf(cruiserDirection);
placeShip(x, y, cruiserLength, cruiserDirectionNumber);
displayGrid(true);
System.out.println("Place your Submarine (length 3): ");
submarine = scan.nextLine();
String submarineArray[] = submarine.split(",");
x = Integer.valueOf(submarineArray[0]);
y = Integer.valueOf(submarineArray[1]);
System.out.println("Choose direction (d/r): ");
submarineDirection = scan.nextLine();
if (submarineDirection.equals("d")) {
submarineDirection = "0";
} else {
submarineDirection = "1";
}
submarineDirectionNumber = Integer.valueOf(submarineDirection);
placeShip(x, y, submarineLength, submarineDirectionNumber);
displayGrid(true);
System.out.println("Place your Destroyer (length 2): ");
destroyer = scan.nextLine();
String destroyerArray[] = destroyer.split(",");
x = Integer.valueOf(destroyerArray[0]);
y = Integer.valueOf(destroyerArray[1]);
System.out.println("Choose direction (d/r): ");
destroyerDirection = scan.nextLine();
if (destroyerDirection.equals("d")) {
destroyerDirection = "0";
} else {
destroyerDirection = "1";
}
destroyerDirectionNumber = Integer.valueOf(destroyerDirection);
10. placeShip(x, y, destroyerLength, destroyerDirectionNumber);
displayGrid(true);
}
}
while (countPlayer1 != 0 || countPlayer2 != 0) {
if (playerTurn) {
System.out.println("PLAYER 1 TURN");
System.out.println("Take a shot: ");
shot = scan.nextLine();
String shotArray[] = shot.split(",");
x = Integer.valueOf(shotArray[0]);
y = Integer.valueOf(shotArray[1]);
shotReturn = shoot(x, y);
if (shotReturn == -1) {
System.out.println("Miss!");
} else if (shotReturn == 0) {
System.out.println("Hit!");
countPlayer2--;
} else {
System.out.println("Ship sank!");
}
playerTurn = false;
} else {
System.out.println("PLAYER 2 TURN");
System.out.println("Take a shot: ");
shot = scan.nextLine();
String shotArray[] = shot.split(",");
x = Integer.valueOf(shotArray[0]);
y = Integer.valueOf(shotArray[1]);
shotReturn = shoot(x, y);
if (shotReturn == -1) {
System.out.println("Miss!");
} else if (shotReturn == 0) {
System.out.println("Hit!");
countPlayer1--;
} else {
11. System.out.println("Ship sank!");
}
playerTurn = true;
}
}
}
}
Solution
import java.util.Scanner;
public class Main {
public static int gridArray1[][] = null;
public static int gridArray2[][] = null;
public static boolean playerTurn = true;
public static int count = 0;
public static void setGridDimensions(int x_max, int y_max) {
Main.gridArray1 = new int[x_max][y_max];
Main.gridArray2 = new int[x_max][y_max];
for (int i = 0; i < x_max; i++) {
for (int j = 0; j < y_max; j++) {
gridArray1[i][j] = -1;
gridArray2[i][j] = -1;
}
}
}
static void placeShip(int starting_x, int starting_y, int length, int direction) {
count++;
int ships[] = new int[10];
int battleshipArrayPositions1[] = new int[4];
int cruiserArrayPositions1[] = new int[3];
int submarineArrayPositions1[] = new int[3];
int destroyerArrayPositions1[] = new int[2];
12. int carrierArrayPositions2[] = new int[5];
int battleshipArrayPositions2[] = new int[4];
int cruiserArrayPositions2[] = new int[3];
int submarineArrayPositions2[] = new int[3];
int destroyerArrayPositions2[] = new int[2];
if (playerTurn) {
if (direction == 0) {
for (int i = starting_x; i < starting_x + length; i++) {
Main.gridArray1[i][starting_y] = 0;
}
} else {
for (int i = starting_y; i < starting_y + length; i++) {
Main.gridArray1[starting_x][i] = 0;
}
}
} else {
if (direction == 0) {
for (int i = starting_x; i < starting_x + length; i++) {
Main.gridArray2[i][starting_y] = 0;
}
} else {
for (int i = starting_y; i < starting_y + length; i++) {
Main.gridArray2[starting_x][i] = 0;
}
}
}
}
boolean isConflictingShipPlacement(int starting_x, int starting_y, int length, int direction) {
return false;
}
static int shoot(int x, int y) {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (Main.gridArray2[i][j] == 0) {
13. return 0;
}
}
}
} else {
}
return 0;
}
boolean hasBeenAttempted(int x, int y) {
return false;
}
static void displayGrid(boolean showShips) {
int count = 0;
System.out.print(" 0 1 2 3 4 5 6 7 8 9");
if (showShips) {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray1[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 0) {
System.out.print("@ ");
} else if (Main.gridArray1[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
} else {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
14. if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray2[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray2[i][j] == 0) {
System.out.print("@ ");
} else if (Main.gridArray2[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
}
} else {
if (playerTurn) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray1[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 0) {
System.out.print("- ");
} else if (Main.gridArray1[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
} else {
15. for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j % 10 == 0) {
System.out.print(" " + count + " ");
count++;
}
if (Main.gridArray2[i][j] == -1) {
System.out.print("- ");
} else if (Main.gridArray2[i][j] == 0) {
System.out.print("- ");
} else if (Main.gridArray2[i][j] == 1) {
System.out.print("+ ");
} else {
System.out.print("X ");
}
}
}
}
}
System.out.println("");
}
public static void main(String[] args) {
int carrierLength = 5, battleshipLength = 4, cruiserLength = 3, submarineLength = 3,
destroyerLength = 2,
countPlayer1 = 5, countPlayer2 = 5, x, y, carrierDirectionNumber,
battleshipDirectionNumber,
cruiserDirectionNumber, submarineDirectionNumber, destroyerDirectionNumber,
shotReturn;
String carrier, battleship, cruiser, submarine, destroyer, carrierDirection,
battleshipDirection,
cruiserDirection, submarineDirection, destroyerDirection, shot;
setGridDimensions(10, 10);
Scanner scan = new Scanner(System.in);
for (int i = 0; i <= 1; i++) {
if (playerTurn) {
System.out.println("PLAYER 1 TURN");
16. displayGrid(true);
System.out.println("Place your Carrier (length 5): ");
carrier = scan.nextLine();
String carrierArray[] = carrier.split(",");
x = Integer.valueOf(carrierArray[0]);
y = Integer.valueOf(carrierArray[1]);
System.out.println("Choose direction (d/r): ");
carrierDirection = scan.nextLine();
if (carrierDirection.equals("d")) {
carrierDirection = "0";
} else {
carrierDirection = "1";
}
carrierDirectionNumber = Integer.valueOf(carrierDirection);
placeShip(x, y, carrierLength, carrierDirectionNumber);
displayGrid(true);
System.out.println("Place your Battleship (length 4): ");
battleship = scan.nextLine();
String battleshipArray[] = battleship.split(",");
x = Integer.valueOf(battleshipArray[0]);
y = Integer.valueOf(battleshipArray[1]);
System.out.println("Choose direction (d/r): ");
battleshipDirection = scan.nextLine();
if (battleshipDirection.equals("d")) {
battleshipDirection = "0";
} else {
battleshipDirection = "1";
}
battleshipDirectionNumber = Integer.valueOf(battleshipDirection);
placeShip(x, y, battleshipLength, battleshipDirectionNumber);
displayGrid(true);
System.out.println("Place your Cruiser (length 3): ");
cruiser = scan.nextLine();
String cruiserArray[] = cruiser.split(",");
x = Integer.valueOf(cruiserArray[0]);
y = Integer.valueOf(cruiserArray[1]);
17. System.out.println("Choose direction (d/r): ");
cruiserDirection = scan.nextLine();
if (cruiserDirection.equals("d")) {
cruiserDirection = "0";
} else {
cruiserDirection = "1";
}
cruiserDirectionNumber = Integer.valueOf(cruiserDirection);
placeShip(x, y, cruiserLength, cruiserDirectionNumber);
displayGrid(true);
System.out.println("Place your Submarine (length 3): ");
submarine = scan.nextLine();
String submarineArray[] = submarine.split(",");
x = Integer.valueOf(submarineArray[0]);
y = Integer.valueOf(submarineArray[1]);
System.out.println("Choose direction (d/r): ");
submarineDirection = scan.nextLine();
if (submarineDirection.equals("d")) {
submarineDirection = "0";
} else {
submarineDirection = "1";
}
submarineDirectionNumber = Integer.valueOf(submarineDirection);
placeShip(x, y, submarineLength, submarineDirectionNumber);
displayGrid(true);
System.out.println("Place your Destroyer (length 2): ");
destroyer = scan.nextLine();
String destroyerArray[] = destroyer.split(",");
x = Integer.valueOf(destroyerArray[0]);
y = Integer.valueOf(destroyerArray[1]);
System.out.println("Choose direction (d/r): ");
destroyerDirection = scan.nextLine();
if (destroyerDirection.equals("d")) {
destroyerDirection = "0";
} else {
destroyerDirection = "1";
18. }
destroyerDirectionNumber = Integer.valueOf(destroyerDirection);
placeShip(x, y, destroyerLength, destroyerDirectionNumber);
displayGrid(true);
playerTurn = false;
} else {
System.out.println("PLAYER 2 TURN");
displayGrid(true);
System.out.println("Place your Carrier (length 5): ");
carrier = scan.nextLine();
String carrierArray[] = carrier.split(",");
x = Integer.valueOf(carrierArray[0]);
y = Integer.valueOf(carrierArray[1]);
System.out.println("Choose direction (d/r): ");
carrierDirection = scan.nextLine();
if (carrierDirection.equals("d")) {
carrierDirection = "0";
} else {
carrierDirection = "1";
}
carrierDirectionNumber = Integer.valueOf(carrierDirection);
placeShip(x, y, carrierLength, carrierDirectionNumber);
displayGrid(true);
System.out.println("Place your Battleship (length 4): ");
battleship = scan.nextLine();
String battleshipArray[] = battleship.split(",");
x = Integer.valueOf(battleshipArray[0]);
y = Integer.valueOf(battleshipArray[1]);
System.out.println("Choose direction (d/r): ");
battleshipDirection = scan.nextLine();
if (battleshipDirection.equals("d")) {
battleshipDirection = "0";
} else {
battleshipDirection = "1";
}
battleshipDirectionNumber = Integer.valueOf(battleshipDirection);
19. placeShip(x, y, battleshipLength, battleshipDirectionNumber);
displayGrid(true);
System.out.println("Place your Cruiser (length 3): ");
cruiser = scan.nextLine();
String cruiserArray[] = cruiser.split(",");
x = Integer.valueOf(cruiserArray[0]);
y = Integer.valueOf(cruiserArray[1]);
System.out.println("Choose direction (d/r): ");
cruiserDirection = scan.nextLine();
if (cruiserDirection.equals("d")) {
cruiserDirection = "0";
} else {
cruiserDirection = "1";
}
cruiserDirectionNumber = Integer.valueOf(cruiserDirection);
placeShip(x, y, cruiserLength, cruiserDirectionNumber);
displayGrid(true);
System.out.println("Place your Submarine (length 3): ");
submarine = scan.nextLine();
String submarineArray[] = submarine.split(",");
x = Integer.valueOf(submarineArray[0]);
y = Integer.valueOf(submarineArray[1]);
System.out.println("Choose direction (d/r): ");
submarineDirection = scan.nextLine();
if (submarineDirection.equals("d")) {
submarineDirection = "0";
} else {
submarineDirection = "1";
}
submarineDirectionNumber = Integer.valueOf(submarineDirection);
placeShip(x, y, submarineLength, submarineDirectionNumber);
displayGrid(true);
System.out.println("Place your Destroyer (length 2): ");
destroyer = scan.nextLine();
String destroyerArray[] = destroyer.split(",");
x = Integer.valueOf(destroyerArray[0]);
20. y = Integer.valueOf(destroyerArray[1]);
System.out.println("Choose direction (d/r): ");
destroyerDirection = scan.nextLine();
if (destroyerDirection.equals("d")) {
destroyerDirection = "0";
} else {
destroyerDirection = "1";
}
destroyerDirectionNumber = Integer.valueOf(destroyerDirection);
placeShip(x, y, destroyerLength, destroyerDirectionNumber);
displayGrid(true);
}
}
while (countPlayer1 != 0 || countPlayer2 != 0) {
if (playerTurn) {
System.out.println("PLAYER 1 TURN");
System.out.println("Take a shot: ");
shot = scan.nextLine();
String shotArray[] = shot.split(",");
x = Integer.valueOf(shotArray[0]);
y = Integer.valueOf(shotArray[1]);
shotReturn = shoot(x, y);
if (shotReturn == -1) {
System.out.println("Miss!");
} else if (shotReturn == 0) {
System.out.println("Hit!");
countPlayer2--;
} else {
System.out.println("Ship sank!");
}
playerTurn = false;
} else {
System.out.println("PLAYER 2 TURN");
System.out.println("Take a shot: ");
shot = scan.nextLine();
String shotArray[] = shot.split(",");
21. x = Integer.valueOf(shotArray[0]);
y = Integer.valueOf(shotArray[1]);
shotReturn = shoot(x, y);
if (shotReturn == -1) {
System.out.println("Miss!");
} else if (shotReturn == 0) {
System.out.println("Hit!");
countPlayer1--;
} else {
System.out.println("Ship sank!");
}
playerTurn = true;
}
}
}
}