-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic_tac_toe.cpp
117 lines (97 loc) · 2.74 KB
/
tic_tac_toe.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <vector>
using namespace std;
vector<int> board(10, 2);
int turn = 1;
int possWin(int player) {
int winningMove = 0;
for (int i = 1; i <= 9; i += 3) {
int product = board[i] * board[i + 1] * board[i + 2];
if (product == player * player * 2) {
if (board[i] == 2) winningMove = i;
else if (board[i + 1] == 2) winningMove = i + 1;
else if (board[i + 2] == 2) winningMove = i + 2;
}
}
for (int i = 1; i <= 3; i++) {
int product = board[i] * board[i + 3] * board[i + 6];
if (product == player * player * 2) {
if (board[i] == 2) winningMove = i;
else if (board[i + 3] == 2) winningMove = i + 3;
else if (board[i + 6] == 2) winningMove = i + 6;
}
}
if ((board[1] * board[5] * board[9] == player * player * 2) && (board[1] == 2 || board[5] == 2 || board[9]==2))
winningMove = 1;
else if ((board[3] * board[5] * board[7] == player * player * 2) && (board[3] == 2 || board[5] == 2 || board[7]==2))
winningMove = 3;
return winningMove;
}
void go(int n) {
if (turn % 2 == 1)
board[n] = 3;
else
board[n] = 5;
turn++;
}
void makeMove() {
int move = 0;
if (board[5] == 2)
move = 5;
else {
for (int i = 2; i <= 8; i += 2) {
if (board[i] == 2) {
move = i;
break;
}
}
}
if (move == 0) {
for (int i = 1; i <= 9; i++) {
if (board[i] == 2) {
move = i;
break;
}
}
}
go(move);
}
void printBoard() {
for (int i = 1; i <= 9; i++) {
if (i % 3 != 0)
cout << board[i] << " | ";
else
cout << board[i] << endl;
}
}
int main() {
while (turn <= 9) {
cout << "Turn: " << turn << endl;
printBoard();
if (turn % 2 == 1){
cout << "Player X's turn." << endl;
cout<<"Choose your desired position(1-9) : ";
int hmove;
cin>>hmove;
go(hmove);
}
else{
cout << "Player O's turn." << endl;
makeMove();
}
int winner = possWin((turn % 2 == 1) ? 3 : 5);
if (winner != 0) {
cout << endl;
printBoard();
if (turn % 2 == 1)
cout << "Player X wins!" << endl;
else
cout << "Player O wins!" << endl;
break;
}
}
if (turn > 9) {
cout << "The game ended in a draw!" << endl;
}
return 0;
}