Created
July 7, 2020 13:04
-
-
Save olegrewko/e3ede92e9b0264740c4e70758464d944 to your computer and use it in GitHub Desktop.
sb_OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package IgorDolgov.seabattleOOP; | |
import java.util.Scanner; | |
public class Main { | |
static String player1; | |
static String player2; | |
private static int[][] monitor1; | |
private static int[][] monitor2; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Player1, enter your name"); | |
player1 = sc.nextLine(); | |
System.out.println("Player2, enter your name"); | |
player2 = sc.nextLine(); | |
ShipPlace.placeShips(player1, Field.field1, monitor1); | |
ShipPlace.placeShips(player2, Field.field2, monitor2); | |
do { | |
makeTurn(player1, monitor1, Field.field2); | |
makeTurn(player2, monitor2, Field.field1); | |
} while (!isWin()); | |
} | |
public static void makeTurn(String player, int[][] monitor, int[][] field) { | |
while (true) { | |
System.out.println(player + ", make your turn"); | |
System.out.println(" 0 1 2 3 4 5 6 7 8 9"); | |
for (int i = 0; i < monitor.length; i++) { | |
System.out.print(i + " "); | |
for (int j = 0; j < monitor[1].length; j++) { | |
if (monitor[j][i] == 0) { | |
System.out.print("- "); | |
} else if (monitor[j][i] == 1) { | |
System.out.print(". "); | |
} else { | |
System.out.print("X "); | |
} | |
} | |
System.out.println(); | |
} | |
System.out.println("Enter OY coordinate: "); | |
Scanner sc = new Scanner(System.in); | |
int x = sc.nextInt(); | |
System.out.println("Enter OX coordinate: "); | |
int y = sc.nextInt(); | |
if (field[y][x] == 1) { | |
System.out.println("Hit! Make your turn again"); | |
monitor[y][x] = 2; | |
} else { | |
System.out.println("Miss! Your opponents turn"); | |
monitor[x][y] = 1; | |
break; | |
} | |
} | |
} | |
public static boolean isWin() { | |
int counter1 = 0; | |
for (int i = 0; i < monitor1.length; i++) { | |
for (int j = 0; j < monitor1[i].length; j++) { | |
if (monitor1[i][j] == 2) { | |
counter1++; | |
} | |
} | |
} | |
int counter2 = 0; | |
for (int i = 0; i < monitor2.length; i++) { | |
for (int j = 0; j < monitor2[i].length; j++) { | |
if (monitor2[i][j] == 2) { | |
counter2++; | |
} | |
} | |
} | |
if (counter1 >= 10) { | |
System.out.println(player1 + "Win"); | |
return true; | |
} | |
if (counter2 >= 10) { | |
System.out.println(player2 + "Win"); | |
return true; | |
} | |
return false; | |
} | |
} | |
//------------------------------------------------------------------- | |
class ShipPlace { | |
public static void placeShips(String player, int[][] field, int[][] monitor) { | |
int deck = 4; | |
while (deck >= 1) { | |
System.out.println(player + ", please place your " + deck + "-deck ships on the sea"); | |
System.out.println(); | |
Field.drawField(field); | |
System.out.println("Enter OY coordinate: [0-9] "); | |
Scanner sc = new Scanner(System.in); | |
int x = sc.nextInt(); | |
System.out.println("Enter OX coordinate: [0-9]"); | |
int y = sc.nextInt(); | |
System.out.println("Choose direction: "); | |
System.out.println(" 1 Horizontal "); | |
System.out.println(" 2 Vertical "); | |
int direction = sc.nextInt(); | |
if (!isAvailable(x, y, deck, direction, field)) { | |
System.out.println("Wrong coordinates"); | |
continue; | |
} | |
for (int i = 0; i < deck; i++) { | |
if (direction == 1) { | |
field[x][y + i] = 1; | |
} else { | |
field[x + i][y] = 1; | |
} | |
} | |
deck--; | |
} | |
} | |
public static boolean isAvailable(int x, int y, int deck, int rotation, int[][] field) { | |
if (rotation == 1) { | |
if (y + deck > field.length) { | |
return false; | |
} | |
} | |
if (rotation == 2) { | |
if (x + deck > field.length) { | |
return false; | |
} | |
} | |
//проверка на прилипание кораблей | |
while (deck != 0) { | |
for (int i = 0; i < deck; i++) { | |
int xi = 0; | |
int yi = 0; | |
if (rotation == 1) { | |
yi = i; | |
} else { | |
xi = i; | |
} | |
if (x + 1 + xi < field.length && x + 1 + xi >= 0) { | |
if (field[x + 1 + xi][y + yi] != 0) { | |
return false; | |
} | |
} | |
if (x - 1 + xi < field.length && x - 1 + xi >= 0) { | |
if (field[x - 1 + xi][y + yi] != 0) { | |
return false; | |
} | |
} | |
if (y + 1 + yi < field.length && y + 1 + yi >= 0) { | |
if (field[x + xi][y + 1 + yi] != 0) { | |
return false; | |
} | |
} | |
if (y - 1 + yi < field.length && y - 1 + yi >= 0) { | |
if (field[x + xi][y - 1 + yi] != 0) { | |
return false; | |
} | |
} | |
} | |
deck--; | |
} | |
return true; | |
} | |
} | |
//----------------------------------------------------- | |
class Field { | |
public static int[][] field1 = new int[10][10]; | |
public static int[][] field2 = new int[10][10]; | |
public static void drawField(int[][] field) { | |
System.out.println(" 0 1 2 3 4 5 6 7 8 9"); | |
for (int i = 0; i < field.length; i++) { | |
System.out.print(i + " "); | |
for (int j = 0; j < field.length; j++) { | |
if (field[i][j] == 0) { | |
System.out.print("- "); | |
} else { | |
System.out.print("X "); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment