-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.cpp
More file actions
142 lines (122 loc) · 4.94 KB
/
cat.cpp
File metadata and controls
142 lines (122 loc) · 4.94 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
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
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#include "global.h"
#include "cat.h"
using namespace std;
Cat::Cat() {
location = pair<int, int>(3, 5);
lastLocation = pair<int, int>(3, 5);
}
Cat::Cat(pair<int, int> startLocation) {
location = startLocation;
lastLocation = startLocation;
}
// Return 2 if cat caught the mouse, 1 if cat saw the mouse, or 0 if cat saw nothing
int Cat::move(pair<int, int> mouseLocation) {
int searchResults = search(mouseLocation);
vector<pair<int, int>> directionOptions;
int randomIndex;
switch (searchResults) {
case 0: // If mouse is not visible, go random direction
// get direction options
if (location.first-1 >= 0 && maze[location.second][location.first-1] == 0 && pair<int, int>(location.first-1, location.second) != lastLocation) { // check left
directionOptions.push_back(pair<int, int>(location.first-1, location.second));
}
if (location.first+1 < 10 && maze[location.second][location.first+1] == 0 && pair<int, int>(location.first+1, location.second) != lastLocation) { // check right
directionOptions.push_back(pair<int, int>(location.first+1, location.second));
}
if (location.second-1 >= 0 && maze[location.second-1][location.first] == 0 && pair<int, int>(location.first, location.second-1) != lastLocation) { // check up
directionOptions.push_back(pair<int, int>(location.first, location.second-1));
}
if (location.second+1 < 10 && maze[location.second+1][location.first] == 0 && pair<int, int>(location.first, location.second+1) != lastLocation) { // check down
directionOptions.push_back(pair<int, int>(location.first, location.second+1));
}
// Check if there is no options
if (directionOptions.empty()) {
pair<int, int> temp = lastLocation;
lastLocation = location;
location = temp;
return 0;
}
randomIndex = rand() % directionOptions.size();
lastLocation = location;
location = directionOptions.at(randomIndex);
return 0;
break;
case 1: // Mouse is visible and left
if (pair<int, int>(location.first-1, location.second) == mouseLocation) {
location.first--;
return 2;
} else {
location.first--;
location.first--;
return 1;
}
break;
case 2: // Mouse is visible and right
if (pair<int, int>(location.first+1, location.second) == mouseLocation) {
location.first++;
return 2;
} else {
location.first++;
location.first++;
return 1;
}
break;
case 3: // Mouse is visible and up
if (pair<int, int>(location.first, location.second-1) == mouseLocation) {
location.second--;
return 2;
} else {
location.second--;
location.second--;
return 1;
}
break;
case 4: // Mouse is visible and down
if (pair<int, int>(location.first, location.second+1) == mouseLocation) {
location.second++;
return 2;
} else {
location.second++;
location.second++;
return 1;
}
break;
}
return 0;
}
// Return direction of the mouse if seen. If not, return 0
// 1 = left, 2 = right, 3 = up, 4 = down
int Cat::search(pair<int, int> mouseLocation) {
// left
if (location.first-1 >= 0 && maze[location.second][location.first-1] != 1) {
if (pair<int, int>(location.first-1, location.second) == mouseLocation || pair<int, int>(location.first-2, location.second) == mouseLocation) {
return 1;
}
}
// right
if (location.first+1 < 10 && maze[location.second][location.first+1] != 1) {
if (pair<int, int>(location.first+1, location.second) == mouseLocation || pair<int, int>(location.first+2, location.second) == mouseLocation) {
return 2;
}
}
// up
if (location.second-1 >= 0 && maze[location.second-1][location.first] != 1) {
if (pair<int, int>(location.first, location.second-1) == mouseLocation || pair<int, int>(location.first, location.second-2) == mouseLocation) {
return 3;
}
}
// down
if (location.second+1 < 10 && maze[location.second+1][location.first] != 1) {
if (pair<int, int>(location.first, location.second+1) == mouseLocation || pair<int, int>(location.first, location.second+2) == mouseLocation) {
return 4;
}
}
return 0;
}
pair<int, int> Cat::getLocation() {
return location;
}