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 pathai.cpp
More file actions
68 lines (64 loc) · 2.09 KB
/
Copy pathai.cpp
File metadata and controls
68 lines (64 loc) · 2.09 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
//
// Created by alane on 04.01.2018.
//
#include "ai.h"
#include "helpers.h"
#include "map.h"
int canGo(Snake *snake, Direction direction, int index) {
int xPos =
(*snake).positions[(*snake).currSize - 1][0] +
(direction == RIGHT ? 1 : 0) +
(direction == LEFT ? -1 : 0);
int yPos =
(*snake).positions[(*snake).currSize - 1][1] +
(direction == UP ? 1 : 0) +
(direction == DOWN ? -1 : 0);
return !isCollidingPoint(
xPos,
yPos,
index,
false
);
}
Direction getBestDirection(struct Snake *snake, int index) {
int canGoUp = canGo(snake, UP, index);
int canGoDown = canGo(snake, DOWN, index);
int canGoRight = canGo(snake, RIGHT, index);
int canGoLeft = canGo(snake, LEFT, index);
int needsUp =
(*snake).positions[(*snake).currSize - 1][1] == gCurrRewardPos[1] ?
-1 :
(*snake).positions[(*snake).currSize - 1][1] <= gCurrRewardPos[1];
int needsRight =
(*snake).positions[(*snake).currSize - 1][0] == gCurrRewardPos[0] ?
-1 :
(*snake).positions[(*snake).currSize - 1][0] <= gCurrRewardPos[0];
if((*snake).currDirection == UP) canGoDown = false;
if((*snake).currDirection == DOWN) canGoUp = false;
if((*snake).currDirection == RIGHT) canGoLeft = false;
if((*snake).currDirection == LEFT) canGoRight = false;
if (needsUp != -1) {
if (needsUp && canGoUp) {
return UP;
} else if (!needsUp && canGoDown) {
return DOWN;
}
}
if(needsRight != -1){
if (needsRight && canGoRight) {
return RIGHT;
} else if (!needsRight && canGoLeft) {
return LEFT;
}
}
if (canGoUp) return UP;
else if (canGoDown) return DOWN;
else if (canGoRight) return RIGHT;
else if (canGoLeft) return LEFT;
return UP;
}
void setEnemyDirections() {
for (int a = 0; a < gPlayerCount + gEnemyCount; a++) {
if(!gSnakes[a].isPlayer) gSnakes[a].currDirection = getBestDirection(&gSnakes[a], a);
}
}