-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
306 lines (293 loc) · 7.18 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// At the beginning of your code
// #if __cplusplus < 201103L
// #error This file requires C++11 support
// #endif
#include <iostream>
#include <string>
#include <ctime>
// ctime helps to work with time related things s.a get_current_time, compare_time
#include <random>
// random help to generate random numbers
using namespace std;
char board[9] = {
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
' ',
};
void instructions();
void show_board();
void computer_choice();
void player_one_choice();
void player_two_choice();
int choice_count(char symbol);
char check_winner();
void computer_vs_player();
void player_vs_player();
int main()
{
instructions();
int mode;
cout<<"1. Computer VS Player Mode"<<endl;
cout<<"2. Player VS Player Mode"<<endl;
cout<<"Enter mode:";
cin>>mode;
switch (mode)
{
case 1:
computer_vs_player();
break;
case 2:
player_vs_player();
break;
default:
cout<<"Choose corrent mode";
break;
}
return 0;
}
void computer_vs_player()
{
string player_name;
cout << "enter player name:";
cin>>player_name;
//getline(cin, player_name);
while (true)
{
system("cls");
show_board();
if (choice_count('X') == choice_count('O'))
{
cout << player_name << "'s turn:" << endl;
player_one_choice();
}
else
{
cout << "Computer's Turn" << endl;
computer_choice();
}
char winner = check_winner();
if (winner == 'X')
{
system("cls");
show_board();
cout << player_name << " Won";
break;
}
else if (winner == 'O')
{
system("cls");
show_board();
cout << "Computer Won";
break;
}
else if (winner == 'D')
{
system("cls");
show_board();
cout << "Draw Game";
break;
}
}
}
void player_vs_player()
{
string player1_name;
cout << "enter player 1 name:"<<endl;
cin>>player1_name;
//getline(cin, player1_name);
string player2_name;
cout << "enter player 2 name:"<<endl;
cin>>player2_name;
//getline(cin, player2_name);
while (true)
{
system("cls");
show_board();
if (choice_count('X') == choice_count('O'))
{
cout << player1_name << "'s Turn" << endl;
player_one_choice();
//break;
}
else
{
cout << player2_name << "'s Turn" << endl;
player_two_choice();
//break;
}
char winner = check_winner();
if (winner == 'X')
{
system("cls");
show_board();
cout << player1_name << " Won The Game" << endl;
}
else if (winner == 'O')
{
system("cls");
show_board();
cout << player2_name << " Won The Game" << endl;
}
else if (winner == 'D')
{
system("cls");
show_board();
cout << "Draw Game";
break;
}
}
}
void computer_choice()
{
srand(time(0)); // srand:sets the starting points for the random num. seq to make them random wach time ||time:gets the curernt time in sec || there can be anything instead of 0 as they act as constants.
int choice;
do
{
choice = rand() % 10; // return a random number btw 0 to 9
} while (board[choice] != ' ');
board[choice] = 'O'; // as computer enters O
}
void player_one_choice()
{
while (true)
{
// int choice;
cout << "Enter the positon from (1-9):";
int choice;
cin >> choice;
choice--;
if (choice < 0 || choice > 8)
{
cout << "Please enter from position (1-9).";
}
else if (board[choice] != ' ')
{
cout << "please enter another postion.";
}
else
{
board[choice] = 'X';
break; // mistake
}
}
}
void player_two_choice()
{
while (true)
{
int choice;
cout << "Enter the positon from (1-9):";
cin >> choice;
choice--;
if (choice < 0 || choice > 9)
{
cout << "Please enter from position (1-9).";
}
else if (board[choice] != ' ')
{
cout << "please enter another postion.";
}
else
{
board[choice] = 'O';
break;
}
}
}
int choice_count(char symbol)
{
int total = 0;
for (int i = 0; i < 9; i++)
{
if (board[i] == symbol)
{
total = total + 1;
}
}
return total;
}
char check_winner()
{
// checking for horizontal
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 for vertical
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 for diagnols
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 (choice_count('X') + choice_count('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 << " " << board[3] << " | " << board[4] << " | " << board[5] << " " << endl;
cout << "______|_______|_______" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << " " << endl;
cout << " | | " << endl
<< endl;
}
void instructions()
{
cout << "Enter any key to start the game";
getchar();
cout << "\n";
cout << "............." << endl;
cout << " TIC-TAC-TOE" << endl;
cout << "............." << endl;
cout << "PLAYER 1 -> X" << endl;
cout << "PLAYER 2 -> O" << endl;
cout << "COMPUTER -> O" << endl;
cout << " | | " << endl;
cout << " 1 | 2 | 3 " << endl;
cout << "______|_______|_______" << endl;
cout << " | | " << endl;
cout << " 4 | 5 | 6 " << endl;
cout << "______|_______|_______" << endl;
cout << " | | " << endl;
cout << " 7 | 8 | 9 " << endl;
cout << " | | " << endl
<< endl;
}