SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
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++) {
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) {
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) {
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) {
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")) {
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);
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): ");
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();
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);
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 {
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];
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) {
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++) {
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 {
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");
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]);
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";
}
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);
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]);
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(",");
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;
}
}
}
}

Contenu connexe

Similaire à import java.util.Scanner;public class Main {    public static in.pdf

The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 
MineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdfMineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdf
aniyathikitchen
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
aravlitraders2012
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
actocomputer
 

Similaire à import java.util.Scanner;public class Main {    public static in.pdf (20)

The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Ocr code
Ocr codeOcr code
Ocr code
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
 
MineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdfMineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdf
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
java slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdfjava slip for bachelors of business administration.pdf
java slip for bachelors of business administration.pdf
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautiful
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
week-21x
week-21xweek-21x
week-21x
 

Plus de anwarsadath111

There are several ways in which the gene regulation in eukaryotes di.pdf
There are several ways in which the gene regulation in eukaryotes di.pdfThere are several ways in which the gene regulation in eukaryotes di.pdf
There are several ways in which the gene regulation in eukaryotes di.pdf
anwarsadath111
 
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdf
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdfQues-1) What is the most likely cause of a throat infection in a 6-y.pdf
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdf
anwarsadath111
 
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdfPart1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
anwarsadath111
 
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdfPerformance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
anwarsadath111
 
package employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdfpackage employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdf
anwarsadath111
 

Plus de anwarsadath111 (20)

(1) c. Two-tailed test(2)d. Ho Men and women are the same in .pdf
(1) c. Two-tailed test(2)d. Ho Men and women are the same in .pdf(1) c. Two-tailed test(2)d. Ho Men and women are the same in .pdf
(1) c. Two-tailed test(2)d. Ho Men and women are the same in .pdf
 
We use a base (in this case sodium bicarbonate) during the separatio.pdf
  We use a base (in this case sodium bicarbonate) during the separatio.pdf  We use a base (in this case sodium bicarbonate) during the separatio.pdf
We use a base (in this case sodium bicarbonate) during the separatio.pdf
 
While computing the dilluted earnings per share Dividend paid on Pre.pdf
While computing the dilluted earnings per share Dividend paid on Pre.pdfWhile computing the dilluted earnings per share Dividend paid on Pre.pdf
While computing the dilluted earnings per share Dividend paid on Pre.pdf
 
There are several ways in which the gene regulation in eukaryotes di.pdf
There are several ways in which the gene regulation in eukaryotes di.pdfThere are several ways in which the gene regulation in eukaryotes di.pdf
There are several ways in which the gene regulation in eukaryotes di.pdf
 
The water must be boiled to remove traces of dissolved carbon dioxid.pdf
The water must be boiled to remove traces of dissolved carbon dioxid.pdfThe water must be boiled to remove traces of dissolved carbon dioxid.pdf
The water must be boiled to remove traces of dissolved carbon dioxid.pdf
 
adsorb onto silica gel Solu.pdf
  adsorb onto silica gel                                      Solu.pdf  adsorb onto silica gel                                      Solu.pdf
adsorb onto silica gel Solu.pdf
 
Solution Plants show different types of symptoms when there is an.pdf
Solution Plants show different types of symptoms when there is an.pdfSolution Plants show different types of symptoms when there is an.pdf
Solution Plants show different types of symptoms when there is an.pdf
 
SF4SolutionSF4.pdf
SF4SolutionSF4.pdfSF4SolutionSF4.pdf
SF4SolutionSF4.pdf
 
Quick ratio = (Cash + accounts receivable)Accounts payableFor 200.pdf
Quick ratio = (Cash + accounts receivable)Accounts payableFor 200.pdfQuick ratio = (Cash + accounts receivable)Accounts payableFor 200.pdf
Quick ratio = (Cash + accounts receivable)Accounts payableFor 200.pdf
 
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdf
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdfQues-1) What is the most likely cause of a throat infection in a 6-y.pdf
Ques-1) What is the most likely cause of a throat infection in a 6-y.pdf
 
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdfPart1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
Part1. Option 3rd; Somatic cells such as liver cells cannot undergo .pdf
 
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdfPerformance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
Performance RatiosMarket Value Added (MVA)MVA=(Company’s market.pdf
 
package employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdfpackage employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdf
 
P = 212option ASolutionP = 212option A.pdf
P = 212option ASolutionP = 212option A.pdfP = 212option ASolutionP = 212option A.pdf
P = 212option ASolutionP = 212option A.pdf
 
OSI MODELIt has 7 layersINTERNET MODELIt has5 LayersDOD MO.pdf
OSI MODELIt has 7 layersINTERNET MODELIt has5 LayersDOD MO.pdfOSI MODELIt has 7 layersINTERNET MODELIt has5 LayersDOD MO.pdf
OSI MODELIt has 7 layersINTERNET MODELIt has5 LayersDOD MO.pdf
 
Option 2 namely TPP contains a thiazolium ring is correct. This is b.pdf
Option 2 namely TPP contains a thiazolium ring is correct. This is b.pdfOption 2 namely TPP contains a thiazolium ring is correct. This is b.pdf
Option 2 namely TPP contains a thiazolium ring is correct. This is b.pdf
 
NH4NO3 = N2O + 2 H2OMoles of NH4NO3 = 12 x moles of H2O= 12 x .pdf
NH4NO3 = N2O + 2 H2OMoles of NH4NO3 = 12 x moles of H2O= 12 x .pdfNH4NO3 = N2O + 2 H2OMoles of NH4NO3 = 12 x moles of H2O= 12 x .pdf
NH4NO3 = N2O + 2 H2OMoles of NH4NO3 = 12 x moles of H2O= 12 x .pdf
 
No. of shares outstanding = Total assets x weight of equity price .pdf
No. of shares outstanding = Total assets x weight of equity  price .pdfNo. of shares outstanding = Total assets x weight of equity  price .pdf
No. of shares outstanding = Total assets x weight of equity price .pdf
 
molarity = moles volume = 2.0 x 10-3 mol (18.51000) L = 0.11 M.pdf
molarity = moles  volume = 2.0 x 10-3 mol  (18.51000) L = 0.11 M.pdfmolarity = moles  volume = 2.0 x 10-3 mol  (18.51000) L = 0.11 M.pdf
molarity = moles volume = 2.0 x 10-3 mol (18.51000) L = 0.11 M.pdf
 
hi Q is nearly done,just give me dia of shaftSolutionhi Q is n.pdf
hi Q is nearly done,just give me dia of shaftSolutionhi Q is n.pdfhi Q is nearly done,just give me dia of shaftSolutionhi Q is n.pdf
hi Q is nearly done,just give me dia of shaftSolutionhi Q is n.pdf
 

Dernier

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Dernier (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

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; } } } }