-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectfour.cpp
More file actions
385 lines (280 loc) · 8.22 KB
/
Connectfour.cpp
File metadata and controls
385 lines (280 loc) · 8.22 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <iostream>
#include <windows.h>
using namespace std;
class Playerinfo{
public:
string name;
char character[20];
//char name[15];
//char character[1];
void set_default_character(char default_character[20]);
void set_default_name(string default_name);
};
void Playerinfo::set_default_character(char default_character[20]){
character[0] = default_character[0];
}
void Playerinfo::set_default_name(string default_name){
name = default_name;
}
char a[20]={'X'};
char b[20]={'O'};
Playerinfo Player_1; //Player1 info
Playerinfo Player_2; //Player2 info
int Choose; //input Choose
char board[7][8]; //board size
int turn = 1; //Player's turn
int DropChoice;
int check_condition;
int win = 0, full = 0; //Conditions
int trueWidth = 6, trueLength = 7; // Connect Four size
void Win(char player);
void Full(char player);
void Check_Condition();
void Update_Board(char player);
void Display_Board();
void Default_Board();
void Choose1();
void Choose2();
void Choose3();
int Play_Again();
void Main_menu();
int main()
{
Player_1.set_default_character(a); //default Charater of Player1
Player_2.set_default_character(b); //default Charater of Player2
Player_1.set_default_name("Player1"); //default Name of Player1
Player_2.set_default_name("Player2"); //default Name of Player2
Main_menu();
return 0;
}
/*void GoBack(string Back){ //Go back to before main menu
if(Back == "q"){
Main_menu();
}
}*/
void Choose1(){ //Change Playre Names
cout << "\n";
cout << "Please Enter Player1's name : " ;
cin >> Player_1.name;
//GoBack(Player_1.name);
cout << "Please Enter Player2's name : " ;
cin >> Player_2.name;
//GoBack(Player_2.name);
if (Player_1.name == Player_2.name){
cout << "Wrong Input! The Names cannot be same! Please try again!" << endl;
Choose1();
}
Main_menu();
}
void Choose2(){ //Change Playre Characters
char a[20], b[20]; // temp character
cout << "\n";
cout << "Please Enter Player1's Charater : " ;
cin >> a ;
//GoBack(Player_1.character);
cout << "Please Enter Player2's Charater : " ;
cin >> b ;
//GoBack(Player_2.character);
if (a[0] == b[0]){
cout << "Wrong Input! The Charaters cannot be same! Please try again!" << endl;
Choose2();
}
if ((strlen(a) >1 ) || (strlen(b) >1 )){
cout << "Wrong Input! There should one charater of each Player! Please try again!" << endl;
Choose2();
}
Player_1.set_default_character(a);
Player_2.set_default_character(b);
Main_menu();
}
void Choose3(){ //Game start
if (turn == 1){
Display_Board();
}
if (win != 1 && full != 7){
do
{
full = 0; //reset
if(turn % 2 == 1){
cout << Player_1.name << "'s Turn. ";
}
else if (turn % 2 == 0){
cout << Player_2.name << "'s Turn. ";
}
cout << "Please enter a number between 1 and 7: ";
cin >> DropChoice;
while ( board[1][DropChoice] == Player_1.character[0] || board[1][DropChoice] == Player_2.character[0] )
{
cout << "That row is full, please enter a new row: ";
cin >> DropChoice;
}
}while ( DropChoice < 1 || DropChoice > 7 ); //Keep input until input is correct
//Update board
if(turn % 2 == 1){
Update_Board(Player_1.character[0]);
}
else if (turn % 2 == 0){
Update_Board(Player_2.character[0]);
}
Display_Board();
//Check which players win
Check_Condition();
turn++;
//cout << full;
Choose3();
}
}
void Display_Board(){
int x, y;
for(x = 1; x <= trueWidth; x++) //print out layout; /*******/
{
cout << "|";
for(y = 1; y <= trueLength; y++)
{
if(board[x][y] != Player_1.character[0] && board[x][y] != Player_2.character[0])
board[x][y] = ' ';
cout << " " << board[x][y];
}
cout << " " << "|" << endl;
}
cout << " 1 2 3 4 5 6 7 " << endl; //mark the column
}
void Default_Board(){
int x, y;
for(x = 1; x <= trueWidth; x++){ //print out layout; |*******|
for(y = 1; y <= trueLength; y++){
board[x][y] = ' ';
}
}
}
void Update_Board(char player){
int out = 0;
int width = 6;
do
{
if ( board[width][DropChoice] != Player_1.character[0] && board[width][DropChoice] != Player_2.character[0] ) //Check any cherker placed from row 1 t0 6
{
board[width][DropChoice] = player; //Place checker
out = 1;
}
else
--width; // Check next row
}while ( out != 1 ); //Keep check until placed checker
}
void Win(char Player){
char Checker;
Checker = Player;
for( int i = 6; i >= 1; --i )
{
for( int j = 7; j >= 1; --j )
{
//diagoanl
if( board[i][j] == Checker &&
board[i-1][j-1] == Checker &&
board[i-2][j-2] == Checker &&
board[i-3][j-3] == Checker )
{
win = 1;
}
if( board[i][j] == Checker &&
board[i-1][j+1] == Checker &&
board[i-2][j+2] == Checker &&
board[i-3][j+3] == Checker )
{
win = 1;
}
//horizontal
if( board[i][j] == Checker &&
board[i][j-1] == Checker &&
board[i][j-2] == Checker &&
board[i][j-3] == Checker )
{
win = 1;
}
if( board[i][j] == Checker &&
board[i-1][j] == Checker &&
board[i-2][j] == Checker &&
board[i-3][j] == Checker )
{
win = 1;
}
//vertical
if ( board[i][j] == Checker &&
board[i][j+1] == Checker &&
board[i][j+2] == Checker &&
board[i][j+3] == Checker )
{
win = 1;
}
}
}
}
void Full(){
for ( int x = 1; x <= 7; ++x )
{
if ( board[1][x] != ' ' )
++full;
}
}
void Check_Condition(){
Full();
if(full == 7){
cout <<"Game Over!!! Draw!!!" << endl;
Play_Again(); //Ask Continues
}
if(turn % 2 == 1){
Win(Player_1.character[0]);
if ( win == 1){
cout << Player_1.name << " wins." << endl;
Play_Again(); //Ask Continues
}
}
else if (turn % 2 == 0){
Win(Player_2.character[0]);
if ( win == 1){
cout << Player_1.name << " wins." << endl;
Play_Again(); //Ask Continues
}
}
}
int Play_Again(){
char Continues;
do{
cout << "Continues??? (Y/N) : ";
cin >> Continues;
if (Continues == 'Y'){
Main_menu();
}
else if (Continues == 'N'){
cout << "Thank you for playing!!!" << endl;
break;
}
}while (Continues != 'Y' || Continues != 'N');
return 0;
}
void Main_menu(){ // Main menu
win = 0, full = 0, turn = 1, board[7][8] ={}; //Reset
Default_Board(); //Reset
cout << "\n";
cout << "Welcome to my Connect Four!!! " << endl;
cout << "1. Change Player Names, Please Enter 1." << endl;
cout << " Now are " << Player_1.name << " and " << Player_2.name << "." << endl;
cout << "2. Change Player Characters, Please Enter 2." << endl;
cout << " Now are " << Player_1.name << " : " << Player_1.character <<" and " << Player_2.name << " : " << Player_2.character << "." << endl;
cout << "3. Start Game!" << endl;
cout << "Enter your Choose : ";
cin >> Choose;
switch (Choose){
case 1: Choose1(); break;
case 2: Choose2(); break;
case 3: Choose3(); break;
default: {
cout << "\n";
cout << "Wrong Input! Please try again!" << endl;
cout << "\n";
Sleep(500);
Main_menu();
break;
}
}
}