-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
311 lines (263 loc) · 9.98 KB
/
snake.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
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
#include <sys/ioctl.h>
//#include <stropts.h>
#include <termios.h>
#include <fcntl.h>
#define ROWS 23
#define COLUMNS 80
#define B L"\u2501"//STRAIGHT LINE
#define S L"\u2800"//EMPTY SPACE
#define UL L"\u250F"//UPPER LEFT CORNER
#define UR L"\u2513"//UPPER RIGHT CORNER
#define BL L"\u2517"//BOTTOM LEFT CORNER
#define BR L"\u251B"//BOTTOM RIGHT CORNER
#define BP L"\u2503"//VERTICAL LINE
#define PR L"\u227B"//PLAYER RIGHT SPRITE
#define PL L"\u227A"//PLAYER LEFT SPRITE
#define PU L"\u2227"//PLAYER UP SPRITE
#define PD L"\u2228"//PLAYER DOWN SPRITE
#define PH L"\u0040"//PLAYER HEAD SPRITE
#define FOOD L"\u2605"//FOOD SPRITE
#define T 100000 //TIME FROM ONE MOVE TO THE OTHER
#define game_t int*
struct termios saved_attributes;
void reset_input_mode ();
void set_input_mode();
void printArena(game_t arena[][COLUMNS]);
void spawnPlayer(game_t arena[][COLUMNS]);
int isPlayer(game_t arena[][COLUMNS]);
void move_up(game_t arena[][COLUMNS]);
void move_down(game_t arena[][COLUMNS]);
void move_left(game_t arena[][COLUMNS]);
void move_right(game_t arena[][COLUMNS]);
void helloScreen();
void spawnFood(game_t arena[][COLUMNS]);
void removeTail(game_t arena[][COLUMNS]);
int x,y; //Position of the head of snake
int tailx,taily; //position to the tail of the snake
int flag=0; // 0=no food spawned, 1=food already spawned
int main(){
setlocale(LC_ALL, ""); //TO PRINT UNICODE CHARACTERS
//setlocale(LC_CTYPE,"C-UTF-8");
//helloScreen();
fcntl(0, F_GETFL, 0);
fcntl(0, F_SETFL,O_NONBLOCK);//USED TO AVOID INPUT WAITING
srand(time(0));
//DEFINITION OF PLAYABLE MATRIX
game_t ciccio[ROWS][COLUMNS]={
{UL,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,UR},
{BP,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,BP},
{BP,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,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S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,S,BP},
{BL,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,BR}
};
spawnPlayer(ciccio); //SPAWN PLAYER AT INITIAL POSITION
printArena(ciccio); //PRINT THE ARENA INITIALLY WITH PLAYER IN THE MIDDLE
char mv,c;
int n;
set_input_mode(); //SETTING TERMINAL MODE TO EVITATE ECHOING OF THE PRESSED CHARACTERS AND TAKING INPUT WITHOUT THE '\n'
while(1){
spawnFood(ciccio); //TRY TO SPAWN FOOD IF IT'S POSSIBLE
read(0, &c, 1); //
if(c=='w'||c=='a'||c=='s'||c=='d') // IF IS PRESSED A VALID CHARACTER MOVE IN THAT DIRECTION
mv=c; //
system("clear");
switch (mv) {
case 'w':
move_up(ciccio);
break;
case 'a':
move_left(ciccio);
break;
case 'd':
move_right(ciccio);
break;
case 's':
move_down(ciccio);
break;
}
printArena(ciccio);
//printf("move%c\n",mv)
usleep(T);
}
reset_input_mode();
}
void printArena(game_t arena[][COLUMNS]){
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLUMNS;j++)
printf("%ls", arena[i][j]);
printf("\n");
}
//printf("tail:%d-%d/head:%d-%d",tailx,taily,x,y);
}
void spawnPlayer(game_t arena[][COLUMNS]){
x=10,y=40;
tailx=x,taily=y;
arena[x][y]=PH;
}
int isPlayer(game_t arena[][COLUMNS]){
if(arena[x][y]==PL||arena[x][y]==PR ||arena[x][y]==PU||arena[x][y]==PD||arena[x][y]==PH)
return 1;
else
return 0;
}
void move_up(game_t arena[][COLUMNS]){
x--;
int tmp=x;
if(isPlayer(arena)||arena[x][y]==B ||arena[x][y]==BP) //if player hits himself or the wall
exit(0); //exit from the game
if(arena[x][y]==FOOD){//if the player eats food
flag=0; // set flag=0 to respawn a new food
arena[x][y]=PH;
arena[x+1][y]=PU; //advance head and not the tail
}
else{
arena[x][y]=PH;
arena[x+1][y]=PU;
removeTail(arena);
}
}
void move_down(game_t arena[][COLUMNS]){
x++;
int tmp=x;
if(isPlayer(arena)||arena[x][y]==B ||arena[x][y]==BP)//if player hits himself
exit(0);
if(arena[x][y]==FOOD){
flag=0;
arena[x][y]=PH;
arena[x-1][y]=PD;
}
else{
arena[x][y]=PH;
arena[x-1][y]=PD;
removeTail(arena);
}
}
void move_left(game_t arena[][COLUMNS]){
y--;
if(isPlayer(arena)||arena[x][y]==B ||arena[x][y]==BP)//if player hits himself
exit(0);
if(arena[x][y]==FOOD){
flag=0;
arena[x][y]=PH;
arena[x][y+1]=PL;
}
else{
arena[x][y]=PH;
arena[x][y+1]=PL;
removeTail(arena);
}
}
void move_right(game_t arena[][COLUMNS]){
y++;
if(isPlayer(arena)||arena[x][y]==B ||arena[x][y]==BP)//if player hits himself
exit(0);
if(arena[x][y]==FOOD){
flag=0;
arena[x][y]=PH;
arena[x][y-1]=PR;
}
else{
arena[x][y]=PH;
arena[x][y-1]=PR;
removeTail(arena);
}
}
void set_input_mode (){
struct termios tattr;
char *name;
/* Make sure stdin is a terminal. */
if (!isatty (STDIN_FILENO))
{
fprintf (stderr, "Not a terminal.\n");
exit (EXIT_FAILURE);
}
/* Save the terminal attributes so we can restore them later. */
tcgetattr (STDIN_FILENO, &saved_attributes);
atexit (reset_input_mode);
/* Set the funny terminal modes. */
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO|O_NONBLOCK); /* Clear ICANON and ECHO. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}
void reset_input_mode () {
tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}
void helloScreen(){
initscr();
noecho();
curs_set(FALSE);
mvprintw(5,18,"Hello and welcome to the Snake (C-Version)!");
mvprintw(10,20,"Use the W-A-S-D-keys to move the player\n");
refresh();
sleep(3);
mvprintw(15,25,"Press any key to continue");
refresh();
if(getchar());
endwin();
}
void spawnFood(game_t arena[][COLUMNS]){
if(flag==0){
int nx=rand()%(ROWS-2)+1;
int ny=rand()%(COLUMNS-2)+1;
if(arena[nx][ny]==PH||arena[nx][ny]==PU||arena[nx][ny]==PD||arena[nx][ny]==PL||arena[nx][ny]==PR){
nx=rand()%(ROWS-2)+1;
ny=rand()%(COLUMNS-2)+1;
}
arena[nx][ny]=FOOD;
flag=1;
}
}
void removeTail(game_t arena[][COLUMNS]){
game_t t=arena[tailx][taily];
//printf("tail:--%ls--\n",arena[tailx][taily]);
if(arena[tailx][taily]==PU){
//printf("tail=up");
arena[tailx][taily]=S;
tailx-=1;
}
else
if(arena[tailx][taily]==PD){
//printf("tail=down");
arena[tailx][taily]=S;
tailx+=1;
}
else
if(arena[tailx][taily]==PL){
// printf("tail=left");
arena[tailx][taily]=S;
taily-=1;
}
else
if(arena[tailx][taily]==PR){
//printf("tail=right");
arena[tailx][taily]=S;
taily+=1;
}
}