-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
243 lines (212 loc) · 6.81 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
//
// main.c
// Snake
//
// Created by Fabio Codiglioni on 23/02/16.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <poll.h>
#define VERSION "2.0.0"
#define HEIGHT 20
#define WIDTH 40
#define EMPTY 0
#define FOOD -1
#define for_i for (i = 0; i < HEIGHT; i++)
#define for_j for (j = 0; j < WIDTH; j++)
/*** GLOBAL VARIABLES ***/
int grid[HEIGHT][WIDTH];
/*** HEADERS ***/
void clearScreen();
void splashScreen();
void printGrid(int score, int record);
void generateFood();
unsigned int turnSnake(char direction, unsigned int end, unsigned int *foodFlag);
/*** MAIN ***/
int main(int argc, const char *argv[]) {
unsigned int i, j;
unsigned int end = 0;
unsigned int foodFlag = 0;
unsigned int length = 1;
int score = -1, record = 0, delay = 1000;
char direction, prevDirection = 'd';
long duration = 0;
static struct termios oldt, newt; // Structs to get all the keystrokes directly to stdin
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI }; // Struct to timeout getchar
FILE *recordFile = fopen("snake_record.bin", "r+b");
if (recordFile != NULL) {
fread(&record, sizeof(int), 1, recordFile);
fclose(recordFile);
}
recordFile = NULL;
srand((unsigned int)time(NULL));
// Initialize grid
for_i
for_j
grid[i][j] = EMPTY;
for (i = 21; i >= 18; i--, length++)
grid[10][i] = length;
splashScreen();
// Instructions to get all the keystrokes directly to stdin
tcgetattr( STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON);
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
// Game cycle
while (end == 0) {
if (foodFlag == 0) {
generateFood();
foodFlag = 1;
score++;
}
clearScreen();
printGrid(score, record);
do {
if (poll(&mypoll, 1, delay))
direction = getchar();
else
direction = prevDirection;
} while (direction != 'w' && direction != 'a' && direction != 's' && direction != 'd' && direction != 'q');
if (direction == 'q') {
puts("\n\nEXIT\n");
exit(EXIT_SUCCESS);
}
end = turnSnake(direction, end, &foodFlag);
prevDirection = direction;
duration++;
if ((duration % 30) == 0 && delay > 200)
delay -= 50;
}
puts("\n\nGAME OVER!\n");
if (score > record) {
recordFile = fopen("snake_record.bin", "wb");
if (recordFile != NULL) {
fwrite(&score, sizeof(int), 1, recordFile);
fclose(recordFile);
}
}
// Restore normal mode
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
/*** FUNCTIONS ***/
void clearScreen() {
fflush(stdout);
printf("\e[1;1H\e[2J");
fflush(stdout);
}
void splashScreen() {
clearScreen();
//printf(" _____ _ _____ __ __ ______\n / ___// | / / | / //_// ____/\n \\__ \\/ |/ / /| | / ,< / __/\n ___/ / /| / ___ |/ /| |/ /___\n /____/_/ |_/_/ |_/_/ |_/_____/ v%s\n", VERSION);
//puts("\nCreated by Fabio Codiglioni");
printf(" _____ _ _ _ ________\n / ____| \\ | | /\\ | |/ / ____| \n | (___ | \\| | / \\ | ' /| |__ \n \\___ \\| . ` | / /\\ \\ | < | __| ____\n ____) | |\\ |/ ____ \\| . \\| |____ ________________________/ O \\___/\n |_____/|_| \\_/_/ \\_\\_|\\_\\______| <_/_\\_/_\\_/_\\_/_\\_/_\\_/_______/ \\ v%s", VERSION);
fflush(stdout);
sleep(2);
clearScreen();
}
void printGrid(int score, int record) {
unsigned int i, j;
fflush(stdout);
for (i = 0; i < WIDTH + 2; i++)
printf("%c", '-');
printf(" Score: %d\n", score);
for_i {
printf("%c", '|');
for_j {
if (grid[i][j] == EMPTY)
printf("%c", ' ');
else if (grid[i][j] == FOOD)
printf("%c", 'w');
else if (grid[i][j] == 1)
printf("%c", 'o');
else if (grid[i][j] > 1)
printf("%c", '*');
}
if (i == 0)
printf("| Record: %d\n", record);
else
printf("%s", "|\n");
}
for (i = 0; i < WIDTH + 2; i++)
printf("%c", '-');
puts("");
fflush(stdout);
}
void generateFood() {
unsigned int i, j;
do {
i = rand() % HEIGHT;
j = rand() % WIDTH;
} while (grid[i][j] != EMPTY);
grid[i][j] = FOOD;
}
unsigned int turnSnake(char direction, unsigned int end, unsigned int *foodFlag) {
unsigned int i, j, foodEaten = 0;
int max = 0;
for_i {
for_j {
if (grid[i][j] > 0) {
grid[i][j]++;
}
}
}
for_i
for_j
if (grid[i][j] > max)
max = grid[i][j];
for_i
for_j
if (grid[i][j] == 2) {
switch (direction) {
case 'w':
if (grid[i - 1][j] == EMPTY)
grid[i - 1][j] = 1;
else if (grid[i - 1][j] == FOOD) {
foodEaten = 1;
grid[i - 1][j] = 1;
}
else
end = 1;
break;
case 'a':
if (grid[i][j - 1] == EMPTY)
grid[i][j - 1] = 1;
else if (grid[i][j - 1] == FOOD) {
foodEaten = 1;
grid[i][j - 1] = 1;
}
else
end = 1;
break;
case 's':
if (grid[i + 1][j] == EMPTY)
grid[i + 1][j] = 1;
else if (grid[i + 1][j] == FOOD) {
foodEaten = 1;
grid[i + 1][j] = 1;
}
else
end = 1;
break;
case 'd':
if (grid[i][j + 1] == EMPTY)
grid[i][j + 1] = 1;
else if (grid[i][j + 1] == FOOD) {
foodEaten = 1;
grid[i][j + 1] = 1;
}
else
end = 1;
break;
}
}
for_i
for_j
if (grid[i][j] == max && foodEaten == 0)
grid[i][j] = EMPTY;
if (foodEaten == 1)
*foodFlag = 0;
return end;
}