game
This commit is contained in:
parent
257bf01c5c
commit
bf9c2bf8c6
61
du4/game.c
Normal file
61
du4/game.c
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include "snake.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
||||||
|
struct snake* novy_uzol = calloc(1, sizeof(struct snake));
|
||||||
|
novy_uzol->x = x;
|
||||||
|
novy_uzol->y = y;
|
||||||
|
novy_uzol->next = snake;
|
||||||
|
return novy_uzol;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct snake* remove_snake(struct snake* hlava) {
|
||||||
|
if (hlava == NULL) return NULL;
|
||||||
|
if (hlava->next == NULL) {
|
||||||
|
free(hlava);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
hlava->next = remove_snake(hlava->next);
|
||||||
|
return hlava;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
|
for (struct snake* clanok = snake; clanok != NULL; clanok = clanok->next) {
|
||||||
|
if (clanok->x == x && clanok->y == y) return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_snake(struct snake* sn) {
|
||||||
|
if (sn == NULL) return;
|
||||||
|
free_snake(sn->next);
|
||||||
|
free(sn);
|
||||||
|
}
|
||||||
|
|
||||||
|
int step_state(struct state* st) {
|
||||||
|
int next_x = st->snake->x + st->sx;
|
||||||
|
int next_y = st->snake->y + st->sy;
|
||||||
|
|
||||||
|
if (next_x < 0 || next_x >= st->width || next_y < 0 || next_y >= st->height)
|
||||||
|
return END_WALL;
|
||||||
|
|
||||||
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
|
if (st->foodx[i] == next_x && st->foody[i] == next_y) {
|
||||||
|
st->foodx[i] = -1;
|
||||||
|
st->foody[i] = -1;
|
||||||
|
st->snake = add_snake(st->snake, next_x, next_y);
|
||||||
|
int zostava_jedlo = 0;
|
||||||
|
for (int j = 0; j < FOOD_COUNT; j++) {
|
||||||
|
if (st->foodx[j] >= 0) zostava_jedlo = 1;
|
||||||
|
}
|
||||||
|
if (!zostava_jedlo) return END_FOOD;
|
||||||
|
return END_CONTINUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
st->snake = remove_snake(st->snake);
|
||||||
|
if (is_snake(st->snake, next_x, next_y))
|
||||||
|
return END_SNAKE;
|
||||||
|
st->snake = add_snake(st->snake, next_x, next_y);
|
||||||
|
return END_CONTINUE;
|
||||||
|
}
|
||||||
36
du4/game.h
Normal file
36
du4/game.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef snake_h_INCLUDED
|
||||||
|
#define snake_h_INCLUDED
|
||||||
|
|
||||||
|
#define FOOD_COUNT 20
|
||||||
|
|
||||||
|
struct snake {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
struct snake* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum endgame {
|
||||||
|
END_CONTINUE = 0,
|
||||||
|
END_WALL,
|
||||||
|
END_SNAKE,
|
||||||
|
END_FOOD,
|
||||||
|
END_USER
|
||||||
|
};
|
||||||
|
|
||||||
|
struct state {
|
||||||
|
struct snake* snake;
|
||||||
|
int foodx[FOOD_COUNT];
|
||||||
|
int foody[FOOD_COUNT];
|
||||||
|
int sx;
|
||||||
|
int sy;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct snake* add_snake(struct snake* snake, int x, int y);
|
||||||
|
struct snake* remove_snake(struct snake* snake);
|
||||||
|
int is_snake(struct snake* snake, int x, int y);
|
||||||
|
void free_snake(struct snake* sn);
|
||||||
|
int step_state(struct state* state);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user