Repository navigation
Expand file tree
/
Copy pathsnake_linux.c
More file actions
326 lines (312 loc) · 7.15 KB
/
Copy pathsnake_linux.c
File metadata and controls
326 lines (312 loc) · 7.15 KB
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
// https://zxbcw.cn/post/218247/
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define X 40
#define Y 20
// char HEAD = '@'; // The shape of snake head
// char BODY = 'O'; // The shape of snake body
#define HEAD '@' // The shape of snake head
#define BODY 'O' // The shape of snake body
char a[Y][X] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
char *p[Y * X] = {&a[0][3], &a[0][2], &a[0][1],
&a[0][0]}; // p[0] stand for snake head
int n = 3; // The length of snake body (without head)
int i, j;
int direction = 1; // 1.right;2.up;3.left;4.down;-1.exit
int delay = 200 * 1000; // delay 0.2s(200ms)
_Bool isPause = 0;
#define moveBody() \
{ \
*p[n] = 0; \
for (i = n; i > 0; i--) \
{ \
p[i] = p[i - 1]; /* per part goes to the address of the next part \
ofbody*/ \
} \
*p[0] = BODY; /* The First part of snake body come to snake head*/ \
}
void moveRight()
{
moveBody();
p[0] = p[0] + 1; // Move snake head
*p[0] = HEAD; // change the char of new head(new address)'s shape to HEAD
}
void moveLeft()
{
moveBody();
p[0] = p[0] - 1;
*p[0] = HEAD;
}
void moveDown()
{
moveBody();
p[0] = p[0] + X;
*p[0] = HEAD;
}
void moveUp()
{
moveBody();
p[0] = p[0] - X;
*p[0] = HEAD;
}
void show()
{
system("clear");
printf("Your Score is:%d\n", n - 3);
for (i = 0; i < X; i++)
printf("_");
printf("\n");
for (i = 0; i < Y; i++)
{
// printf("|");
for (j = 0; j < X; j++)
{
printf("%c", (a[i][j] == 0) ? ' ' : a[i][j]);
}
// printf("|");
printf("\n");
}
for (i = 0; i < X; i++)
printf("-");
printf("\nw,s,a,d->Up Down Left Right;\nj,k->Speed Up/Down;\nESC: Exit\n");
}
void randomApple() // Random
{
srand(time(NULL));
do
{
i = rand() % Y;
j = rand() % X;
// if random location is 0 ->*;else find again and again
} while (a[i][j] != 0);
a[i][j] = '*';
}
void canEat()
{
switch (direction)
{
// Right
case 1: {
if (*(p[0] + 1) == '*')
{
n++; // length++
p[n] = p[n - 1];
randomApple();
}
break;
}
// Up
case 2: {
if (*(p[0] - X) == '*')
{
n++; // length++
p[n] = p[n - 1];
randomApple();
}
break;
}
// Left
case 3: {
if (*(p[0] - 1) == '*')
{
n++; // length++
p[n] = p[n - 1];
randomApple();
}
break;
}
// Down
case 4: {
if (*(p[0] + X) == '*')
{
n++; // length++
p[n] = p[n - 1];
randomApple();
}
break;
}
}
}
void isFail()
{
if (p[0] < &a[0][0] ||
p[0] > &a[Y - 1][X - 1]) // snake is not in the matrix
{
printf("fail!\n");
direction = -1;
}
else
{
switch (direction)
{
// Right
case 1: {
for (i = n; i > 0; i--)
{
if ((p[0] + 1) == p[i]) // Right of the head is body
{
printf("fail!\n");
direction = -1;
}
}
break;
}
// Up
case 2: {
for (i = n; i > 0; i--)
{
if ((p[0] - X) == p[i]) // Up of the head is body
{
printf("fail!\n");
direction = -1;
}
}
break;
}
// Left
case 3: {
for (i = n; i > 0; i--)
{
if ((p[0] - 1) == p[i]) // Left of the head is body
{
printf("fail!\n");
direction = -1;
}
}
break;
}
// Down
case 4: {
for (i = n; i > 0; i--)
{
if ((p[0] + X) == p[i]) // Down of the head is body
{
printf("fail!\n");
direction = -1;
}
}
break;
}
}
}
}
void *key(void *arg) // Direction Control:w,s,a,d-->Up Down Left Right
{
char k;
while (1)
{
k = getchar();
switch (k)
{
case 'w': // Up
{
if (direction != 4)
direction = 2;
break;
}
case 's': // Down
{
if (direction != 2)
direction = 4;
break;
}
case 'a': // Left
{
if (direction != 1)
direction = 3;
break;
}
case 'd': // Right
{
if (direction != 3)
direction = 1;
break;
}
case 'j': // SpeedUp
{
delay = delay * 4 / 5;
break;
}
case 'k': // SpeedDown
{
delay = delay * 5 / 4;
break;
}
case 27: // ESC
{
printf("Exit!\n");
isPause = 0;
direction = -1;
pthread_exit(NULL);
break;
}
case ' ': // Space
{
if (isPause)
{
printf("Continue!\n");
}
else
{
printf("Pause!\n");
}
isPause = !isPause;
break;
}
}
}
}
int main()
{
system("stty -icanon");
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
// set pthread_attr to detached
pthread_t tid;
pthread_create(&tid, &attr, key, NULL); // Create pthread to capture input
randomApple();
while (1)
{
show();
do
{
usleep(delay);
} while (isPause);
isFail(); // Judge if will eat self
canEat(); // Judge if will eat *
switch (direction) // choose which direction to move
{
case 1: // Right
{
moveRight();
break;
}
case 2: // Up
{
moveUp();
break;
}
case 3: // Left
{
moveLeft();
break;
}
case 4: // Down
{
moveDown();
break;
}
case -1: // Exit
{
printf("Your Final Score is:%d", n - 3);
return -1;
break;
}
}
}
return 0;
}