-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
133 lines (124 loc) · 3.28 KB
/
snake.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Created by jiricmi1 on 5/10/23.
//
#include "snake.h"
#include <stdlib.h>
#include <stdio.h>
/* mallocs body part and set basic values */
struct body_part_t *malloc_body() {
struct body_part_t *body = (struct body_part_t *) malloc(sizeof(struct body_part_t));
if (body == NULL) {
fprintf(stderr, "Cannot malloc body\n");
exit(1);
}
body->next = NULL;
body->previous = NULL;
return body;
}
/* mallocs snake struct and set basic values */
struct snake_t *malloc_snake() {
struct snake_t *snake = (struct snake_t *) malloc(sizeof(struct snake_t));
if (snake == NULL) {
fprintf(stderr, "Cannot malloc snake\n");
exit(1);
}
snake->tail = NULL;
snake->head = NULL;
snake->size = 0;
snake->direction = 0;
snake->color = 0;
return snake;
}
/* move snake by one to new coords */
int move_snake(struct snake_t *snake, unsigned int x, unsigned int y) {
add_body_part(snake, x, y);
return remove_body_part(snake);
}
/* add new node to linked list */
void add_body_part(struct snake_t *snake, unsigned int x, unsigned int y) {
struct body_part_t *new_head = malloc_body();
new_head->x = x;
new_head->y = y;
if (snake->head == NULL) {
snake->head = new_head;
snake->tail = new_head;
} else {
struct body_part_t *head = snake->head;
new_head->next = head;
head->previous = new_head;
snake->head = new_head;
}
snake->size += 1;
}
/* removes body part from part */
int remove_body_part(struct snake_t *snake) {
int x, y;
if (snake->tail != NULL) {
struct body_part_t *tail = snake->tail;
x = tail->x;
y = tail->y;
if (snake->tail == snake->head) {
snake->head = NULL;
snake->tail = NULL;
free(tail);
} else {
struct body_part_t *new_tail = snake->tail->previous;
new_tail->next = NULL;
snake->tail = new_tail;
free(tail);
}
snake->size -= 1;
return x + 19 * y;
} else {
fprintf(stderr, "Warning: empty snake\n");
return -1;
}
}
/* create snake on possition on game start */
struct snake_t *create_snake(const unsigned short int color, unsigned int x, unsigned int y) {
struct snake_t *snake = malloc_snake();
snake->color = color;
for (unsigned int i = 0; i < 3; ++i) {
add_body_part(snake, x + i, y);
}
return snake;
}
/* empty linked list andd remove snake */
void remove_snake(struct snake_t *snake) {
struct body_part_t *head = snake->head;
struct body_part_t *next;
while (head != NULL) {
next = head->next;
free(head);
head = next;
}
free(snake);
}
/* add direction to coords */
void direction_func(unsigned int *x, unsigned int *y, int direction) {
switch (direction) {
case 0:
*x += 1;
break;
case 1:
*y += 1;
break;
case 2:
*x -= 1;
break;
case 3:
*y -= 1;
break;
default:
fprintf(stderr, "Error: wrong direction\n");
exit(1);
}
if (*x == -1) {
*x = 18;
}
if (*y == -1) {
*y = 9;
}
*x = (*x) % 19;
*y = (*y) % 10;
}