clean up updateAnaconda()
This commit is contained in:
parent
7f7a694486
commit
c1434abc40
31
c/anaconda.c
31
c/anaconda.c
|
@ -82,13 +82,14 @@ void appendPoint(point *dest, point *origin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) {
|
int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) {
|
||||||
point *prev = anaconda->chain;
|
point *temp, *new, *ptr;
|
||||||
point *temp = rotate(mkPoint(10, 0), anaconda->rot);
|
new = anaconda->chain;
|
||||||
point *new = mkPoint(
|
temp = rotate(mkPoint(10, 0), anaconda->rot);
|
||||||
prev->x + temp->x,
|
new = mkPoint(
|
||||||
prev->y + temp->y
|
new->x + temp->x,
|
||||||
|
new->y + temp->y
|
||||||
);
|
);
|
||||||
new->next = prev;
|
new->next = anaconda->chain;
|
||||||
anaconda->chain = new;
|
anaconda->chain = new;
|
||||||
|
|
||||||
free(temp);
|
free(temp);
|
||||||
|
@ -98,28 +99,26 @@ int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) {
|
||||||
apple->x = randrange(20, w / 2);
|
apple->x = randrange(20, w / 2);
|
||||||
apple->y = randrange(20, h / 2);
|
apple->y = randrange(20, h / 2);
|
||||||
} else {
|
} else {
|
||||||
point *traverser = new;
|
ptr = new;
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
if(traverser->next) {
|
if(ptr->next) {
|
||||||
if(traverser->next->next) {
|
if(ptr->next->next) ptr = ptr->next;
|
||||||
traverser = traverser->next;
|
else break;
|
||||||
} else break;
|
|
||||||
} else break;
|
} else break;
|
||||||
}
|
}
|
||||||
free(traverser->next);
|
free(ptr->next);
|
||||||
traverser->next = NULL;
|
ptr->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
point *top = anaconda->chain;
|
ptr = anaconda->chain;
|
||||||
point *ptr = top;
|
|
||||||
for(int i = 0; i < 3; i++) {
|
for(int i = 0; i < 3; i++) {
|
||||||
if(ptr->next) ptr = ptr->next;
|
if(ptr->next) ptr = ptr->next;
|
||||||
else return 1; /* we're fine, the snake is too short to intersect itself */
|
else return 1; /* we're fine, the snake is too short to intersect itself */
|
||||||
}
|
}
|
||||||
|
|
||||||
while(ptr->next) {
|
while(ptr->next) {
|
||||||
if(intersect(top, top->next, ptr, ptr->next)) return 0;
|
if(intersect(new, new->next, ptr, ptr->next)) return 0;
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue