-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindWinnerOnATicTacToeGame.java
44 lines (38 loc) · 1.19 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
35
36
37
38
39
40
41
42
43
44
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/
public class FindWinnerOnATicTacToeGame {
private final int[][] input;
public FindWinnerOnATicTacToeGame(int[][] input) {
this.input = input;
}
public String solution() {
String[][] board = new String[3][3];
boolean isX = true;
for (int[] move : input) {
board[move[0]][move[1]] = isX ? "A" : "B";
isX = !isX;
}
for (int i = 0; i < 3; i++) {
String a = board[i][0];
if (isValid(a, board[i][1], board[i][2])) {
return a;
}
a = board[0][i];
if (isValid(a, board[1][i], board[2][i])) {
return a;
}
}
String a = board[0][0];
if (isValid(a, board[1][1], board[2][2])) {
return a;
}
a = board[0][2];
if (isValid(a, board[1][1], board[2][0])) {
return a;
}
return input.length == 9 ? "Draw" : "Pending";
}
private boolean isValid(String a, String b, String c) {
return a != null && a == b && a == c;
}
}