-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathValidTicTacToeState.java
More file actions
139 lines (103 loc) · 4.74 KB
/
ValidTicTacToeState.java
File metadata and controls
139 lines (103 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
,dW"Yvd `7MMpMMMb.pMMMb. M"""MMV ,p6"bo ,pW"Wq.`7MMpMMMb.pMMMb. ,6"Yb.`7MM `7MM
,W' MM MM MM MM ' AMV 6M' OO 6W' `Wb MM MM MM 8) MM MM MM
8M MM MM MM MM AMV 8M 8M M8 MM MM MM ,pm9MM MM MM
YA. MM MM MM MM AMV , ,, YM. , YA. ,A9 MM MM MM ,, 8M MM MM MM
`MbmdMM .JMML JMML JMML.AMMmmmM db YMbmd' `Ybmd9'.JMML JMML JMML.db `Moo9^Yo.`Mbod"YML.
MM
.JMML.
* This code snippet is copyright at qmz.com.au
* - qmz.com.au is a top-ranking IT consulting firm that specialised in
* - Mobile Development, Web Development, Machine Learning, and Cloud Computing.
* - We provide individual and company training for professional development.
* - Find out more information in our official site:
* - https://qmz.com.au
*/
// A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
// The board is a 3 x 3 array, and consists of characters " ", "X", and "O". The " " character represents an empty square.
// Here are the rules of Tic-Tac-Toe:
// Players take turns placing characters into empty squares (" ").
// The first player always places "X" characters, while the second player always places "O" characters.
// "X" and "O" characters are always placed into empty squares, never filled ones.
// The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
// The game also ends if all squares are non-empty.
// No more moves can be played if the game is over.
// Example 1:
// Input: board = ["O ", " ", " "]
// Output: false
// Explanation: The first player always plays "X".
// Example 2:
// Input: board = ["XOX", " X ", " "]
// Output: false
// Explanation: Players take turns making moves.
// Example 3:
// Input: board = ["XXX", " ", "OOO"]
// Output: false
// Example 4:
// Input: board = ["XOX", "O O", "XOX"]
// Output: true
// Note:
// board is a length-3 array of strings, where each string board[i] has length 3.
// Each board[i][j] is a character in the set {" ", "X", "O"}.
class Solution {
private char[][] gameBoard = new char[3][3];
public boolean validTicTacToe(String[] board) {
unpackBoard(board);
return gameBoardValidation();
}
private void unpackBoard(String[] board){
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
this.gameBoard[i][j] = board[i].charAt(j);
}
}
}
private boolean gameBoardValidation(){
int numX, numO;
numX = count('X');
numO = count('O');
//X goes first
if(numO>numX) return false;
//players take turns
if(numX>numO+1) return false;
//both players can't win
if(winner('X') && winner('O')) return false;
//game ends when one player wins
if(winner('X') && numX==numO) return false;
//game ends when one player wins
if(winner('O') && numX>numO) return false;
return true;
}
private int count(char player){
int num = 0;
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
if(gameBoard[i][j] == player) num++;
}
}
return num;
}
private boolean winner(char player){
if(validateRows(player)) return true;
if(validateColumns(player)) return true;
if(validateDiagonal(player)) return true;
return false;
}
private boolean validateRows(char player){
for(int i=0; i<3; i++){
if(gameBoard[i][0] == player && gameBoard[i][1] == player && gameBoard[i][2] == player) return true;
}
return false;
}
private boolean validateColumns(char player){
for(int i=0; i<3; i++){
if(gameBoard[0][i] == player && gameBoard[1][i] == player && gameBoard[2][i] == player) return true;
}
return false;
}
private boolean validateDiagonal(char player){
if(gameBoard[0][0] == player && gameBoard[1][1] == player && gameBoard[2][2] == player) return true;
if(gameBoard[0][2] == player && gameBoard[1][1] == player && gameBoard[2][0] == player) return true;
return false;
}
}