Update 'du7/snake.c'
This commit is contained in:
parent
17ebf57d24
commit
68c7b7b549
48
du7/snake.c
48
du7/snake.c
@ -2,41 +2,41 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
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)); // Dynamické prideľovanie pamäte pre novú hadiu hlavu
|
||||||
|
|
||||||
new_head->x = x;
|
new_head->x = x;
|
||||||
new_head->y = y;
|
new_head->y = y;
|
||||||
new_head->next = snake;
|
new_head->next = snake; // Nastaví ďalší ukazovateľ novej hlavy hada na aktuálnu hlavu hada
|
||||||
return new_head;
|
return new_head;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake){
|
struct snake* remove_snake(struct snake* snake){ // Definujte funkciu na odstránenie posledného hadieho chvosta
|
||||||
if (snake == NULL) {
|
if (snake == NULL) { // Ak je had prázdny, vráti NULL
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct snake* current = snake;
|
struct snake* current = snake; // Nastaví aktuálny ukazovateľ na aktuálnu hlavu hada a ukazovateľ p na hodnotu NULL
|
||||||
struct snake* p = NULL;
|
struct snake* p = NULL;
|
||||||
|
|
||||||
while (current->next != NULL) {
|
while (current->next != NULL) { // Prejdite hadom, kým nedosiahnete posledný uzol
|
||||||
p = current;
|
p = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(current);
|
free(current); // Uvoľnite posledný uzol
|
||||||
|
|
||||||
if (p == NULL) {
|
if (p == NULL) { // Ak mal had iba jeden uzol, vráti hodnotu NULL
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
p->next = NULL;
|
p->next = NULL; // Nastaví ďalší ukazovateľ nového posledného uzla na NULL a vráti hada
|
||||||
return snake;
|
return snake;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void free_snake(struct snake* sn){
|
void free_snake(struct snake* sn){ // Definujem funkciu na oslobodenie celého hada
|
||||||
struct snake* current = sn;
|
struct snake* current = sn; // Nastaví aktuálny ukazovateľ na aktuálnu hlavu hada
|
||||||
while (current != NULL) {
|
while (current != NULL) { // Prejdem hadom a uvoľnite každý uzol
|
||||||
struct snake* next = current->next;
|
struct snake* next = current->next;
|
||||||
free(current);
|
free(current);
|
||||||
current = next;
|
current = next;
|
||||||
@ -44,33 +44,33 @@ void free_snake(struct snake* sn){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int is_snake(struct snake* snake,int x,int y){
|
int is_snake(struct snake* snake,int x,int y){ // Definujem funkciu na kontrolu, či je bod v hadovi
|
||||||
struct snake* current = snake;
|
struct snake* current = snake; // Nastaví aktuálny ukazovateľ na aktuálnu hlavu hada
|
||||||
while (current != NULL) {
|
while (current != NULL) { // Prejdite hadom a skontrolujte, či sa súradnice zhodujú
|
||||||
if (current->x == x && current->y == y) {
|
if (current->x == x && current->y == y) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0; // Ak sa súradnice nezhodujú, vráti 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int step_state(struct state* st){
|
int step_state(struct state* st){ // Definujem funkciu na aktualizáciu stavu hry po jednom kroku
|
||||||
int nx = (st->snake->x + st->sx);
|
int nx = (st->snake->x + st->sx); // Vypočítajem nové súradnice hlavy hada
|
||||||
int ny = (st->snake->y + st->sy);
|
int ny = (st->snake->y + st->sy);
|
||||||
if (nx < 0 || nx >= st->width || ny < 0 || ny >= st->height) {
|
if (nx < 0 || nx >= st->width || ny < 0 || ny >= st->height) { // Skontrolujem, či had narazí na stenu
|
||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake hit itself
|
// Check if snake hit itself
|
||||||
if (is_snake(st->snake, nx, ny)) {
|
if (is_snake(st->snake, nx, ny)) { // Skontrolujem, či sa had zasiahne sám
|
||||||
return END_SNAKE;
|
return END_SNAKE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake ate food
|
// Check if snake ate food
|
||||||
int food_eaten = 0;
|
int food_eaten = 0; // Skontrolujem, či had žerie jedlo
|
||||||
for (int a = 0; a < FOOD_COUNT; a++) {
|
for (int a = 0; a < FOOD_COUNT; a++) {
|
||||||
if (st->foodx[a] >= 0 && st->foodx[a] == nx && st->foody[a] == ny) {
|
if (st->foodx[a] >= 0 && st->foodx[a] == nx && st->foody[a] == ny) {
|
||||||
st->foodx[a] = -1;
|
st->foodx[a] = -1;
|
||||||
@ -81,7 +81,7 @@ int step_state(struct state* st){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if game ended
|
// Skontrolujem, či sa hra skončila
|
||||||
if (!food_eaten) {
|
if (!food_eaten) {
|
||||||
int food_left = 0;
|
int food_left = 0;
|
||||||
for (int a = 0; a < FOOD_COUNT; a++) {
|
for (int a = 0; a < FOOD_COUNT; a++) {
|
||||||
@ -93,9 +93,9 @@ int step_state(struct state* st){
|
|||||||
if (!food_left) {
|
if (!food_left) {
|
||||||
return END_FOOD;
|
return END_FOOD;
|
||||||
}
|
}
|
||||||
// Remove the last snake part
|
// Odstráňym poslednú časť hada
|
||||||
st->snake = remove_snake(st->snake);
|
st->snake = remove_snake(st->snake);
|
||||||
// Add new snake part
|
// Pridajem novú časť hada
|
||||||
st->snake = add_snake(st->snake, nx, ny);
|
st->snake = add_snake(st->snake, nx, ny);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user