generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
43 lines (37 loc) · 1.22 KB
/
solution.cpp
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
#include <string>
#include <vector>
class Solution {
public:
std::string tictactoe(std::vector<std::vector<int>>& moves) {
char board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}};
bool aTurn{true};
for (const auto& move : moves) {
board[move[0]][move[1]] = aTurn ? 'A' : 'B';
aTurn = !aTurn;
}
if (board[0][0] != 0) {
if ((board[0][0] == board[0][1] && board[0][1] == board[0][2]) ||
(board[0][0] == board[1][0] && board[1][0] == board[2][0])) {
return board[0][0] == 'A' ? "A" : "B";
}
}
if (board[1][1] != 0) {
if ((board[1][0] == board[1][1] && board[1][1] == board[1][2]) ||
(board[0][1] == board[1][1] && board[1][1] == board[2][1]) ||
(board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0])) {
return board[1][1] == 'A' ? "A" : "B";
}
}
if (board[2][2] != 0) {
if ((board[2][0] == board[2][1] && board[2][1] == board[2][2]) ||
(board[0][2] == board[1][2] && board[1][2] == board[2][2])) {
return board[2][2] == 'A' ? "A" : "B";
}
}
return moves.size() == 9 ? "Draw" : "Pending";
}
};