2021-10-17 20:56:06 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
typedef struct point {
|
|
|
|
double x;
|
|
|
|
double y;
|
|
|
|
struct point *next;
|
|
|
|
} point;
|
|
|
|
|
|
|
|
typedef struct anaconda {
|
|
|
|
int score;
|
|
|
|
double rot;
|
|
|
|
struct point *chain;
|
|
|
|
} anaconda;
|
|
|
|
|
|
|
|
Display *d;
|
|
|
|
Window w;
|
|
|
|
XEvent e;
|
|
|
|
int s;
|
|
|
|
GC gc;
|
|
|
|
|
|
|
|
void xinit(void) {
|
|
|
|
d = XOpenDisplay(NULL);
|
|
|
|
s = DefaultScreen(d);
|
|
|
|
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
|
|
|
|
BlackPixel(d, s), WhitePixel(d, s));
|
|
|
|
XSelectInput(d, w, ExposureMask | KeyPressMask | PointerMotionMask);
|
|
|
|
XMapWindow(d, w);
|
|
|
|
gc = XCreateGC(d, w, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thanks to
|
|
|
|
* https://stackoverflow.com/questions/3838329/how-can-i-check-if-two-segments-intersect/9997374#9997374
|
|
|
|
* for functions ccw and intersect
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ccw(point *a, point *b, point *c) {
|
|
|
|
return (c->y - a->y) * (b->x - a->x) >
|
|
|
|
(b->y - a->y) * (c->x - a->x);
|
|
|
|
}
|
|
|
|
|
|
|
|
int intersect(point *a, point *b, point *c, point *d) {
|
|
|
|
return (ccw(a, c, d) != ccw(b, c, d)) && (ccw(a, b, c) != ccw(a, b, d));
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:03:02 -05:00
|
|
|
int randrange(int b, int s) {
|
|
|
|
return rand() % (b - s + 1) + s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int eucliddist(point *a, point *b) {
|
|
|
|
return sqrt(pow(a->x - b->x, 2) + pow(a->y - b->y, 2));
|
|
|
|
}
|
|
|
|
|
2021-10-17 20:56:06 -05:00
|
|
|
point *mkPoint(double x, double y) {
|
2021-10-18 20:03:02 -05:00
|
|
|
point *ret = malloc(sizeof *ret);
|
2021-10-17 20:56:06 -05:00
|
|
|
|
|
|
|
ret->x = x;
|
|
|
|
ret->y = y;
|
|
|
|
ret->next = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
point *rotate(point *p, double rot) {
|
|
|
|
double rad = rot * M_PI/180;
|
|
|
|
|
|
|
|
return mkPoint(
|
|
|
|
p->x * cos(rad) - p->y * sin(rad),
|
|
|
|
p->x * sin(rad) + p->y * cos(rad)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendPoint(point *dest, point *origin) {
|
|
|
|
while(dest->next) dest = dest->next;
|
|
|
|
dest->next = origin;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:03:02 -05:00
|
|
|
int updateAnaconda(anaconda *anaconda, int w, int h, point *apple) {
|
2021-10-17 20:56:06 -05:00
|
|
|
point *prev = anaconda->chain;
|
|
|
|
point *temp = rotate(mkPoint(10, 0), anaconda->rot);
|
|
|
|
point *new = mkPoint(
|
|
|
|
prev->x + temp->x,
|
|
|
|
prev->y + temp->y
|
|
|
|
);
|
|
|
|
new->next = prev;
|
|
|
|
anaconda->chain = new;
|
|
|
|
|
|
|
|
free(temp);
|
|
|
|
|
2021-10-18 20:13:49 -05:00
|
|
|
if(eucliddist(new, apple) <= 30) {
|
|
|
|
anaconda->score += 30;
|
|
|
|
apple->x = randrange(20, w / 2);
|
|
|
|
apple->y = randrange(20, h / 2);
|
|
|
|
} else {
|
|
|
|
point *traverser = new;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(traverser->next) {
|
|
|
|
if(traverser->next->next) {
|
|
|
|
traverser = traverser->next;
|
|
|
|
} else break;
|
2021-10-17 20:56:06 -05:00
|
|
|
} else break;
|
2021-10-18 20:13:49 -05:00
|
|
|
}
|
|
|
|
free(traverser->next);
|
|
|
|
traverser->next = NULL;
|
2021-10-17 20:56:06 -05:00
|
|
|
}
|
2021-10-18 18:52:33 -05:00
|
|
|
|
|
|
|
point *top = anaconda->chain;
|
|
|
|
point *ptr = top;
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
if(ptr->next) ptr = ptr->next;
|
|
|
|
else return 1; /* we're fine, the snake is too short to intersect itself */
|
|
|
|
}
|
|
|
|
|
|
|
|
while(ptr->next) {
|
|
|
|
if(intersect(top, top->next, ptr, ptr->next)) return 0;
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2021-10-18 20:03:02 -05:00
|
|
|
|
|
|
|
if(
|
|
|
|
new->x >= w || new->x <= 0 ||
|
|
|
|
new->y >= h || new->y <= 0
|
|
|
|
) return 0;
|
|
|
|
|
|
|
|
if(eucliddist(new, apple) <= 30) {
|
|
|
|
anaconda->score += 30;
|
2021-10-18 20:13:49 -05:00
|
|
|
apple->x = randrange(20, w / 2);
|
|
|
|
apple->y = randrange(20, h / 2);
|
2021-10-18 20:03:02 -05:00
|
|
|
}
|
|
|
|
|
2021-10-18 18:52:33 -05:00
|
|
|
return 1;
|
2021-10-17 20:56:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
point *generateChain(int length) {
|
|
|
|
point *ret = mkPoint(100, 100);
|
|
|
|
point *head = ret;
|
|
|
|
|
|
|
|
for(int i = 1; i < length - 1; i++) {
|
|
|
|
ret->next = mkPoint(
|
|
|
|
10 * i + 100,
|
|
|
|
5 * i + 100
|
|
|
|
);
|
|
|
|
ret = ret->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
anaconda *mkAnaconda(point *point, double rot) {
|
2021-10-18 20:03:02 -05:00
|
|
|
anaconda *ret = malloc(sizeof *ret);
|
2021-10-17 20:56:06 -05:00
|
|
|
|
|
|
|
ret->chain = point;
|
|
|
|
ret->rot = rot;
|
|
|
|
ret->score = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2021-10-18 18:52:33 -05:00
|
|
|
anaconda *anaconda = mkAnaconda(generateChain(30), 0);
|
2021-10-17 20:56:06 -05:00
|
|
|
xinit();
|
2021-10-18 20:13:49 -05:00
|
|
|
srand(time(0));
|
2021-10-18 20:03:02 -05:00
|
|
|
int width = DisplayWidth(d, s);
|
|
|
|
int height = DisplayHeight(d, s);
|
2021-10-18 20:13:49 -05:00
|
|
|
point *apple = mkPoint(randrange(20, width / 2 - 20), randrange(20, height / 2 - 20));
|
2021-10-17 20:56:06 -05:00
|
|
|
while(1) {
|
2021-10-18 20:03:02 -05:00
|
|
|
if(!updateAnaconda(anaconda, width, height, apple)) {
|
2021-10-18 18:52:33 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2021-10-17 20:56:06 -05:00
|
|
|
XClearWindow(d, w);
|
|
|
|
point *ptr = anaconda->chain;
|
|
|
|
while(ptr->next) {
|
|
|
|
XDrawLine(d, w, gc, ptr->x, ptr->y, ptr->next->x, ptr->next->y);
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
2021-10-18 20:13:49 -05:00
|
|
|
printf("%f %f\n", apple->x, apple->y);
|
2021-10-18 20:03:02 -05:00
|
|
|
XDrawArc(d, w, gc, apple->x - (5/2), apple->y - (5/2), 5, 5, 0, 360*64);
|
2021-10-17 20:56:06 -05:00
|
|
|
while(XPending(d)) {
|
|
|
|
XNextEvent(d, &e);
|
|
|
|
switch(e.type) {
|
|
|
|
case KeyPress:
|
|
|
|
switch(e.xkey.keycode) {
|
|
|
|
case 113: /* left arrow key */
|
|
|
|
anaconda->rot += 10;
|
|
|
|
break;
|
|
|
|
case 114: /* right arrow key */
|
|
|
|
anaconda->rot -= 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|