-
Notifications
You must be signed in to change notification settings - Fork 1
/
lists.c
69 lines (63 loc) · 1.84 KB
/
lists.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
/*funçõs que trabalham com listas encadeadas*/
#include "lists.h"
//insere um novo sprite no inicio da lista encadeada
void push(SPRITE **head_ref, SPRITE s) {
SPRITE *node = (SPRITE *) malloc(sizeof(SPRITE));
*node = s;
node->next = *head_ref;
*head_ref = node;
}
//imprime uma lista de sprites na janela
void print_list(WINDOW *w, SPRITE *head){
SPRITE *current = head;
while(current != NULL){
if (current->alive && (current->position.x != current->position.last_x || current->position.y != current->position.last_y)) {
mvwaddch(w, current->position.y, current->position.x, current->representation);
}
current = current->next;
}
}
//imprime todos os sprites contidos na lista na tela
void print_map(WINDOW *w, struct sprite_list sl){
//cuidado com a ordem! imprime por cima se tiver dois sprites no mesmo lugar
SPRITE *list[] = {sl.spaces, sl.walls, sl.fruits, sl.ghosts, sl.shot, sl.nest, sl.mr_do};
for (int i = 0; i < sizeof(list)/sizeof(SPRITE*); i++) {
print_list(w, list[i]);
}
}
//retorna a posicao do caractere no mapa
struct position find_char(struct sprite_list *sl, chtype ch){
struct position position;
SPRITE *list[] = {sl->mr_do, sl->nest};
for (int i = 0; i < (int)sizeof(list)/sizeof(SPRITE*); i++) {
SPRITE *current = list[i];
while(current != NULL){
if (current->representation == ch) {
position = current->position;
}
current = current->next;
}
}
return position;
}
int list_size(SPRITE *sp){
int count = 0;
SPRITE *current = sp;
while(current != NULL){
count++;
current = current->next;
}
return count;
}
//retorna quantidade de sprites vivos numa lista
int count_alive(SPRITE *sp){
int count = 0;
SPRITE *current = sp;
while(current != NULL){
if (current->alive) {
count++;
}
current = current->next;
}
return count;
}