-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
419 lines (348 loc) · 9.45 KB
/
main.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
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include "list_ptr.h"
// penser au COLOR_PAIR
/**************************/
/* Parameters of the Game */
/**************************/
#define max(a, b) ((a > b) ? a : b)
#define min(a, b) ((a > b) ? b : a)
#define abs(a) ((a > 0) ? a : -a)
#define randab(a, b) ( (rand() % abs(b - a)) + min(a, b))
#define WAIT_TIME 50 // the time to wait for a user input
#define GROWTH 5 // length's gain when one food is eat
#define MAX_FOOD 10 // Maximum food quantity at the same time
#define RANDOMIZE FALSE
// they both take a value in initGame()
int CAN_CROWL_ON_HIM; // obvious
int CAN_GO_THROUGH_BORDER; // "
WINDOW *gWGame, *gWStats;
typedef enum {UP, DOWN, LEFT, RIGHT} t_dir;
int justForKonami = -1;
// initialize the ncurses library, the random, and print the screen
void initGame () {
timeout(WAIT_TIME); // if no input then dont block
//cbreak();
start_color();
init_pair(1 , 28, COLOR_BLACK);
keypad(stdscr, TRUE); // use of the arrow keys
noecho(); // no echo (obvious :p)
curs_set(0); // no cursor
gWGame= subwin(stdscr, LINES - 3, COLS, 0, 0);
gWStats= subwin(stdscr, 3, COLS, LINES - 3, 0);
box(gWGame, ACS_VLINE, ACS_HLINE);
box(gWStats, ACS_VLINE, ACS_HLINE);
mvwprintw(gWGame, 0, 1, " Snake IT! --- <> with ♥ by T2 --- ");
mvwprintw(gWStats, 0, 1, " Stats ");
wrefresh(gWGame);
wrefresh(gWStats);
CAN_CROWL_ON_HIM = (rand()%2 == 0) ? TRUE : FALSE;
CAN_GO_THROUGH_BORDER = (rand()%2 == 0) ? TRUE : FALSE;
}
// display the snake
void displaySnake (int isAlive, int currDir) {
t_pos pos;
listPtr_move2head();
listPtr_next ();
while (!listPtr_isOut ()) {
listPtr_readData (&pos);
switch (pos.body) {
case HORIZONTAL: mvwprintw(gWGame, pos.line, pos.col, "═"); break;
case VERTICAL: mvwprintw(gWGame, pos.line, pos.col, "║"); break;
case TOP_LEFT: mvwprintw(gWGame, pos.line, pos.col, "╔"); break;
case TOP_RIGHT: mvwprintw(gWGame, pos.line, pos.col, "╗"); break;
case BOTTOM_LEFT: mvwprintw(gWGame, pos.line, pos.col, "╚"); break;
case BOTTOM_RIGHT: mvwprintw(gWGame, pos.line, pos.col, "╝"); break;
}
listPtr_next();
}
listPtr_move2end();
listPtr_readData(&pos);
mvwprintw(gWGame, pos.line, pos.col, "*");
listPtr_move2head();
listPtr_readData (&pos);
if (isAlive) {
switch (currDir) {
case UP: mvwprintw(gWGame, pos.line, pos.col, "^"); break;
case DOWN: mvwprintw(gWGame, pos.line, pos.col, "v"); break;
case RIGHT: mvwprintw(gWGame, pos.line, pos.col, ">"); break;
case LEFT: mvwprintw(gWGame, pos.line, pos.col, "<"); break;
}
}
else mvwprintw(gWGame, pos.line, pos.col, "!");
}
// display some information about the game
void displayStats(int foodEat, int length, int foodQtt) {
mvwprintw(gWStats, 1, 1, "foodEat: %d, length: %d, foodQtt: %d, cannibalism(c): %3s, ghost(g): %3s | 'q' to quit | 'r' to replay", foodEat, length, foodQtt, CAN_CROWL_ON_HIM ? "YES" : "NO", CAN_GO_THROUGH_BORDER ? "YES" : "NO");
wrefresh(gWStats);
}
// display all the food
void displayFood(t_pos * foods, int foodQtt) {
int i;
for (i = 0; i < foodQtt; i++) {
wattron(gWGame, COLOR_PAIR(1));
mvwprintw(gWGame, foods[i].line, foods[i].col, "♥");
wattroff(gWGame, COLOR_PAIR(1));
}
}
// add a random food at the i position
void randomFood(t_pos * foods, int i) {
foods[i].line = rand()%(LINES - 3 - 2) + 1;
foods[i].col = rand()%(COLS - 2) + 1;
}
// return TRUE if the snake eat and update some stuff if he did
int eat(t_pos head, t_pos * foods, int foodQtt, int * foodEat, int * growth) {
int i, eatSometh = FALSE;
for (i = 0; i < foodQtt; i++) {
if (head.line == foods[i].line && head.col == foods[i].col) {
randomFood(foods, i);
eatSometh = TRUE;
(*foodEat)++;
(*growth)+=GROWTH;
}
}
return eatSometh;
}
void randPosOnWall (t_pos * pos, t_dir * currDir) {
int wall = rand()%4;
int bounce = rand()%2;
if (bounce == 0) {
switch (wall) {
case 0:
*currDir = UP;
pos -> line = LINES - 5;
pos -> col = randab(1, COLS - 2);
break;
case 1:
*currDir = DOWN;
pos -> line = 1;
pos -> col = randab(1, COLS - 2);
break;
case 2:
*currDir = RIGHT;
pos -> line = randab(1, LINES - 5);
pos -> col = 1;
break;
case 3:
*currDir = LEFT;
pos -> line = randab(1, LINES - 5);
pos -> col = COLS - 2;
break;
}
}
else {
// *currDir = (*currDir == UP) ? DOWN : (*currDir == DOWN) ? UP : (*currDir == RIGHT) ? LEFT : RIGHT; // not eazy to understand..
switch (*currDir) {
case UP:
*currDir = DOWN;
(pos -> line)++;
break;
case DOWN:
*currDir = UP;
(pos -> line)--;
break;
case RIGHT:
*currDir = LEFT;
(pos -> col)--;
break;
case LEFT:
*currDir = RIGHT;
(pos -> col)++;
break;
}
}
}
void endGame() {
int i, j;
for (i = 0; i < LINES; i++)
for (j = 0; j < COLS; j++)
mvprintw (i, j, " ");
delwin(gWGame);
delwin(gWStats);
}
int snake(void) {
/**********************************/
/* Declaration and Initialisation */
/**********************************/
int key = -1,
foodEat = 0,
length = 1,
growth = 0,
foodQtt = 1,
continueGame = TRUE;
t_dir currDir = RIGHT,
prevDir;
t_pos head = {1, 1, HORIZONTAL},
prev,
end,
foods[MAX_FOOD];
initGame();
randomFood(foods, 0);
listPtr_init ();
listPtr_appendHead (head);
/********************/
/* Loop of the Game */
/********************/
while (continueGame) {
// listen for the next player's action
key = getch();
prevDir = currDir;
switch (key) {
case 'r':
case 'q': // quit
continueGame = FALSE;
break;
case ' ':
growth += 1;
break;
case 'g':
CAN_GO_THROUGH_BORDER = !CAN_GO_THROUGH_BORDER;
break;
case 'c':
CAN_CROWL_ON_HIM = !CAN_CROWL_ON_HIM;
break;
// the player can't go to the opposite direction
case 'k':
case KEY_UP: // go up
if (currDir != DOWN) currDir = UP;
break;
case 'j':
case KEY_DOWN: // go down
if (currDir != UP) currDir = DOWN;
break;
case 'h':
case KEY_LEFT: // go left
if (currDir != RIGHT) currDir = LEFT;
break;
case 'l':
case KEY_RIGHT: // go right
if (currDir != LEFT) currDir = RIGHT;
break;
default: // anything else ? just continue
break;
}
listPtr_move2head();
listPtr_readData (&head);
prev = head;
// updating the position of the head
switch (currDir) {
case UP: head.line --; break;
case DOWN: head.line ++; break;
case RIGHT: head.col ++; break;
case LEFT: head.col --; break;
}
// find the symbol to represent the part nex to the head
if (prevDir == currDir) {
if (currDir == UP || currDir == DOWN) prev.body = VERTICAL;
else prev.body = HORIZONTAL;
}
else {
if ((prevDir == UP && currDir == RIGHT) || (prevDir == LEFT && currDir == DOWN))
prev.body = TOP_LEFT;
else if ((prevDir == UP && currDir == LEFT) || (prevDir == RIGHT && currDir == DOWN))
prev.body = TOP_RIGHT;
else if ((prevDir == DOWN && currDir == RIGHT) || (prevDir == LEFT && currDir == UP))
prev.body = BOTTOM_LEFT;
else
prev.body = BOTTOM_RIGHT;
}
// update the part next to the head
listPtr_move2head();
listPtr_removeElt (); // an update element primitive should be nice
listPtr_appendHead (prev);
// is it a legal movment ?
if (
(
head.line >= LINES - 3 - 1 ||
head.line <= 0 ||
head.col >= COLS - 1 ||
head.col <= 0
) && CAN_GO_THROUGH_BORDER == FALSE
) continueGame = FALSE;
else {
if (head.line >= LINES - 3 - 1 || head.line <= 0 || head.col >= COLS - 1 || head.col <= 0) {
if (
CAN_GO_THROUGH_BORDER &&
CAN_CROWL_ON_HIM &&
RANDOMIZE
) randPosOnWall(&head, &currDir);
else {
if (head.line >= LINES - 3 - 1) head.line = 1;
else if (head.line <= 0) head.line = LINES - 3 - 2;
if (head.col >= COLS - 1) head.col = 1;
else if (head.col <= 0) head.col = COLS - 2;
}
}
if (eat(head, foods, foodQtt, &foodEat, &growth) && foodQtt < MAX_FOOD) {
randomFood(foods, foodQtt);
foodQtt++;
}
if (growth == 0) {
listPtr_move2end ();
listPtr_readData(&end);
mvwprintw(gWGame, end.line, end.col, " ");
listPtr_removeElt ();
} else {
growth--;
length++;
}
if (listPtr_isInList(head) && CAN_CROWL_ON_HIM == FALSE) {
continueGame = FALSE;
} else {
listPtr_appendHead (head);
displaySnake(continueGame, currDir);
displayFood(foods, foodQtt);
wrefresh(gWGame);
displayStats(foodEat, length, foodQtt);
}
}
}
/*******************/
/* End of the Game */
/*******************/
listPtr_move2end ();
listPtr_readData(&end);
mvwprintw(gWGame, end.line, end.col, " ");
listPtr_removeElt ();
listPtr_appendHead (head);
displaySnake(continueGame, currDir);
wrefresh(gWGame);
listPtr_removeList ();
displayStats(foodEat, length, foodQtt);
timeout(-1);
while (key != 'q') {
if (key == 'r') {
endGame();
return 1;
}
key = getch();
}
endGame();
return 0;
}
int konami (int key) {
int code[10] = {KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_LEFT, KEY_RIGHT, 'b', 'a'};
justForKonami++;
if (key == code[justForKonami]) {
if (key == 'a') {
snake();
refresh();
return TRUE;
}
}
else {
if (key == KEY_UP)justForKonami = 0;
else justForKonami = -1;
}
return FALSE;
}
int main () {
srand(time(NULL));
setlocale(LC_ALL, "");
initscr();
while(snake());
endwin();
return 0;
}