funguje
This commit is contained in:
parent
3720a2eb68
commit
900460348b
85
cv9/snake.c
85
cv9/snake.c
@ -1,66 +1,69 @@
|
|||||||
|
Samozrejme, tu je upravený program v jazyku C:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include "snake.h"
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define FOOD_COUNT 5
|
||||||
|
|
||||||
|
struct snake {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
struct snake* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
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* add_snake(struct snake* snake, int x, int y) {
|
||||||
// Allocate memory for the new snake part
|
struct snake* head = (struct snake*)malloc(sizeof(struct snake));
|
||||||
struct snake* new_part = (struct snake*)malloc(sizeof(struct snake));
|
if (head == NULL) {
|
||||||
if (new_part == NULL) {
|
return NULL;
|
||||||
return NULL; // Memory allocation failed
|
|
||||||
}
|
}
|
||||||
|
head->x = x;
|
||||||
// Set the coordinates of the new part
|
head->y = y;
|
||||||
new_part->x = x;
|
head->next = snake;
|
||||||
new_part->y = y;
|
return head;
|
||||||
|
|
||||||
// Set the next pointer of the new part to the current head of the snake
|
|
||||||
new_part->next = snake;
|
|
||||||
|
|
||||||
// Return the new head of the snake
|
|
||||||
return new_part;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake) {
|
struct snake* remove_snake(struct snake* snake) {
|
||||||
// Check if the snake is empty
|
|
||||||
if (snake == NULL) {
|
if (snake == NULL) {
|
||||||
return NULL; // Snake is empty, no part to remove
|
return NULL;
|
||||||
}
|
}
|
||||||
|
struct snake* next = snake->next;
|
||||||
// Save the next part of the snake
|
|
||||||
struct snake* next_part = snake->next;
|
|
||||||
|
|
||||||
// Free the current head of the snake
|
|
||||||
free(snake);
|
free(snake);
|
||||||
|
return next;
|
||||||
// Return the new head of the snake
|
|
||||||
return next_part;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_snake(struct snake* snake, int x, int y) {
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
// Traverse the snake
|
|
||||||
while (snake != NULL) {
|
while (snake != NULL) {
|
||||||
// Check if the coordinates match
|
|
||||||
if (snake->x == x && snake->y == y) {
|
if (snake->x == x && snake->y == y) {
|
||||||
return 1; // Coordinates are part of the snake
|
return 1;
|
||||||
}
|
}
|
||||||
// Move to the next part of the snake
|
|
||||||
snake = snake->next;
|
snake = snake->next;
|
||||||
}
|
}
|
||||||
// Coordinates are not part of the snake
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_snake(struct snake* sn) {
|
void free_snake(struct snake* snake) {
|
||||||
// Traverse the snake
|
while (snake != NULL) {
|
||||||
while (sn != NULL) {
|
struct snake* next = snake->next;
|
||||||
// Save the next part of the snake
|
free(snake);
|
||||||
struct snake* next_part = sn->next;
|
snake = next;
|
||||||
|
|
||||||
// Free the current part of the snake
|
|
||||||
free(sn);
|
|
||||||
|
|
||||||
// Move to the next part of the snake
|
|
||||||
sn = next_part;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Place your testing code here
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user