111111
This commit is contained in:
parent
e48d2bf689
commit
52d7eeffe3
34
cv9/snake.c
34
cv9/snake.c
@ -1,6 +1,7 @@
|
|||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Pridá novú časť hada na danú pozíciu a vráti novú hlavu hada
|
||||||
struct snake* add_snake(struct snake* snake, int x, int y) {
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
||||||
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
|
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
|
||||||
new_head->x = x;
|
new_head->x = x;
|
||||||
@ -9,29 +10,7 @@ struct snake* add_snake(struct snake* snake, int x, int y) {
|
|||||||
return new_head;
|
return new_head;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*struct snake* remove_snake(struct snake* snake) {
|
// Odstráni poslednú časť hada a vráti novú hlavu hada
|
||||||
struct snake* temp = snake;
|
|
||||||
while(temp->next->next != NULL) {
|
|
||||||
temp = temp->next;
|
|
||||||
}
|
|
||||||
free(temp->next);
|
|
||||||
temp->next = NULL;
|
|
||||||
return snake;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*struct snake* remove_snake(struct snake* snake) {
|
|
||||||
if (snake == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
struct snake* temp = snake;
|
|
||||||
while(temp->next->next != NULL) {
|
|
||||||
temp = temp->next;
|
|
||||||
}
|
|
||||||
free(temp->next);
|
|
||||||
temp->next = NULL;
|
|
||||||
return snake;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake) {
|
struct snake* remove_snake(struct snake* snake) {
|
||||||
if (snake == NULL || snake->next == NULL) {
|
if (snake == NULL || snake->next == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -45,8 +24,7 @@ struct snake* remove_snake(struct snake* snake) {
|
|||||||
return snake;
|
return snake;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zistí, či sú dané súradnice súčasťou hada
|
||||||
|
|
||||||
int is_snake(struct snake* snake, int x, int y) {
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
while(snake != NULL) {
|
while(snake != NULL) {
|
||||||
if(snake->x == x && snake->y == y) {
|
if(snake->x == x && snake->y == y) {
|
||||||
@ -57,6 +35,7 @@ int is_snake(struct snake* snake, int x, int y) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uvoľní pamäť všetkých častí hada
|
||||||
void free_snake(struct snake* snake) {
|
void free_snake(struct snake* snake) {
|
||||||
struct snake* temp;
|
struct snake* temp;
|
||||||
while(snake != NULL) {
|
while(snake != NULL) {
|
||||||
@ -66,18 +45,22 @@ void free_snake(struct snake* snake) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zmení stav hry podľa pravidiel hry
|
||||||
int step_state(struct state* state) {
|
int step_state(struct state* state) {
|
||||||
int new_x = state->snake->x + state->sx;
|
int new_x = state->snake->x + state->sx;
|
||||||
int new_y = state->snake->y + state->sy;
|
int new_y = state->snake->y + state->sy;
|
||||||
|
|
||||||
|
// Ak je nová pozícia mimo hracej plochy, vráti END_WALL
|
||||||
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
|
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
|
||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak je nová pozícia na hadovi, vráti END_SNAKE
|
||||||
if(is_snake(state->snake, new_x, new_y)) {
|
if(is_snake(state->snake, new_x, new_y)) {
|
||||||
return END_SNAKE;
|
return END_SNAKE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak je nová pozícia na jedle, označí jedlo ako zjedené a pridá novú časť hada
|
||||||
for(int i = 0; i < FOOD_COUNT; i++) {
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
||||||
if(state->foodx[i] == new_x && state->foody[i] == new_y) {
|
if(state->foodx[i] == new_x && state->foody[i] == new_y) {
|
||||||
state->foodx[i] = -1;
|
state->foodx[i] = -1;
|
||||||
@ -92,6 +75,7 @@ int step_state(struct state* state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak je nová pozícia voľná, pridá novú časť hada a odstráni poslednú časť
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
state->snake = remove_snake(state->snake);
|
state->snake = remove_snake(state->snake);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user