|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1275. Find Winner on a Tic Tac Toe Game |
| 5 | + * |
| 6 | + * Tic-tac-toe is played by two players A and B on a 3 x 3 grid. |
| 7 | + * |
| 8 | + * Here are the rules of Tic-Tac-Toe: |
| 9 | + * Players take turns placing characters into empty squares (" "). |
| 10 | + * The first player A always places "X" characters, while the second player B always places "O" characters. |
| 11 | + * "X" and "O" characters are always placed into empty squares, never on filled ones. |
| 12 | + * The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal. |
| 13 | + * The game also ends if all squares are non-empty. |
| 14 | + * No more moves can be played if the game is over. |
| 15 | + * Given an array moves where each element is another array of size 2 corresponding to the row and column of |
| 16 | + * the grid where they mark their respective character in the order in which A and B play. |
| 17 | + * |
| 18 | + * Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw", |
| 19 | + * if there are still movements to play return "Pending". |
| 20 | + * |
| 21 | + * You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first. |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] |
| 25 | + * Output: "A" |
| 26 | + * Explanation: "A" wins, he always plays first. |
| 27 | + * "X " "X " "X " "X " "X " |
| 28 | + * " " -> " " -> " X " -> " X " -> " X " |
| 29 | + * " " "O " "O " "OO " "OOX" |
| 30 | + * |
| 31 | + * Example 2: |
| 32 | + * Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] |
| 33 | + * Output: "B" |
| 34 | + * Explanation: "B" wins. |
| 35 | + * "X " "X " "XX " "XXO" "XXO" "XXO" |
| 36 | + * " " -> " O " -> " O " -> " O " -> "XO " -> "XO " |
| 37 | + * " " " " " " " " " " "O " |
| 38 | + * |
| 39 | + * Example 3: |
| 40 | + * Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] |
| 41 | + * Output: "Draw" |
| 42 | + * Explanation: The game ends in a draw since there are no moves to make. |
| 43 | + * "XXO" |
| 44 | + * "OOX" |
| 45 | + * "XOX" |
| 46 | + * |
| 47 | + * Example 4: |
| 48 | + * Input: moves = [[0,0],[1,1]] |
| 49 | + * Output: "Pending" |
| 50 | + * Explanation: The game has not finished yet. |
| 51 | + * "X " |
| 52 | + * " O " |
| 53 | + * " " |
| 54 | + * |
| 55 | + * Constraints: |
| 56 | + * 1 <= moves.length <= 9 |
| 57 | + * moves[i].length == 2 |
| 58 | + * 0 <= moves[i][j] <= 2 |
| 59 | + * There are no repeated elements on moves. |
| 60 | + * moves follow the rules of tic tac toe. |
| 61 | + * */ |
| 62 | +public class _1275 { |
| 63 | + public static class Solution1 { |
| 64 | + public String tictactoe(int[][] moves) { |
| 65 | + String[][] board = new String[3][3]; |
| 66 | + for (int i = 0; i < moves.length; i++) { |
| 67 | + if (i % 2 == 0) { |
| 68 | + board[moves[i][0]][moves[i][1]] = "X"; |
| 69 | + } else { |
| 70 | + board[moves[i][0]][moves[i][1]] = "O"; |
| 71 | + } |
| 72 | + if (i > 3) { |
| 73 | + if (!wins(board).equals("")) { |
| 74 | + return wins(board); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return moves.length == 9 ? "Draw" : "Pending"; |
| 79 | + } |
| 80 | + |
| 81 | + private String wins(String[][] board) { |
| 82 | + //check rows |
| 83 | + for (int i = 0; i < 3; i++) { |
| 84 | + if (board[i][0] == null) { |
| 85 | + break; |
| 86 | + } |
| 87 | + String str = board[i][0]; |
| 88 | + if (str.equals(board[i][1]) && str.equals(board[i][2])) { |
| 89 | + return getWinner(str); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + //check columns |
| 94 | + for (int j = 0; j < 3; j++) { |
| 95 | + if (board[0][j] == null) { |
| 96 | + break; |
| 97 | + } |
| 98 | + String str = board[0][j]; |
| 99 | + if (str.equals(board[1][j]) && str.equals(board[2][j])) { |
| 100 | + return getWinner(str); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //check diagonals |
| 105 | + if (board[1][1] == null) { |
| 106 | + return ""; |
| 107 | + } |
| 108 | + String str = board[1][1]; |
| 109 | + if (str.equals(board[0][0]) && str.equals(board[2][2]) || (str.equals(board[0][2]) && str.equals(board[2][0]))) { |
| 110 | + return getWinner(str); |
| 111 | + } |
| 112 | + return ""; |
| 113 | + } |
| 114 | + |
| 115 | + private String getWinner(String str) { |
| 116 | + if (str.equals("X")) { |
| 117 | + return "A"; |
| 118 | + } else { |
| 119 | + return "B"; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments