TTT
unknown
java
a year ago
6.1 kB
10
Indexable
import javax.swing.plaf.IconUIResource;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char[][] gameField = new char[3][3];
System.out.println("---------");
for (int i = 0; i < 3; i++) {
gameField[i][0] = '_';
gameField[i][1] = '_';
gameField[i][2] = '_';
System.out.println(String.format("| %c %c %c |", gameField[i][0], gameField[i][1], gameField[i][2]));
}
System.out.println("---------");
int countX = 0;
int countO = 0;
boolean notFinished = true;
int count = 2;
while (notFinished) {
boolean xTurn = count % 2 == 0;
if (xTurn) {
String step = s.nextLine();
String[] userCoordinates = step.split(" ");
if (userCoordinates.length != 2) {
System.out.println("You should enter numbers!");
continue;
}
int stepX = 0;
int stepY = 0;
try {
stepX = Integer.parseInt(userCoordinates[0]);
stepY = Integer.parseInt(userCoordinates[1]);
} catch (NumberFormatException e) {
System.out.println("You should enter numbers!");
continue;
}
if (stepX < 1 || stepX > 3 || stepY < 1 || stepY > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
if (gameField[stepX - 1][stepY - 1] == ' ' || gameField[stepX - 1][stepY - 1] == '_') {
gameField[stepX - 1][stepY - 1] = 'X';
countX++;
} else {
System.out.println("This cell is occupied! Choose another one!");
continue;
}
} else if (!xTurn) {
String step = s.nextLine();
String[] userCoordinates = step.split(" ");
if (userCoordinates.length != 2) {
System.out.println("You should enter numbers!");
continue;
}
int stepX = 0;
int stepY = 0;
try {
stepX = Integer.parseInt(userCoordinates[0]);
stepY = Integer.parseInt(userCoordinates[1]);
} catch (NumberFormatException e) {
System.out.println("You should enter numbers!");
continue;
}
if (stepX < 1 || stepX > 3 || stepY < 1 || stepY > 3) {
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
if (gameField[stepX - 1][stepY - 1] == ' ' || gameField[stepX - 1][stepY - 1] == '_') {
gameField[stepX - 1][stepY - 1] = 'O';
countO++;
} else {
System.out.println("This cell is occupied! Choose another one!");
continue;
}
}
++count;
System.out.println("---------");
for (int i = 0; i < 3; i++) {
System.out.println(String.format("| %c %c %c |", gameField[i][0], gameField[i][1], gameField[i][2]));
}
System.out.println("---------");
boolean spase = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j ++) {
if (gameField[i][j] == '_') {
spase = true;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j ++) {
if (gameField[i][j] == '_') {
spase = true;
}
}
}
boolean isXLinesH = false;
boolean isOLinesH = false;
for (int i = 0; i < 3; i++) {
if (gameField[i][0] == 'X' && gameField[i][1] == 'X' && gameField[i][2] == 'X') {
isXLinesH = true;
}
if (gameField[i][0] == 'O' && gameField[i][1] == 'O' && gameField[i][2] == 'O') {
isOLinesH = true;
}
}
boolean isXLinesV = false;
boolean isOLinesV = false;
for (int i = 0; i < 3; i++) {
if (gameField[0][i] == 'X' && gameField[1][i] == 'X' && gameField[2][i] == 'X') {
isXLinesV = true;
}
if (gameField[0][i] == 'O' && gameField[1][i] == 'O' && gameField[2][i] == 'O') {
isOLinesV = true;
}
}
boolean isXDiagonal = (gameField[0][0] == 'X' && gameField[1][1] == 'X' && gameField[2][2] == 'X') || (gameField[0][2] == 'X' && gameField[1][1] == 'X' && gameField[2][0] == 'X');
boolean isODiagonal = (gameField[0][0] == 'O' && gameField[1][1] == 'O' && gameField[2][2] == 'O') || (gameField[0][2] == 'O' && gameField[1][1] == 'O' && gameField[2][0] == 'O');
boolean xWins = isXLinesH || isXLinesV || isXDiagonal;
boolean oWins = isOLinesH || isOLinesV || isODiagonal;
if (xWins & oWins || countX - countO > 1 || countO - countX > 1) {
System.out.println("Impossible");
break;
} else if (xWins) {
System.out.println("X wins");
break;
} else if (oWins) {
System.out.println("O wins");
break;
} else if (!spase) {
System.out.println("Draw");
break;
}
}
}
}Editor is loading...
Leave a Comment