forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindWinnerOnATicTacToeGame.java
34 lines (32 loc) · 1.12 KB
/
FindWinnerOnATicTacToeGame.java
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
public class FindWinnerOnATicTacToeGame {
public String tictactoe(int[][] moves) {
String winner = getWinnerInTicTacToe(moves);
if ("".equals(winner)) return moves.length == 9 ? "Draw" : "Pending";
return winner;
}
private String getWinnerInTicTacToe(int[][] moves) {
final int[] rows = new int[3], columns = new int[3];
int d1 = 0, d2 = 0, k = 1;
for (int[] move : moves) {
rows[move[0]] += k;
columns[move[1]] += k;
if (move[0] == move[1]) d1 += k;
if (move[0] == 2 - move[1]) d2 += k;
k *= -1;
}
String result = winnerCheckInArray(rows);
if (!"".equals(result)) return result;
result = winnerCheckInArray(columns);
if (!"".equals(result)) return result;
if (d1 == 3 || d2 == 3) return "A";
if (d1 == -3 || d2 == -3) return "B";
return "";
}
private String winnerCheckInArray(int[] array) {
for (int element : array) {
if (element == 3) return "A";
if (element == -3) return "B";
}
return "";
}
}