-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
468 lines (450 loc) · 11.2 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include<iostream>
#include<SFML/Graphics.hpp>
using namespace std;
//Functions prototypes
void design_Items(sf::RectangleShape, sf::Sprite , sf::Texture , sf::Text , sf::Font , sf::Font , sf::Text , sf::Text , sf::Text ); //Line 36
void recordTheInput(int); //Line 74
void draw_playBoard(sf::RectangleShape, sf::Text); //Line 98
void drawCircle(int, int); //Line 114
void change_connect4_circle_colors(); //Line 134
void checkWiner(int, int); //Line 297
//constant variables
const int scr_width = 1200;
const int scr_height = 750;
const int grid_size = 100;
const int row_size = 6;
const int col_size = 7;
const int margin = 20;
sf::RenderWindow window(sf::VideoMode(scr_width, scr_height), "Connect Four- DSA Project"); //Create the window
//Gamelogic data
string player, player1 = "Player 1", player2 = "Player 2 ", winner;
int x[6][7]; //Track every ceil
int point[] = { 5,5,5,5,5,5,5 }; //track input row of every colomn
int win = 0; //Track there is a winner
int start_col = 0; //track for winning circles
int start_row = 0;
int end_col = 0;
int end_row = 0;
int color=0; //track ceil color
//
void design_Items(sf::RectangleShape *grid, sf::Sprite *title, sf::Texture *texture, sf::Text *text, sf::Font *font, sf::Font *font2, sf::Font *font3, sf::Text *status, sf::Text *winText, sf::Text *winName,sf::Text *instruction)
{
//Grid design
(*grid).setFillColor(sf::Color::Transparent);
(*grid).setOutlineThickness(2.0f);
(*grid).setOutlineColor(sf::Color::White);
//Title design
(*texture).loadFromFile("title.png");
(*title).setTexture(*texture);
(*title).setPosition(750,10);
//Connect fonts
(*font).loadFromFile("ElFonteAngelia-Bold.ttf");
(*font2).loadFromFile("Milky-Coffee.ttf");
(*font3).loadFromFile("Better-Day.ttf");
//Numbers design
(*text).setFont(*font);
(*text).setString("");
(*text).setCharacterSize(80);
//Status design
(*status).setFont(*font2);
(*status).setString("");
(*status).setCharacterSize(150);
//win text design
(*winText).setFont(*font2);
(*winText).setString("WIN!");
(*winText).setCharacterSize(150);
(*winText).setPosition(810,385);
//winner name design
(*winName).setFont(*font2);
(*winName).setString("");
(*winName).setCharacterSize(50);
(*winName).setPosition(810,350);
//instruction design
(*instruction).setFont(*font2);
(*instruction).setString("");
(*instruction).setCharacterSize(100);
}
void recordTheInput(int index)
{ index--;
if(point[index]>=0)
{
if (player == player1)
{
x[point[index]][index] = 1;
checkWiner(point[index], index);
if(win==1)
winner=player1;
player = player2;
}
else if (player == player2)
{
x[point[index]][index] = 2;
checkWiner(point[index], index);
if(win==1)
winner=player2;
player = player1;
}
point[index]--;
}
}
void draw_playBoard(sf::RectangleShape *grid, sf::Text *text)
{ string num[]={"1","2","3","4","5","6","7"}; //Numbers to display under the game board
for (int i = 0; i < col_size; i++)
{
for (int j = 0; j < row_size; j++)
{
(*grid).setPosition(sf::Vector2f(margin + grid_size * i, margin + grid_size * j)); // (left,up)
window.draw(*grid);
drawCircle(j, i);
}
(*text).setString(num[i]);
(*text).setPosition(sf::Vector2f(50 + grid_size * i, 620));
window.draw(*text);
}
}
void drawCircle(int row,int colomn)
{
if(x[row][colomn]!=0)
{
sf::CircleShape circle(49);
circle.setPosition(sf::Vector2f(margin + colomn * 100, margin + row * 100));
if (x[row][colomn] == 1)
circle.setFillColor(sf::Color(247, 52, 52)); //red
if(x[row][colomn] == 2)
circle.setFillColor(sf::Color(250, 200, 50)); //yellow
if (x[row][colomn] == 11)
circle.setFillColor(sf::Color(166, 10, 10)); //dark red
if (x[row][colomn] == 22)
circle.setFillColor(sf::Color(138, 104, 4)); //dark yellow
window.draw(circle);
}
if (point[0]<0&&point[1]<0&&point[2]<0&&point[3]<0&&point[4]<0&&point[5]<0&&point[6]<0)//Check wether game drow
win=2;
}
void change_connect4_circle_colors()
{
color=11;
if (start_col == end_col)
{
if (x[start_row][start_col] == 2) //if first circle of connect 4 is yellow
color = 22; //color set as dark
while (start_row <= end_row)
{
x[start_row][start_col] = color;
start_row++;
}
}
else if (start_row == end_row)
{
if (x[start_row][start_col] == 2)
color = 22;
while (end_col <= start_col)
{
x[start_row][end_col] = color;
end_col++;
}
}
else if (start_row < end_row && start_col > end_col)
{
if (x[start_row][start_col] == 2)
color = 22;
while (end_col <= start_col)
{
x[start_row][start_col] = color;
start_row++;start_col--;
}
}
else if (start_row < end_row && start_col < end_col)
{
if (x[start_row][start_col] == 2)
color = 22;
while (start_col <= end_col)
{
x[start_row][start_col] = color;
start_row++;start_col++;
}
}
}
int main()
{
player = player1; //By defolt first player is player1
for (int i = 0; i < 6; i++) //Assign '0' to every x
{
for (int t = 0; t < 7; t++)
{
x[i][t] = 0;
}
}
//Create every stuff which need to display
sf::RectangleShape grid(sf::Vector2f(grid_size, grid_size));//Create Grid
sf::Sprite title;//create Title
sf::Texture texture;
sf::Text text; //Numbers Create Text variable type for Numbers(1,2,3,4,5,6,7)
sf::Font font; //Create pront type
sf::Font font2;//Arialn
sf::Font font3;
sf::Text instruction;
sf::Text status; //Create text for status
sf::Text winText; //Create text for display "WIN" text
sf::Text winName; //Create text for display winner
design_Items(&grid,&title, &texture, &text, &font, &font2, &font3, &status, &winText, &winName, &instruction);//Design all created stuff
//
window.setFramerateLimit(15); //set maximum frame rate to 15fps
while (window.isOpen()) //If the WINDOW OPEN this loop execute
{
window.clear(); //first, Clear the window
sf::Event event; //Create Event variable called "event"
while (window.pollEvent(event)) //If some Event(PRESS a key, PRESS mouse key, etc) happend
{
switch (event.type)//check the event / check entered key
{
case sf::Event::Closed: //if PRESS CLOSE button
window.close(); //window close
break;
case sf::Event::KeyPressed: //if press a KEYBOAD KEY
if (win==0 &&event.key.code > 26 && event.key.code < 34)//GET INPUT //check whether ented 1-7 number //Num1=27
{
recordTheInput(event.key.code-26);//call "recordTheInput" function
break;
}
if (event.key.code == sf::Keyboard::Space)//SPACE code is 57, RESET the program
{
win = 0; //set as no winner yet
player = player1; //set current player is player1
for (int i = 0; i < 7; i++) //Assign '5' to every point
{
point[i] = 5;
}
for (int i = 0; i < 6; i++) //Assign '0' to every x
{
for (int t = 0; t < 7; t++)
{
x[i][t] = 0;
}
}
}
break;
}
}
//Draw stuffs here
draw_playBoard(&grid, &text); //Draw the play board
if(win!=0) //If there is WINNER or game DRAW
{
if(win==1)
{
if(player == player2)
winName.setFillColor(sf::Color(247, 52, 52));
if(player == player1)
winName.setFillColor(sf::Color(250, 200, 50));
winName.setString(winner);
window.draw(winName);
window.draw(winText);
status.setString("Press SPACE to start new game");
//change color of connect 4 circles
change_connect4_circle_colors();
//
}
else
status.setString("GAME DRAW,\n Press SPACE to start new game");
status.setCharacterSize(30);
status.setPosition(750,180);
status.setFillColor(sf::Color(255,255,255));
}
else //If there is NO WINNER
{
if(player == player1)
{
status.setString("\t RED\nPlayer Turn ");
status.setFillColor(sf::Color(247, 52, 52));
}
else
{
status.setString(" YELLOW\nPlayer Turn ");
status.setFillColor(sf::Color(250, 200, 50));
}
status.setCharacterSize(40);
status.setPosition(850,250);
instruction.setCharacterSize(26);
instruction.setPosition(750,150);
instruction.setFillColor(sf::Color(255,255,255));
instruction.setString("Enter the Column Number\n You wish to insert the disk");
window.draw(instruction);
}
window.draw(title); //Draw the title
window.draw(status); //Draw the status (RED Player Turn / YELLOW\nPlayer Turn)
//end_Draw stuffs here
window.display();
}
return 0;
}
void checkWiner(int row, int column)//Game LOGIC part.
{
int coun = 1, condition = 1, y = row, z = column, n = 0;//"coun" for track number of connected circles
start_row = row;end_row = row;
start_col = column;end_col = column;
if (row < 3)//Down form last entered
{
while (1)
{
end_col = column;
if (x[y][column] == x[y + 1][column])
coun++;
else
break;
if (coun == 4)
{
end_row = y + 1;
win = 1;
break;
}
y++;
}
}
y = row; coun = 1;
while (win == 0)//Left form last entered
{
end_row = row; end_col=column;
if ((z - 1) >= 0)
{
if (x[y][z] == x[y][z - 1])
{
end_col=z-1;
coun++;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z--;
}
else
break;
}
if(win==0) //special porpose
end_col=z;
z = column;
while (win == 0)//Right form last entered
{
if ((z + 1) <= 6)
{
if (x[y][z] == x[y][z + 1])
{
coun++;
start_col=z+1;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z++;
}
else
break;
}
y = row; z = column; coun = 1;
if(win==0) //special porpose
{
start_row = row; end_row=row;
start_col = column; end_col=column;
}
while (win == 0)//Left Down form last entered
{
if ( (y + 1) <= 5 && (z - 1) >= 0 )
{
if (x[y][z] == x[y+1][z - 1])
{
end_row=y+1;end_col=z-1;
coun++;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z--; y++;
}
else
break;
}
y = row; z = column;
while (win == 0)//Right Up form last entered
{
if ((y - 1) >= 0 && (z + 1) <= 6)
{
if (x[y][z] == x[y - 1][z + 1])
{
start_row=y-1;start_col=z+1;
coun++;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z++; y--;
}
else
break;
}
if(win==0) //special porpose
{
start_row = row; end_row=row;
start_col = column; end_col=column;
}
y = row; z = column; coun = 1;
while (win == 0)//Left Up form last entered
{
if ((y - 1) <= 5 && (z - 1) >= 0)
{
if (x[y][z] == x[y - 1][z - 1])
{
start_row = y-1;
start_col = z-1;
coun++;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z--; y--;
}
else
break;
}
y = row; z = column;
while (win == 0)//Right Down form last entered
{
if ((y + 1) >= 0 && (z + 1) <= 6)
{
if (x[y][z] == x[y + 1][z + 1])
{
end_row = y+1;
end_col = z+1;
coun++;
}
else
break;
if (coun == 4)
{
win = 1;
break;
}
z++; y++;
}
else
break;
}
}