Skip to content

Instantly share code, notes, and snippets.

@pavelmeerkat
Last active March 14, 2021 15:00
Show Gist options
  • Save pavelmeerkat/c4c9c752adda68d5a21686f7488af261 to your computer and use it in GitHub Desktop.
Save pavelmeerkat/c4c9c752adda68d5a21686f7488af261 to your computer and use it in GitHub Desktop.
Simple implementation of tic-tac-toe
package tictactoe;
import java.util.Scanner;
public class Main {
private static final int SELL_NUMBERS = 3;
private static final int X_SUM = 264;
private static final int O_SUM = 237;
private static final String X_WINS = "X wins";
private static final String O_WINS = "O wins";
private static final String DRAW = "Draw";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[][] arr = new char[SELL_NUMBERS][SELL_NUMBERS];
for (int i = 0; i < SELL_NUMBERS; i++) {
for (int j = 0; j < SELL_NUMBERS; j++) {
arr[i][j] = '_';
}
}
printCells(arr);
int characterChanger = 1;
while (scanner.hasNext()) {
String verticalCoordinate = scanner.next();
String horizontalCoordinate = scanner.next();
System.out.println("Enter the coordinates: " + verticalCoordinate + " " + horizontalCoordinate);
try {
int i = Integer.parseInt(verticalCoordinate);
int j = Integer.parseInt(horizontalCoordinate);
if (i <= SELL_NUMBERS && j <= SELL_NUMBERS) {
if (arr[i - 1][j - 1] != '_') {
System.out.println("This cell is occupied! Choose another one!");
} else if (characterChanger == 1) {
arr[i - 1][j - 1] = 'X';
characterChanger = 2;
printCells(arr);
} else {
arr[i - 1][j - 1] = 'O';
characterChanger = 1;
printCells(arr);
}
} else {
System.out.println("Coordinates should be from 1 to 3!");
}
} catch (NumberFormatException exception) {
System.out.println("You should enter numbers!");
}
switch (gameAnalyzer(arr)) {
case X_WINS:
System.out.println(X_WINS);
return;
case O_WINS:
System.out.println(O_WINS);
return;
case DRAW:
System.out.println(DRAW);
return;
}
}
}
private static void printCells(char[][] arr) {
System.out.println("---------");
for (char[] elements : arr) {
System.out.print("| ");
for (char elem : elements) {
System.out.print(elem + " ");
}
System.out.println("|");
}
System.out.println("---------");
}
private static String gameAnalyzer(char[][] arr) {
int numberOfX = 0;
int numberOfO = 0;
int primaryDiagonalSum = 0;
int secondaryDiagonalSum = 0;
boolean xWin = false;
boolean oWin = false;
for (int i = 0; i < SELL_NUMBERS; i++) {
int horizontalSum = 0;
int verticalSum = 0;
for (int j = 0; j < SELL_NUMBERS; j++) {
if (arr[i][j] == 'X') numberOfX++;
if (arr[i][j] == 'O') numberOfO++;
if (i == j) primaryDiagonalSum += arr[i][j];
if (i + j == SELL_NUMBERS - 1) secondaryDiagonalSum += arr[i][j];
horizontalSum += arr[i][j];
verticalSum += arr[j][i];
}
if (horizontalSum == X_SUM || verticalSum == X_SUM || primaryDiagonalSum == X_SUM || secondaryDiagonalSum == X_SUM) {
xWin = true;
} else if (horizontalSum == O_SUM || verticalSum == O_SUM || primaryDiagonalSum == O_SUM || secondaryDiagonalSum == O_SUM) {
oWin = true;
}
}
if (xWin) {
return X_WINS;
} else if (oWin) {
return O_WINS;
} else if (numberOfX + numberOfO == 9) {
return DRAW;
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment