/
list.c
246 lines (194 loc) · 4.97 KB
/
list.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "list.h"
#include "mylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <debugging.h>
/**
* This function will create a new list and return a pointer to that list.
*/
LIST* create_list()
{
LIST *l = malloc(sizeof(LIST));
l->size = 0;
l->head = NULL;
l->tail = NULL;
l->food = NULL;
return l;
}
/**
* This function will add the given data to the linked list (llist)
*/
void list_add(LIST* llist, int row, int col, int width, int height)
{
//printf("ADDING TO THE LIST\n");
NODE *node = create_node(row, col, width, height);
//first, check to see if the list is brand new
if((llist->head == NULL) &&(llist->tail == NULL)){
//new list, point the head and tail toward the newly created node
llist->head = node;
llist->tail = node;
}
//else, make the new node the tail
else{
//instantiate node as the new tail of the list
llist->tail->next = node;
node->prev = llist->tail;
node->next = NULL;
llist->tail = node;
}
llist->size++;
return;
}
/**
* This function will remove the data located at the given index in the list (llist)
* just like the above function, but removes the node instead
* remember to free the node and re-arrange your pointers as needed!
*/
void list_del(LIST* llist, int index)
{
if((index > (llist->size - 1)) || (index < 0)){
//printf("Error, index %d is out of bounds\n",index);
return;
}
//check to see if the user wants to remove the head
else if(index == 0){
//wants to remove the head
NODE *toDelete = llist->head;
llist->head = toDelete->next;
free(toDelete);
llist->size--;
}
else{
NODE *cur = llist->head;
int i;
for(i = 0; i < (index - 1); i++){
cur = cur->next;
}
//have the node we want, time to remove it
NODE *toDelete = cur->next;
cur->next = toDelete->next;
if(cur->next == NULL){
llist->tail = cur;
}
free(toDelete);
llist->size--;
}
}
/**
* This function will create a node from the given data and return a pointer to that node.
*/
NODE* create_node(int row, int col, int width, int height)
{
NODE *node = malloc(sizeof(NODE));
node->row = row;
node->col = col;
node->width = width;
node->height = height;
return node;
}
/**
* This function will print a node out in a nicely formatted way. Do whatever you want, just try to make it easy to read.
* Note you will be using this to traverse over the list and print the nodes.
*/
void print_node(NODE* node)
{
//printf("-------------------------------\n");
//printf("Node value = %d\n",node->data);
//print the next node's value only if node isn't the tail
if(node->next != NULL){
//printf("Next Node's value = %d\n",node->next->data);
}
else{
// printf("This node is the tail\n");
}
// printf("-------------------------------\n");
// printf("-------------------------------\n");
return;
}
/*
This function will initialize the snake, and the food that will cause the snake to grow
*/
LIST* initializeSnake(){
LIST * l = create_list();
//establish 3 sections
int col = 60;
for(int i = 0; i < 5; i++){
list_add(l,0,col,10,10);
col -= 10;
}
int foodRow = 100;
int foodCol = 100;
l->food = create_node(foodRow, foodCol, 10, 10);
return l;
}
/*
updates the positions of the snake
*/
void updateSnakePositions(LIST* snake, int oldRow, int oldCol){
NODE* current = snake->tail;
while(current->prev != snake->head){
current->row = current->prev->row;
current->col = current->prev->col;
current = current->prev;
}
current->row = oldRow;
current->col = oldCol;
}
void drawSnake(LIST* snake, int ateFood, int oldRow, int oldCol, int seed){
//draw the new location of the head of the snake
drawImage3(snake->head->row,
snake->head->col,
snake->head->width,
snake->head->height, WHITE);
//only draw over the tail if it didn't eat food
if(ateFood == 0){
drawImage3(oldRow, oldCol, snake->tail->width,
snake->tail->height, BLACK);
}
else{
//snake ate the food, find a new place for the food
srand(seed);
int row = 0;
int col = 0;
do{
row = rand() % 16;
col = rand() % 24;
}
while(contains(snake, row, col));
//make a new food piece
NODE* newFood = create_node(row * snake->head->height,
col * snake->head->width,
snake->head->height,
snake->head->width);
DEBUG_PRINTF("New food place at (%d,%d)\n",newFood->row,newFood->col);
snake->food = newFood;
drawImage3(snake->food->row,
snake->food->col,
snake->food->width,
snake->food->height, WHITE);
}
}
int hitSelf(LIST* snake){
int result = 0;
NODE* current = snake->head->next;
while(current != NULL){
if(snake->head->row == current->row && snake->head->col == current->col){
result = 1;
}
current = current->next;
}
return result;
}
/*
This determines if one of the snake's segments is at the given row or column
*/
int contains(LIST* snake, int row, int col){
NODE* current = snake->head;
while(current != NULL){
if(current->row == row && current->col == col){
return 1;
}
current = current->next;
}
return 0;
}