-
Notifications
You must be signed in to change notification settings - Fork 0
/
1275. Find Winner on a Tic Tac Toe Game
57 lines (43 loc) · 1.39 KB
/
1275. Find Winner on a Tic Tac Toe Game
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
class Solution {
public:
string tictactoe(vector<vector<int>>& moves) {
vector<vector<char>> grid(3, vector<char> (3));
char val = 'x';
for(auto a : moves)
{
grid[a[0]][a[1]] = val;
if(val == 'x')
val = 'o';
else
val = 'x';
}
//check for row and column
for(int i = 0; i<3; i++)
{
//row check
if(grid[i][0] =='x' && grid[i][1] =='x' && grid[i][2] == 'x')
return "A";
if(grid[i][0] =='o' && grid[i][1] =='o' && grid[i][2] == 'o')
return "B";
//column check
if(grid[0][i] =='x' && grid[1][i] =='x' && grid[2][i] == 'x')
return "A";
if(grid[0][i] =='o' && grid[1][i] =='o' && grid[2][i] == 'o')
return "B";
}
//diagonal check
if(grid[0][0]=='x' && grid[1][1]=='x' && grid[2][2]=='x')
return "A";
if(grid[0][2]=='x' && grid[1][1]=='x' && grid[2][0]=='x')
return "A";
if(grid[0][0]=='o' && grid[1][1]=='o' && grid[2][2]=='o')
return "B";
if(grid[0][2]=='o' && grid[1][1]=='o' && grid[2][0]=='o')
return "B";
if(moves.size() < 9)
{
return "Pending";
}
return "Draw";
}
};