-
Notifications
You must be signed in to change notification settings - Fork 127
/
tic-tac-toe.cpp
204 lines (192 loc) · 5.6 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
char board[9] = {' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' '};
void show_board();
void get_x_player_choice();
void get_o_player_choice();
void get_computer_choice();
int count_board(char symbol);
char check_winner();
void computer_vs_player();
void player_vs_player();
int main()
{
int mode;
cout << "1. Computer VS Player." << endl;
cout << "2. Player VS Player." << endl;
cout << "Select Game Mode: 1/2?:" << endl;
cin >> mode;
switch(mode) {
case 1 :
computer_vs_player();
break;
case 2:
player_vs_player();
break;
default :
cout << "Please Select Valid Game Mode." << endl;
break;
}
return 0;
}
void computer_vs_player() {
string player_name;
cout << "Enter Your Name: ";
cin >> player_name;
while(true) {
system("cls");
show_board();
if(count_board('X') == count_board('O')) {
cout << player_name << "'s Turn." << endl;
get_x_player_choice();
}
else{
get_computer_choice();
}
char winner = check_winner();
if(winner == 'X') {
system("cls");
show_board();
cout << player_name << " Won The Game." << endl;
break;
}
else if(winner == 'O') {
system("cls");
show_board();
cout << "Computer Won The Game." << endl;
break;
}
else if(winner == 'D') {
cout << "Game is Draw." << endl;
break;
}
}
}
void get_computer_choice() {
srand(time(0));
int choice;
do{
choice = rand()%10;
}while(board[choice] != ' ');
board[choice] = 'O';
}
void get_x_player_choice() {
while(true) {
cout << "Select Your Position (1 – 9): " ;
int choice;
cin >> choice;
choice--;
if(choice < 0 || choice > 8) {
cout << "Please Select Your Choice From (1 – 9)." << endl;
}
else if(board[choice] != ' ') {
cout << "Please Select An Empty Position." << endl;
}
else {
board[choice] = 'X';
break;
}
}
}
void get_o_player_choice() {
while(true) {
cout << "Select Your Position (1 – 9): " ;
int choice;
cin >> choice;
choice--;
if(choice < 0 || choice > 8) {
cout << "Please Select Your Choice From (1 – 9)." << endl;
}
else if(board[choice] != ' ') {
cout << "Please Select An Empty Position." << endl;
}
else {
board[choice] = 'O';
break;
}
}
}
void player_vs_player() {
string x_player_name , o_player_name;
cout << "Enter X Player Name: " ;
cin >> x_player_name;
cout << "Enter O Player Name: " ;
cin >> o_player_name;
while(true) {
system("cls");
show_board();
if(count_board('X') == count_board('O')) {
cout << x_player_name << "'s Turn." << endl;
get_x_player_choice();
}
else{
cout << o_player_name << "'s Turn." << endl;
get_o_player_choice();
}
char winner = check_winner();
if(winner == 'X') {
system("cls");
show_board();
cout << x_player_name << " Won The Game." << endl;
break;
}
else if(winner == 'O') {
system("cls");
show_board();
cout << o_player_name << " Won The Game." << endl;
break;
}
else if(winner == 'D') {
cout << "Game is Draw." << endl;
break;
}
}
}
int count_board(char symbol) {
int total = 0;
for(int i=0; i<9; i++) {
if(board[i] == symbol)
total += 1;
}
return total;
}
char check_winner() {
// checking winner in horizontal/row
if(board[0] == board[1] && board[1] == board[2] && board[0] != ' ')
return board[0];
if(board[3] == board[4] && board[4] == board[5] && board[3] != ' ')
return board[3];
if(board[6] == board[7] && board[7] == board[8] && board[6] != ' ')
return board[6];
// checking winner in vertical/column
if(board[0] == board[3] && board[3] == board[6] && board[0] != ' ')
return board[0];
if(board[1] == board[4] && board[4] == board[7] && board[1] != ' ')
return board[1];
if(board[2] == board[5] && board[5] == board[8] && board[2] != ' ')
return board[2];
// checking winner in diagonal
if(board[0] == board[4] && board[4] == board[8] && board[0] != ' ')
return board[0];
if(board[2] == board[4] && board[4] == board[6] && board[2] != ' ')
return board[2];
if(count_board('X') + count_board('O') < 9)
return 'C';
else
return 'D';
}
void show_board() {
cout << " " << " | " << " | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << " " << " | " << " | " << endl;
cout << "--------------------------" << endl;
cout << " " << " | " << " | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << " " << " | " << " | " << endl;
cout << "--------------------------" << endl;
cout << " " << " | " << " | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " " << " | " << " | " << endl;
}