Untitled
unknown
plain_text
2 years ago
2.9 kB
3
Indexable
import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String border = "---------"; int counter = 0; char[][] symbolsArray = new char[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { symbolsArray[i][j] = input.charAt(counter); counter++; } } boolean xWon = false; boolean oWon = false; System.out.println(border); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(symbolsArray[i][j]); System.out.print(" "); } System.out.println("|"); if (counter < 7) { System.out.println(); } } System.out.println(border); boolean cont = true; int x = 0; int y = 0; int[][] combinations = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} }; for (int i = 0; i < combinations.length; i++) { x = 0; y = 0; for(int j = 0; j < 3; j++) { if (input.charAt(combinations[i][j]) == 'X') { x++; continue; } if (input.charAt(combinations[i][j]) == 'O') { y++; } } if (x == 3) { xWon = true; } else if (y == 3) { oWon = true; } } if ((!xWon & !oWon) & !input.contains("")) { System.out.println("Draw"); cont = false; } else if (xWon & oWon) { System.out.println("Impossible"); cont = false; } else if (xWon) { System.out.println("X wins"); cont = false; } else if (oWon) { System.out.println("O wins"); cont = false; } if (cont) { x = 0; y = 0; for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == 'X') { x++; continue; } if (input.charAt(i) == 'O') { y++; } } if ((Math.abs(x - y) > 1) || (x == 3 & y == 3)) { System.out.println("Impossible"); } else if (x + y < 9) { System.out.println("Game not finished"); } else { System.out.println("Draw"); } } } }
Editor is loading...