-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
228 lines (189 loc) · 3.53 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
#include <stdlib.h>
#include <ncurses.h>
#include "include/snake.h"
struct point
{
int x;
int y;
};
struct _snake_s
{
struct point point;
direction dir;
struct _snake_s *next;
};
snake snake_create(int x, int y)
{
snake s = malloc(sizeof(struct _snake_s));
s->point.x = x;
s->point.y = y;
s->dir = LEFT;
s->next = NULL;
snake_eat(s);
snake_eat(s);
return s;
}
void snake_eat(snake s)
{
snake sp;
snake ns = malloc(sizeof(struct _snake_s));
ns->next = NULL;
sp = s;
while (sp->next != NULL)
{
sp = sp->next;
}
switch (sp->dir)
{
case LEFT:
ns->dir = LEFT;
ns->point.x = sp->point.x + 1;
ns->point.y = sp->point.y;
break;
case RIGHT:
ns->dir = RIGHT;
ns->point.x = sp->point.x - 1;
ns->point.y = sp->point.y;
break;
case UP:
ns->dir = UP;
ns->point.x = sp->point.x;
ns->point.y = sp->point.y +1;
break;
case DOWN:
ns->dir = DOWN;
ns->point.x = sp->point.x;
ns->point.y = sp->point.y - 1;
break;
}
sp->next = ns;
}
void act_on_direction(snake s)
{
switch (s->dir)
{
case LEFT:
s->point.x--;
break;
case RIGHT:
s->point.x++;
break;
case UP:
s->point.y--;
break;
case DOWN:
s->point.y++;
break;
}
}
void clear_last_section(snake s)
{
switch (s->dir)
{
case LEFT:
mvaddch(s->point.y, s->point.x+1, ' ');
break;
case RIGHT:
mvaddch(s->point.y, s->point.x-1, ' ');
break;
case UP:
mvaddch(s->point.y+1, s->point.x, ' ');
break;
case DOWN:
mvaddch(s->point.y-1, s->point.x, ' ');
break;
}
}
void snake_update(snake s)
{
snake sp;
direction prevdir, curdir;
sp = s;
prevdir = s->dir;
while (sp != NULL)
{
act_on_direction(sp); //moves and prints snake
//
mvaddch(sp->point.y, sp->point.x, ACS_CKBOARD); //
if(sp->next == NULL) //
clear_last_section(sp);
curdir = sp->dir; //updates directions
if (sp->dir != prevdir)
{
sp->dir = prevdir;
prevdir = curdir;
}
sp = sp->next;
}
}
bool snake_is_suicidal(snake s)
{
snake sp = s->next;
while (sp != NULL)
{
if((s->point.x == sp->point.x) && (s->point.y == sp->point.y)) return true;
sp = sp->next;
}
return false;
}
void snake_set_dir(snake s, direction dir)
{
s->dir = dir;
}
direction snake_get_dir(snake s)
{
return s->dir;
}
int snake_getx(snake s)
{
return s->point.x;
}
int snake_gety(snake s)
{
return s->point.y;
}
snake snake_destroy(snake s)
{
snake sp;
while (s != NULL)
{
sp = s;
s = s->next;
free(sp);
}
return s;
}
//Fruit
fruit fruit_create(int x, int y)
{
fruit f = malloc(sizeof(struct point));
f->x = x;
f->y = y;
return f;
}
void fruit_update(fruit f)
{
mvaddch(f->y,f->x, ACS_DIAMOND);
}
int fruit_getx(fruit f)
{
return f->x;
}
int fruit_gety(fruit f)
{
return f->y;
}
void fruit_setx(fruit f, int x)
{
f->x = x;
}
void fruit_sety(fruit f, int y)
{
f->y = y;
}
fruit fruit_destroy(fruit f)
{
free(f);
f = NULL;
return f;
}