du5
This commit is contained in:
parent
d0ff62ffc5
commit
015b6a9dfa
94
du5/program.c
Normal file
94
du5/program.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include "snake.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Pridá nový diel hada na začiatok (hlavu)
|
||||||
|
struct snake* add_snake(struct snake* snake, int x, int y){
|
||||||
|
struct snake* new_head = malloc(sizeof(struct snake));
|
||||||
|
if(!new_head){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new_head->x = x;
|
||||||
|
new_head->y = y;
|
||||||
|
new_head->next = snake;
|
||||||
|
return new_head;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Odstráni posledný diel hada (chvost)
|
||||||
|
struct snake* remove_snake(struct snake* snake){
|
||||||
|
if(!snake || !snake->next){
|
||||||
|
free(snake);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct snake* current = snake;
|
||||||
|
while (current->next && current->next->next){
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
free(current->next);
|
||||||
|
current->next = NULL;
|
||||||
|
return snake;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zistí, či sa na daných súradniciach nachádza časť hada
|
||||||
|
int is_snake(struct snake* snake, int x, int y){
|
||||||
|
while(snake){
|
||||||
|
if(snake->x == x && snake->y == y){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
snake = snake->next;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uvoľní pamäť všetkých častí hada
|
||||||
|
void free_snake(struct snake* sn){
|
||||||
|
while(sn){
|
||||||
|
struct snake* next = sn->next;
|
||||||
|
free(sn);
|
||||||
|
sn = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spraví jeden krok hry – aktualizuje stav podľa pohybu
|
||||||
|
int step_state(struct state* state){
|
||||||
|
if(!state || !state->snake) return END_USER;
|
||||||
|
|
||||||
|
int new_x = state->snake->x + state->sx;
|
||||||
|
int new_y = state->snake->y + state->sy;
|
||||||
|
|
||||||
|
// Kontrola nárazu do steny
|
||||||
|
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height){
|
||||||
|
return END_WALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kontrola nárazu do seba samého
|
||||||
|
if(is_snake(state->snake, new_x, new_y)){
|
||||||
|
return END_SNAKE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kontrola, či had zjedol jedlo
|
||||||
|
for(int i = 0; i < FOOD_COUNT; i++){
|
||||||
|
if(state->foodx[i] == new_x && state->foody[i] == new_y){
|
||||||
|
// Označí jedlo ako zjedené
|
||||||
|
state->foodx[i] = -1;
|
||||||
|
state->foody[i] = -1;
|
||||||
|
// Pridá nový diel hada na pozíciu jedla
|
||||||
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
|
|
||||||
|
// Skontroluje, či ešte zostalo jedlo
|
||||||
|
int food_left = 0;
|
||||||
|
for(int j = 0; j < FOOD_COUNT; j++){
|
||||||
|
if(state->foodx[j] >= 0 && state->foody[j] >= 0){
|
||||||
|
food_left = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return food_left ? END_CONTINUE : END_FOOD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bežný pohyb – had sa pohne o jedno políčko
|
||||||
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
|
state->snake = remove_snake(state->snake);
|
||||||
|
return END_CONTINUE;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user