-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcSnake.c
143 lines (118 loc) · 3.49 KB
/
cSnake.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
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <getopt.h>
#include "board.c"
#include "console.c"
// To compile: gcc -x c cSnake.c -o c-snake -lpthread
void INThandler(int);
struct Board board;
void pre_quit()
{
console_get_key_listen_cancel();
destroy_board(board.width, board.height);
console_cursor_move_by(board.width, board.height);
console_enable_char();
console_show_cursor(true);
}
/**
* Prints out standard unix usage text
*/
void console_usage_print() {
printf(MANUAL_DESCRIPTION);
}
void options_parse(int argc, char **argv, struct Board *board)
{
int opt = 0;
int width = BOARD_WIDTH_DEFAULT;
int height = BOARD_HEIGHT_DEFAULT;
bool wrapAround = false;
//Specifying the expected options
static struct option long_options[] = {
// Ags are actually all optional
{"help", no_argument, 0, 'e'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
{"wrap-around", no_argument, 0, 'a'},
{0, 0, 0, 0 }
};
int long_index = 0;
while ((opt = getopt_long(argc, argv,"w:h:a",
long_options, &long_index )) != -1) {
switch (opt) {
case 'a':
wrapAround = true;
break;
case 'e':
console_usage_print();
exit(0);
break;
case 'h' : height = atoi(optarg);
break;
case 'w' : width = atoi(optarg);
break;
default: console_usage_print();
exit(EXIT_FAILURE);
}
}
board_setup(board, width, height, wrapAround);
}
int main(int argc, char **argv)
{
signal(SIGINT, INThandler);
options_parse(argc, argv, &board);
int delay = 500000;
int snakePieces[board.cellsCount];
int snakeLength = 10;
for (int i = 0; i < snakeLength; i++) {
snakePieces[i] = (snakeLength - i) + 2;
}
console_disable_char();
key input = 0;
key inputNew = input;
console_get_key_listen_start(&input);
bool snakeGrew = false;
int foodPosition = board_create_food_position(board, snakePieces, snakeLength);
snake_move(&snakePieces[0], &snakeLength, KEY_RIGHT, board, foodPosition, &snakeGrew);
console_show_cursor(false);
while (true) {
if (snakeGrew) {
foodPosition = board_create_food_position(board, snakePieces, snakeLength);
}
board_draw(board, snakePieces, snakeLength, foodPosition);
console_cursor_move_by(board.width, board.height);
if (input > 0) {
console_get_key_listen_stop();
if (input == KEY_ESCAPE) {
pre_quit();
return 0;
}
inputNew = input;
input = 0;
console_get_key_listen_start(&input);
}
usleep(delay);
snakeGrew = false;
if (!snake_move(&snakePieces[0], &snakeLength, inputNew, board, foodPosition, &snakeGrew)) {
printf("Game Over!\n");
console_cursor_move_by(0, 1);
usleep(5 * 1000000);
pre_quit();
return 0;
}
if (snakeGrew) {
delay = delay - 20000;
if (delay < 10000) {
delay = 10000;
}
}
}
return 0;
}
void INThandler(int sig)
{
pre_quit();
exit(1);
}