|
| 1 | +package array; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 29/03/2019 |
| 5 | + * A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this |
| 6 | + * board position during the course of a valid tic-tac-toe game. |
| 7 | + * |
| 8 | + * The board is a 3 x 3 array, and consists of characters " ", "X", and "O". The " " character represents an empty |
| 9 | + * square. |
| 10 | + * |
| 11 | + * Here are the rules of Tic-Tac-Toe: |
| 12 | + * |
| 13 | + * Players take turns placing characters into empty squares (" "). |
| 14 | + * The first player always places "X" characters, while the second player always places "O" characters. |
| 15 | + * "X" and "O" characters are always placed into empty squares, never filled ones. |
| 16 | + * The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal. |
| 17 | + * The game also ends if all squares are non-empty. |
| 18 | + * No more moves can be played if the game is over. |
| 19 | + * Example 1: |
| 20 | + * Input: board = ["O ", " ", " "] |
| 21 | + * Output: false |
| 22 | + * Explanation: The first player always plays "X". |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * Input: board = ["XOX", " X ", " "] |
| 26 | + * Output: false |
| 27 | + * Explanation: Players take turns making moves. |
| 28 | + * |
| 29 | + * Example 3: |
| 30 | + * Input: board = ["XXX", " ", "OOO"] |
| 31 | + * Output: false |
| 32 | + * |
| 33 | + * Example 4: |
| 34 | + * Input: board = ["XOX", "O O", "XOX"] |
| 35 | + * Output: true |
| 36 | + * Note: |
| 37 | + * |
| 38 | + * board is a length-3 array of strings, where each string board[i] has length 3. |
| 39 | + * Each board[i][j] is a character in the set {" ", "X", "O"}. |
| 40 | + * |
| 41 | + * Solution: Do a brute-force check for each row, column and diagonals and keep track of count of 'X' and 'O' |
| 42 | + */ |
| 43 | +public class ValidTicTacToeState { |
| 44 | + /** |
| 45 | + * Main method |
| 46 | + * @param args |
| 47 | + */ |
| 48 | + public static void main(String[] args) { |
| 49 | + String[] board = {"XXX", "XOO", "OO "}; |
| 50 | + System.out.println(new ValidTicTacToeState().validTicTacToe(board)); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean validTicTacToe(String[] board) { |
| 54 | + boolean xWon = hasWon(board, 'X'); |
| 55 | + boolean oWon = hasWon(board, 'O'); |
| 56 | + int xcount = 0, ocount = 0; |
| 57 | + for(int i = 0; i < 3; i ++){ |
| 58 | + for(int j = 0; j < 3; j ++){ |
| 59 | + if(board[i].charAt(j) == 'X'){ |
| 60 | + xcount++; |
| 61 | + }else if(board[i].charAt(j) == 'O'){ |
| 62 | + ocount++; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if(xWon && oWon) return false; |
| 67 | + if(xWon){ |
| 68 | + return ((xcount - 1 == ocount)); |
| 69 | + } else if(oWon){ |
| 70 | + return ((xcount == ocount)); |
| 71 | + } else { |
| 72 | + return (xcount == ocount || xcount - 1 == ocount); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private boolean hasWon(String[] board, char c){ |
| 77 | + boolean diagnol = ((board[0].charAt(0) == c && board[1].charAt(1) == c && board[2].charAt(2) == c) || |
| 78 | + (board[0].charAt(2) == c && board[1].charAt(1) == c && board[2].charAt(0) == c)); |
| 79 | + if(diagnol) return true; |
| 80 | + for(int i = 0; i < 3; i ++){ |
| 81 | + if(board[i].charAt(0) == c && board[i].charAt(1) == c && board[i].charAt(2) == c) return true; |
| 82 | + } |
| 83 | + for(int i = 0; i < 3; i ++){ |
| 84 | + if(board[0].charAt(i) == c && board[1].charAt(i) == c && board[2].charAt(i) == c) return true; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
0 commit comments