-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.c
84 lines (70 loc) · 1.65 KB
/
food.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
#include <stddef.h>
#include "food.h"
Food* food_init()
{
Food *food = (Food*) malloc(sizeof(Food));
if (food == NULL) return NULL;
food->head = NULL;
return food;
}
void food_free(Food *food)
{
Node *current_node = food->head;
while (current_node != NULL)
{
Node *node_to_free = current_node;
current_node = current_node->next;
free(node_to_free);
}
free(food);
}
bool food_add(Food *food, int y, int x)
{
Node *new_food = (Node*) malloc(sizeof(Node));
if (new_food == NULL) return false;
new_food->y = y;
new_food->x = x;
new_food->next = NULL;
Node *current_node = food->head;
if (current_node == NULL) {
current_node = new_food;
food->head = new_food;
return true;
}
while (current_node->next != NULL) current_node = current_node->next;
current_node->next = new_food;
return true;
}
bool food_remove(Food *food, int y, int x)
{
Node *before_node = food->head;
if (before_node->x == x && before_node->y == y) {
food->head = food->head->next;
free(before_node);
return true;
}
Node *current_node = before_node->next;
while (current_node != NULL)
{
if (current_node->x == x && current_node->y == y) {
before_node->next = current_node->next;
free(current_node);
return true;
} else {
before_node = current_node;
current_node = current_node->next;
}
}
return false;
}
void render_food(Food *food)
{
Node *current_node = food->head;
while (current_node != NULL)
{
attron(COLOR_PAIR(FOOD_PAIR));
mvaddch(current_node->y, current_node->x, FOOD_CHAR);
attroff(COLOR_PAIR(FOOD_PAIR));
current_node = current_node->next;
}
}