-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
451 lines (399 loc) · 12.5 KB
/
board.c
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
#include <windows.h>
#include <MMSystem.h>
#include "board.h"
#include "keyboard.h"
#include "screen.h"
#include "setting.h"
#include "intro.h"
int sizey = 30;
int sizex = 15;
char player = 'A';
char playerLaser = '|';
char enemy = 'M';
char enemyShielded = 'T';
char enemyLaser = 186;
char explosion = 177;
int laserReady = 1;
int direction = LEFT; //setup huong di chuyen cua enemy ban dau sang ben trai
int initboard(int sizey, int sizex, char world[][sizex])
{
int x, y;
int totalEnemies = 0;
for (x = 0; x < sizex; x++)
{
for (y = 0; y < sizey; y++)
{
if ((y + 1)%2 == 0 && y < 7 && x > 4
&& x < sizex - 5 && x%2 == 0)
{
world[y][x] = enemy;
totalEnemies++;
}
else if ((y + 1)%2 == 0 && y >= 7 && y < 9 && x > 4
&& x < sizex - 5 && x%2 == 0)
{
world[y][x] = enemyShielded;
totalEnemies += 2;
}
else
{
world[y][x] = ' ';
}
}
}
world[sizey - 1][sizex/2] = player;
return totalEnemies;
}
int rules(char world[][sizex], int victory)
{
int x, t = 0; //su dung bien t de kiem tra con player o tren board hay khong
for(x = 0; x < sizex; x++)
{
if(world[sizey - 1][x] != player)
{
t++;
}
}
if(t == sizex)
{
victory = 0;
}
for (x = 0; x < sizex; x++)
{
//neu enemy cham vach nguoi choi thi thua
if (world[sizey - 1][x] == enemy || world[sizey - 1][x] == enemyShielded)
{
victory = 0;
}
}
return victory;
}
int setupEnemyLaser(char world[][sizex], long i, int currentEnemies, int score)
{
int y, x, yi;
int enemyReady = 0;
laserReady++;//
for (x = 0; x < sizex; x++)
{
for (y = sizey - 1; y >= 0; y--)
{
if (world[y][x] == enemyLaser
&& (world[y + 1][x] != enemy & world[y + 1][x] != enemyShielded)) //neu enemyLaser khong co enemy o phia truoc thi di chuyen enemyLaser xuong 1 hang
{
world[y + 1][x] = enemyLaser;
world[y][x] = ' ';
}
else if (world[y][x] == enemyLaser
&& (world[y + 1][x] == enemy | world[y + 1][x] == enemyShielded)) //neu enemyLaser co enemy o phia truoc thi xoa enemyLaser
{
world[y][x] = ' ';
}
}
}
for (x = 0; x < sizex; x++)
{
for (y = 0; y < sizey; y++)
{
//su dung bien i de setup thoi gian ra enemyLaser cu moi lan i%3 == 0 thi cho ra enemyLaser
//neu tai vi tri world[x][y] la dia bay va phia truoc khong co enemyLaser thi cho ra 1 enemyLaser
if ((i%3) == 0 && (world[y][x] == enemyShielded
| world[y][x] == enemy) && (rand()%15) > 8 //rand()%15 > 13 de giam tan suat cua enemyLaser muon tang tan suat thi de rand()%10 > 8
&& world[y + 1][x] != playerLaser)
{
// for (yi = y + 1; yi < sizey; yi++) //duyet tu vi tri co dia bay xuong tung hang
// {
// if (world[yi][x] == enemy | world[yi][x] == enemyShielded) //neu co dia bay khac o phia truoc thi khong cho ra enemyLaser
// {
// enemyReady = 0;
// break;
// }
enemyReady = 1;
// }
if (enemyReady) //neu khong thi cho ra enemyLaser
{
world[y + 1][x] = enemyLaser;
}
}
//neu playerLaser ban trung enemy
if (world[y][x] == playerLaser && world[y - 1][x] == enemy)
{
world[y][x] = ' '; //xoa playerLaser
world[y - 1][x] = explosion; //cho no enemy
score += 50;
}
//neu playerLaser nam ben phai enemy va enemy di sang trai
else if(world[y][x] == playerLaser && world[y][x + 1] == enemy && direction == LEFT)
{
world[y][x] = ' '; //xoa playerLaser
world[y][x + 1] = explosion; //cho no enemy
score += 50;
}
//neu playerLaser nam ben trai enemy va enemy di sang phai
else if(world[y][x] == playerLaser && world[y][x - 1] == enemy && direction == RIGHT)
{
world[y][x] = ' '; //xoa playerLaser
world[y][x - 1] = explosion; //cho no enemy
score += 50;
}
//neu playerLaser nam cheo ben phai enemy va enemy di sang trai
else if(world[y][x] == playerLaser && world[y - 1][x + 1] == enemy && direction == LEFT)
{
world[y][x] = ' '; //xoa playerLaser
world[y - 1][x + 1] = explosion; //cho no enemy
score += 50;
}
//neu playerLaser nam cheo ben trai enemy va enemy di sang phai
else if(world[y][x] == playerLaser && world[y - 1][x - 1] == enemy && direction == RIGHT)
{
world[y][x] = ' '; //xoa playerLaser
world[y - 1][x - 1] = explosion; //cho no enemy
score += 50;
}
else if (world[y][x] == playerLaser && world[y - 1][x] == enemyShielded)
{
world[y][x] = ' ';
world[y - 1][x] = enemy;
score += 50;
}
//neu playerLaser nam ben phai enemyShielded va enemyshielded di sang trai
else if(world[y][x] == playerLaser && world[y][x + 1] == enemyShielded && direction == LEFT)
{
world[y][x] = ' ';
world[y][x + 1] = enemy;
score += 50;
}
//neu playerLaser nam ben trai enemyShielded va enemyshielded di sang phai
else if(world[y][x] == playerLaser && world[y][x - 1] == enemyShielded && direction == RIGHT)
{
world[y][x] = ' ';
world[y][x - 1] = enemy;
score += 50;
}
//neu playerLaser nam cheo ben phai enemy va enemyShielded di sang trai
else if(world[y][x] == playerLaser && world[y - 1][x + 1] == enemyShielded && direction == LEFT)
{
world[y][x] = ' ';
world[y - 1][x + 1] = enemy;
score += 50;
}
//neu playerLaser nam cheo ben trai enemy va enemyShielded di sang phai
else if(world[y][x] == playerLaser && world[y - 1][x - 1] == enemyShielded && direction == RIGHT)
{
world[y][x] = ' ';
world[y - 1][x - 1] = enemy;
score += 50;
}
else if (world[y][x] == playerLaser //neu enemyLaser o tren playerLaser thi xoa ki tu playerLaser
&& world[y - 1][x] == enemyLaser)
{
world[y][x] = ' ';
}
else if (world[y][x] == explosion)
{
world[y][x] = ' ';
}
else if (world[y][x] == playerLaser && world[y - 1][x] != enemyLaser)
{
world[y][x] = ' ';
world[y - 1][x] = playerLaser;
}
}
}
return score;
}
void updateBoard(char world[][sizex], long i, int enemySpeed, int drop, int victory)
{
int y, x;
for (y = 0; y < sizey; y++) //duyet 2 bien trai va phai
{
if (world[y][0] == enemy)
{
direction = RIGHT; //neu enemy cham bien trai thi di chuyen enemy sang phai
drop = 1;
break;
}
if (world[y][sizex - 1] == enemy) //neu enemy cham bien phai thi di chuyen enemy sang trai
{
direction = LEFT;
drop = 1;
break;
}
}
if (i%enemySpeed == 0) //su dung bien i de dieu chinh toc do cua enemy, neu enemySpeed cang nho thi toc do cang nhanh
{
if (direction == LEFT) //direction ban dau = left
{
for (x = 0; x < sizex - 1; x++)
{
for (y = 0; y < sizey; y++)
{
//neu drop = 1 va enemy o vi tri bien phai thi di chuyen enemy xuong va di qua trai
if (drop && (world[y - 1][x + 1] == enemy //drop ban dau = 0
|| world[y - 1][x + 1] == enemyShielded))
{
world[y][x] = world[y - 1][x + 1];
world[y - 1][x + 1] = ' ';
}
else if (!drop && (world[y][x + 1] == enemy //drop = 0 thi di chuyen enemy qua trai
|| world[y][x + 1] == enemyShielded))
{
world[y][x] = world[y][x + 1];
world[y][x + 1] = ' ';
}
}
}
}
else
{
for (x = sizex - 1; x > 0; x--)
{
for (y = 0; y < sizey; y++)
{
//neu drop = 1 va enemy o vi tri bien trai thi di chuyen enemy xuong va di qua phai
if (drop && (world[y - 1][x - 1] == enemy
|| world[y - 1][x - 1] == enemyShielded))
{
world[y][x] = world[y - 1][x - 1];
world[y - 1][x - 1] = ' ';
}
else if (!drop && (world[y][x - 1] == enemy //neu drop = 0 thi di chuyen enemy qua phai
|| world[y][x - 1] == enemyShielded))
{
world[y][x] = world[y][x - 1];
world[y][x - 1] = ' ';
}
}
}
}
}
}
void playerMovement(char world[][sizex], int victory)
{
int keyPress;
int y, x;
if(keypressed())
{
keyPress = getkey();
}
else //neu khong nhan gi thi cho keyPress = ' ' de tranh lap lai di chuyen
{
keyPress = ' ';
}
if (keyPress == KEY_LEFT) //di chuyen player sang trai neu nhan LEFT
{
for (x = 0; x < sizex; x = x + 1)
{
if (world[sizey - 1][x + 1] == player)
{
if(world[sizey - 1][x] == enemyLaser)
{
victory = 0;
}
else
{
world[sizey - 1][x] = player;
world[sizey - 1][x + 1] = ' ';
}
}
}
}
if (keyPress == KEY_RIGHT) //di chuyen player sang ben phai neu nhan RIGHT
{
for (x = sizex - 1; x > 0; x = x - 1)
{
if ( world[sizey - 1][x - 1] == player)
{
if(world[sizey - 1][x] == enemyLaser)
{
victory = 0;
}
else
{
world[sizey - 1][x] = player;
world[sizey - 1][x - 1] = ' ';
}
}
}
}
if (keyPress == KEY_UP && laserReady > 10) //ban ra playerLaser neu nhan UP
{
//laserReady dung de dan cach dan, khong cho dan ban lien tuc
for (x = 0; x < sizex; x++)
{
if (world[sizey - 1][x] == player)
{
world[sizey - 2][x] = playerLaser;
laserReady = 0;
}
}
}
}
int countEnemies(int sizey, int sizex, char world[][sizex])
{
int count = 0, y, x;
for(x = 0; x < sizex; x++)
{
for(y = 0; y < sizey; y++)
{
if(world[y][x] == enemy)
{
count++;
}
if(world[y][x] == enemyShielded)
{
count += 2;
}
}
}
return count;
}
void game(int level, int victory, int score)
{
long i = 0;
//setup board
char world[sizey][sizex];
int totalEnemies = initboard(sizey, sizex, world);
int currentEnemies = totalEnemies;
while(currentEnemies > 0 && victory)
{
hidecursorscr();
int drop = 0; //setup mac dinh huong di chuyen cua enemy KHONG xuong hang
int enemySpeed;
enemySpeed = 1 + 10 * currentEnemies/totalEnemies;
//laserReady ban dau = 1
/*display world*/
prtscr(score, level, sizey, sizex, world);
/*setup enemyLaser*/
score = setupEnemyLaser(world, i, currentEnemies, score);
/*update enemy direction and update board*/
updateBoard(world, i, enemySpeed, drop, victory);
/*count enemies on board*/
currentEnemies = countEnemies(sizey, sizex, world);
/*control player*/
playerMovement(world, victory);
i++;
Sleep(10);
victory = rules(world, victory);
}
if (victory != 0)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10 + level);
level++;
sizex += 5;
i = 0;
game(level, victory, score);
}
else
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
printf("\n \n \n \n \n \n Ban da thua.");
Sleep(1000);
printf("\n \n \n \n \n \n Game over.");
Sleep(1000);
highScore(score, "high score.txt");
Sleep(1000);
printf("\n \n Final Score: %d", score);
getch();
}
}