-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.h
374 lines (323 loc) · 9.89 KB
/
snake.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdint.h> /* Declarations of uint_32 and the like */
//#include "/Applications/mcb32tools.app/Contents/Resources/Toolchain/include/pic32mx.h" /* Declarations of system-specific addresses etc */ ///Applications/mcb32tools.app/Contents/Resources/Toolchain/include/pic32mx.h
#include "\msys64\opt\mcb32tools\include\pic32mx.h"
#include "mipslab.h" /* Declatations for these labs */
#include <stdlib.h>
#define SIZE 1024
int apple_pos[5] = {0,0,0,0,0}; // Array to store all the positions of the apples
int appleCount; // Initial appleCount set from main menu
int last_apple; // Variable to store the pos in array of last apple that was eaten
int appleCC; // The amount of apples that need to be generated to reach appleCount
int player_front = -1, player_rear = -1;
char player_prev_movm[SIZE];
int currScore = 0;
int player_head = 128 * 14 + 6; // Player snake pos
int player_end = 128 * 14 + 2; // Player snake tail
char player_vektor = 'r'; // r = right, l = left, u = up, d = down
int ai_front = -1, ai_rear = -1;
char ai_prev_movm[SIZE];
int AI_head = 128 * 14 + 120; //AI snake pos
int AI_end = 128 * 14 + 124; //AI snake tail
char AI_vektor = 'l'; // r = right, l = left, u = up, d = down
int ai_score=0;
/*
Generate the outline of the snake game (the walls)
incase infinite walls game mode has not been selected
*/
void generate_walls(){
int i;
for ( i = 1; i < 127; i++) {
bitmap[i+128] = 1;
bitmap[i+128*30] = 1;
}
int j;
for ( j = 1; j < 31; j++) {
bitmap[j*128 + 1] = 1;
bitmap[j*128+126] = 1;
}
}
/*
Generates obstacle at with the top left corner at tl
4x4 obstacles with half hollow 2x2 centers.
*/
void generate_bomb(int tl) {
bitmap[tl+1+128]=1;
bitmap[tl+2+2*128]=1;
int i;
//Bottom & top
for (i = 0; i < 4;i++) {
bitmap[tl+i] = 1;
bitmap[tl+i+3*128] = 1;
}
int j;
//Left & right
for (j = 1; j < 3;j++) {
bitmap[tl+j*128] = 1;
bitmap[tl+j*128+3] = 1;
}}
/*
Generates a map with different difficulties depending on the input from menu
Ranges from map 1 (standard map) to map 5 (hardest map)
*/
void generate_map(int map){
if(map==1) {
generate_bomb(60+12*128);
}
if(map==2) {
generate_bomb(62+128*4);
generate_bomb(62+24*128);
}
if(map==3){
generate_bomb(62+128*4);
generate_bomb(62+24*128);
generate_bomb(62+128*8);
generate_bomb(62+20*128);
}if(map==4){
int i;
for (i=0;i<2;i++){
generate_bomb(32+128*4 +60*i);
generate_bomb(32+24*128+60*i);
generate_bomb(32+128*8+60*i);
generate_bomb(32+20*128+60*i);
}
}if(map==5){
int i;
for (i=0;i<2;i++){
generate_bomb(32+128*4 +60*i);
generate_bomb(32+24*128+60*i);
generate_bomb(32+128*8+60*i);
generate_bomb(32+20*128+60*i);
}
for(i=0;i<4;i++){
generate_bomb(54+12*128+i*4);
generate_bomb(54+16*128+i*4);
}
}
}
/*
Pushes an element to the queue
*/
void push(int x, char arr[], int *front, int *rear) {
if (*front == -1) {
*front = 0;
*rear = 0;
}
*rear = (*rear + 1) % SIZE; // Circular increment to prevent array overflow
arr[*rear] = x;
}
/*
Pops an element from the queue
*/
int pop(char arr[], int *front, int *rear) {
if (*front == -1) {
return -1; // Assuming -1 is an invalid value; you can choose a different approach
}
int poppedElem = arr[*front];
if (*front == *rear) {
*front = *rear = -1;
} else {
*front = (*front + 1) % SIZE; // Circular increment to prevent array overflow
}
return poppedElem;
}
/*
Initializes the snake at the position pos
*/
void init_snake(int pos) {
int i = 0;
for (i; i < 6; i++) {
bitmap[pos+i] = 1;
bitmap[pos+i+128] = 1;
}
}
/*
Creates an apple (2x2) at a random position on the map using the value of TMR2 as seed
*/
int create_apple(int TMR2copy) {
int appleX = ((TMR2copy % 61) + 1)*2; // Ensures appleX is >= 3, odd, and < 127
int appleY = ((TMR2copy % 13) + 1)*2; // Ensures appleY is >= 2, even, and < 31
if (bitmap[appleX+appleY*128] != 0 || bitmap[appleX+appleY*128+1] != 0 || bitmap[appleX+appleY*128+128] != 0 || bitmap[appleX+appleY*128+1+128] != 0) {
return 0;
} else {
bitmap[appleX+appleY*128] = 4;
bitmap[appleX+appleY*128+1] = 5;
bitmap[appleX+appleY*128+128] = 5;
bitmap[appleX+appleY*128+1+128] = 5;
if (last_apple < 0) { //Initialize all apples (Start of game)
apple_pos[appleCount+last_apple] = appleX+appleY*128;
last_apple++;
} else {
apple_pos[last_apple] = appleX+appleY*128;
}
appleCC--;
return 1;
}
}
/*
Checks if the snake has collided with an obstacle in the next pixel update
*/
int check_obstacle(int *pos, char *vektor){
int headX = *pos%128;
int headY = *pos/128;
if (*vektor == 'r') {
if (bitmap[headY*128 + (headX+2)%128] == 1 || bitmap[headY*128 + (headX+2)%128] == 2) {
// Game over
return 1;
}
else if (bitmap[headY*128 + (headX+2)%128] == 4) {
int i = 0;
for (i; i < appleCount; i++) {
if (apple_pos[i] == headY*128 + (headX+2)%128) {
last_apple = i;
break;
}
}
return 4;
}
}
else if (*vektor == 'l') {
if (bitmap[headY*128 + (headX+128-1)%128] == 1 || bitmap[headY*128 + (headX+128-1)%128] == 2) {
// Game over
return 1;
}
else if (bitmap[headY*128 + (headX+128-1)%128] == 4) {
// Eat apple
int i = 0;
for (i; i < appleCount; i++) {
if (apple_pos[i] == headY*128 + (headX+128-1)%128) {
last_apple = i;
break;
}
}
return 4;
}
}
else if (*vektor == 'u') {
if (bitmap[((headY+32-1)%32)*128 + headX] == 1 || bitmap[((headY+32-1)%32)*128 + headX] == 2) {
// Game over
return 1;
}
else if (bitmap[((headY+32-1)%32)*128 + headX] == 4) {
// Eat apple
int i = 0;
for (i; i < appleCount; i++) {
if (apple_pos[i] == ((headY+32-1)%32)*128 + headX) {
last_apple = i;
break;
}
}
return 4;
}
}
else if (*vektor == 'd') {
if (bitmap[((headY+2)%32)*128 + headX] == 1 || bitmap[((headY+2)%32)*128 + headX] == 2) {
// Game over
return 1;
}
else if (bitmap[((headY+2)%32)*128 + headX] == 4) {
// Eat apple
int i = 0;
for (i; i < appleCount; i++) {
if (apple_pos[i] == ((headY+2)%32)*128 + headX) {
last_apple = i;
break;
}
}
return 4;
}
}
return 0;
}
/*
Removes the last pixels (2x1) of the snake and updates the end position of the snake
*/
void movement_remove(int *end, int AI) {
int endX = *end%128;
int endY = *end/128;
int stored_v;
if (AI == 1) { // AI movement
stored_v = pop(ai_prev_movm, &ai_front, &ai_rear);
} else { // Player movement
stored_v = pop(player_prev_movm, &player_front, &player_rear);
}
if(stored_v == 'l'){
bitmap[endY*128 + (endX+1)%128]=0;
bitmap[endY*128 + (endX+1)%128 + 128]=0;
*end=endY*128 + (128+endX-1)%128;
}
else if(stored_v == 'r'){
bitmap[endY*128 + endX]=0;
bitmap[endY*128 + endX+128]=0;
*end=endY*128 + (endX+1)%128;
}
else if(stored_v == 'd'){
bitmap[((endY)%32)*128 + endX]=0;
bitmap[((endY)%32)*128 + endX+1]=0;
*end=((endY+1)%32)*128 + endX;
}
else if(stored_v == 'u'){
bitmap[((endY+1)%32)*128 + endX]=0;
bitmap[((endY+1)%32)*128 + endX+1]=0;
*end=((endY+32-1)%32)*128 + endX;
}
}
/*
Updates the snake's head postion (2x1)
*/
int movement(uint8_t button, int *pos, int *end, char *vektor, int AI){
if(button=='l' && *vektor != 'r'){
*vektor = button;
}
if(button=='r' && *vektor != 'l'){
*vektor = button;
}
if(button=='u' && *vektor != 'd'){
*vektor = button;
}
if(button=='d' && *vektor != 'u'){
*vektor = button;
}
int next_step = check_obstacle(pos, vektor);
if (next_step == 4) {
if (AI != 1) {
currScore++;
}else if (AI==1){
ai_score++;
}
appleCC++;
}
if (next_step!=4 && next_step !=5){
movement_remove(end, AI);
}
if (next_step==1){
return 1;
}
if (AI == 1) {
push(*vektor, ai_prev_movm, &ai_front, &ai_rear);
} else {
push(*vektor, player_prev_movm, &player_front, &player_rear);
}
int headX = *pos%128;
int headY = *pos/128;
if (*vektor == 'l'){
bitmap[headY*128 + (128+headX-1)%128]=1;
bitmap[headY*128 + (128+headX-1)%128 + 128]=1;
*pos=headY*128 + (128+headX-1)%128;
}
else if(*vektor == 'r'){
bitmap[headY*128 + (headX+2)%128]=1;
bitmap[headY*128 + (headX+2)%128 + 128]=1;
*pos=headY*128 + (headX+1)%128;
}
else if(*vektor == 'd'){
bitmap[((headY+2)%32)*128 + headX]=1;
bitmap[((headY+2)%32)*128 + headX+1]=1;
*pos=((headY+1)%32)*128 + headX;
}
else if(*vektor == 'u'){
bitmap[((headY+32-1)%32)*128 + headX]=1;
bitmap[((headY+32-1)%32)*128 + headX+1]=1;
*pos=((headY+32-1)%32)*128 + headX;
}
return 0;
}