-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_winner_on_a_tic_tac_toe_game.cpp
78 lines (64 loc) · 2.22 KB
/
find_winner_on_a_tic_tac_toe_game.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<vector>
#include<strings.h>
using namespace std;
class Solution {
public:
void printboard(int board[3][3]){
cout << endl;
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j)
cout << board[i][j] << "\t";
cout << endl;
}
}
bool isLine(int board[3][3]){
bool result = false;
if( (board[0][0] != 0 && board[0][0] == board[0][1] && board[0][1] == board[0][2]) ||
(board[0][0] != 0 && board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][0] != 0 && board[0][0] == board[1][0] && board[1][0] == board[2][0]) ||
(board[0][1] != 0 && board[0][1] == board[1][1] && board[1][1] == board[2][1]) ||
(board[0][2] != 0 && board[0][2] == board[1][1] && board[1][1] == board[2][0]) ||
(board[0][2] != 0 && board[0][2] == board[1][2] && board[1][2] == board[2][2]) ||
(board[1][0] != 0 && board[1][0] == board[1][1] && board[1][1] == board[1][2]) ||
(board[2][0] != 0 && board[2][0] == board[2][1] && board[2][1] == board[2][2]) )
result = true;
return result;
}
string tictactoe(vector<vector<int>>& moves) {
string result = "Pending", marker = "B";
int counter = 0;
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
if(moves.size() > 4){
for(vector<int> pos : moves) {
marker = (marker == "B") ? "A" : "B";
board[pos[0]][pos[1]] = marker[0];
// printboard(board);
result = (counter < 9) ? (isLine(board) ? marker : "Pending") : "Draw";
++counter;
}
}
if(counter == 9)
result = isLine(board) ? marker : "Draw";
return result;
}
};
int main(int args_c, char* args_v[]){
vector<vector<int>> moves = {
// {0, 0}, {1, 1}
// {0, 0}, {2, 0}, {1, 1}, {2, 1}, {2, 2}
// {0, 2}, {2, 0}, {2, 1}, {0, 1}, {1, 2}
// {0, 0}, {1, 1}, {0, 1}, {0, 2}, {1, 0}, {2, 0}
// {1, 1}, {1, 0}, {0, 0}, {2, 2}, {0, 2}, {2, 0}, {0, 1}
// {1, 1}, {1, 0}, {0, 0}, {2, 2}, {0, 2}, {2, 0}, {2, 1}, {0, 1}
// {0, 0}, {1, 1}, {2, 0}, {1, 0}, {1, 2}, {2, 1}, {0, 1}, {0, 2}, {2, 2}
// {0, 2}, {1, 0}, {2, 2}, {1, 2}, {2, 0}, {0, 0}, {0, 1}, {2, 1}, {1, 1}
};
Solution s;
cout << "Result : " << s.tictactoe(moves) << endl;
return 0;
}