-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbored.cpp
77 lines (73 loc) · 1.88 KB
/
bored.cpp
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
#include "bored.h"
#include <math.h>
#include <ncurses.h>
int Bored::decide(int yPacLoc, int xPacLoc, int pacDirection) {
int distance = sqrt((yPacLoc - yLoc)^2 + (xPacLoc - xLoc)^2);
switch(pacDirection) {
case KEY_UP:
yPacLoc -= distance / 2;
break;
case KEY_DOWN:
yPacLoc += distance / 2;
break;
case KEY_LEFT:
xPacLoc -= distance / 2;
break;
case KEY_RIGHT:
xPacLoc += distance / 2;
break;
default:
break;
}
if (yPacLoc <= yLoc && xPacLoc <= xLoc) { // tl
if (checkCollision(yLoc - 1, xLoc) || prevDecision == KEY_DOWN || !hunter)
if (checkCollision(yLoc, xLoc - 1) || prevDecision == KEY_RIGHT || !hunter)
if (rand() % 2)
return KEY_RIGHT;
else
return KEY_DOWN;
else
return KEY_LEFT;
else
return KEY_UP;
}
if (yPacLoc < yLoc && xPacLoc > xLoc) { // tr
if (checkCollision(yLoc - 1, xLoc) || prevDecision == KEY_DOWN || !hunter)
if (checkCollision(yLoc, xLoc + 1) || prevDecision == KEY_LEFT || !hunter)
if (rand() % 2)
return KEY_LEFT;
else
return KEY_DOWN;
else
return KEY_RIGHT;
else
return KEY_UP;
}
if (yPacLoc > yLoc && xPacLoc < xLoc) { // bl
if (checkCollision(yLoc + 1, xLoc) || prevDecision == KEY_UP || !hunter)
if (checkCollision(yLoc, xLoc - 1) || prevDecision == KEY_RIGHT || !hunter)
if (rand() % 2)
return KEY_RIGHT;
else
return KEY_UP;
else
return KEY_LEFT;
else
return KEY_DOWN;
}
if (yPacLoc >= yLoc && xPacLoc >= xLoc) { // br
if (checkCollision(yLoc + 1, xLoc) || prevDecision == KEY_UP || !hunter)
if (checkCollision(yLoc, xLoc + 1) || prevDecision == KEY_LEFT || !hunter)
if (rand() % 2)
return KEY_LEFT;
else
return KEY_UP;
else
return KEY_RIGHT;
else
return KEY_DOWN;
}
}
bool Bored::decideAndUpdate(int yPacLoc, int xPacLoc, int pacDirection) {
return update(decide(yPacLoc, xPacLoc, pacDirection));
}