-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinner.go
57 lines (47 loc) · 1.28 KB
/
Winner.go
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
package main
func tictactoe(moves [][]int) string {
board := make([][]string, 3)
for i := range board {
board[i] = make([]string, 3)
}
for idx, move := range moves {
row, col := move[0], move[1]
if idx % 2 == 0 {
board[row][col] = "X"
} else {
board[row][col] = "O"
}
}
winner := checkWinner(board)
if winner == "X" {
return "A"
} else if winner == "O" {
return "B"
}
if len(moves) < 9 {
return "Pending"
} else {
return "Draw"
}
}
func checkWinner(board [][]string) string {
for i := 0; i < 3; i++ {
if board[i][0] != "" && board[i][0] == board[i][1] && board[i][1] == board[i][2] {
return board[i][0]
}
}
for j := 0; j < 3; j++ {
if board[0][j] != "" && board[0][j] == board[1][j] && board[1][j] == board[2][j] {
return board[0][j]
}
}
if board[0][0] != "" && board[0][0] == board[1][1] && board[1][1] == board[2][2] {
return board[0][0]
}
if board[0][2] != "" && board[0][2] == board[1][1] && board[1][1] == board[2][0] {
return board[0][2]
}
return ""
}
// Runtime:= 2ms Beats 20.31%
// Memory:= 2.28MB Beats 67.19%