This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
76 lines (69 loc) · 2.14 KB
/
map.cpp
File metadata and controls
76 lines (69 loc) · 2.14 KB
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
//
// Created by alane on 04.01.2018.
//
#include <cstdlib>
#include <cstdio>
#include "map.h"
#include "gameProperties.h"
int getRandomPos() {
return rand() % BOARD_SIZE;
}
void setRandomPositionForSnake(Snake *snake, int index) {
(*snake).currDirection = RIGHT;
int randPos = -1;
while (randPos == -1) {
randPos = getRandomPos();
if (randPos + 1 == BOARD_SIZE ||
isCollidingPoint(randPos, randPos, index, false) ||
isCollidingPoint(randPos + 1, randPos, index, false)) {
randPos = -1;
}
}
(*snake).positions[0][0] = randPos;
(*snake).positions[0][1] = randPos;
(*snake).positions[1][0] = randPos + 1;
(*snake).positions[1][1] = randPos;
}
void setRandomPositionForSnakes(struct Snake *snakes, int count) {
for (int a = 0; a < count; a++) {
setRandomPositionForSnake(&snakes[a], a);
}
}
int isCollidingPoint(int x, int y, int excludeIndex, int isPrecise) {
for (int a = 0; a < gPlayerCount + gEnemyCount; a++) {
if (excludeIndex == a) continue;
Snake *snake = &gSnakes[a];
for (int b = 0; b < (*snake).currSize; b++) {
if(isPrecise &&
(*snake).positions[b][0] == x &&
(*snake).positions[b][1] == y) {
return true;
}
if (((*snake).positions[b][0] == x ||
(*snake).positions[b][0] == x + 1 ||
(*snake).positions[b][0] == x - 1)
&&
((*snake).positions[b][1] == y ||
(*snake).positions[b][1] == y + 1 ||
(*snake).positions[b][1] == y - 1)) {
return true;
}
}
}
return false;
}
void checkSnakeCollisions() {
for(int a = 0; a < gPlayerCount + gEnemyCount; a++) {
for(int b = 0; b < gSnakes[a].currSize; b++) {
if(isCollidingPoint(
gSnakes[a].positions[b][0],
gSnakes[a].positions[b][1],
a,
true
)) {
gSnakes[a].active = false;
} else {
}
}
}
}