-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.cpp
More file actions
182 lines (156 loc) · 6.58 KB
/
mouse.cpp
File metadata and controls
182 lines (156 loc) · 6.58 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
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
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#include "global.h"
#include "mouse.h"
using namespace std;
Mouse::Mouse() {
location = pair<int, int>(7, 3);
lastLocation = pair<int, int>(7, 3);
}
Mouse::Mouse(pair<int, int> startLocation) {
location = startLocation;
lastLocation = startLocation;
}
// Return 2 if cat is seen. If not, return 1 if cheese is seen or 0 if neither are seen
int Mouse::move(pair<int, int> catLocation) {
int searchResults = search(catLocation);
vector<pair<int, int>> directionOptions;
int randomIndex;
switch (searchResults) {
case 0: // Nothing is 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 searchResults;
}
randomIndex = rand() % directionOptions.size();
lastLocation = location;
location = directionOptions.at(randomIndex);
break;
case 1: // Cheese is visible, move towards it
if (location.first > cheeseLocation.first) { // Cheese is to the left
lastLocation = location;
location.first--;
} else if (location.first < cheeseLocation.first) { // Cheese is to the right
lastLocation = location;
location.first++;
} else if (location.second > cheeseLocation.second) { // Cheese is up
lastLocation = location;
location.second--;
} else if (location.second < cheeseLocation.second) { // Cheese is down
lastLocation = location;
location.second++;
}
break;
case 2: // Move in any direction but the cat
int catDirection = 0; // 1 = left, 2 = right, 3 = up, 4 = down
//Check the direction of the cat
if (location.first > catLocation.first) { // Cheese is to the left
catDirection = 1;
} else if (location.first < catLocation.first) { // Cheese is to the right
catDirection = 2;
} else if (location.second > catLocation.second) { // Cheese is up
catDirection = 3;
} else if (location.second < catLocation.second) { // Cheese is down
catDirection = 4;
}
// get direction options
if (catDirection != 1 && location.first-1 >= 0 && maze[location.second][location.first-1] == 0) { // check left
directionOptions.push_back(pair<int, int>(location.first-1, location.second));
}
if (catDirection != 2 && location.first+1 < 10 && maze[location.second][location.first+1] == 0) { // check right
directionOptions.push_back(pair<int, int>(location.first+1, location.second));
}
if (catDirection != 3 && location.second-1 >= 0 && maze[location.second-1][location.first] == 0) { // check up
directionOptions.push_back(pair<int, int>(location.first, location.second-1));
}
if (catDirection != 4 && location.second+1 < 10 && maze[location.second+1][location.first] == 0) { // check down
directionOptions.push_back(pair<int, int>(location.first, location.second+1));
}
// Check if there is no options
if (directionOptions.empty()) {
return searchResults;
}
randomIndex = rand() % directionOptions.size();
lastLocation = location;
location = directionOptions.at(randomIndex);
break;
}
return searchResults;
}
// Return 2 if cat is seen. If not, return 1 if cheese is seen or 0 if neither are seen
int Mouse::search(pair<int, int> catLocation) {
bool cheeseIsVisible = false;
// left
for (int i = location.first - 1; i >= 0; i--) {
if (maze[location.second][i] == 1) {
break;
}
if (pair<int, int>(i, location.second) == cheeseLocation) {
cheeseIsVisible = true;
}
if (pair<int, int>(i, location.second) == catLocation) {
return 2;
}
}
// right
for (int i = location.first + 1; i <= 9; i++) {
if (maze[location.second][i] == 1) {
break;
}
if (pair<int, int>(i, location.second) == cheeseLocation) {
cheeseIsVisible = true;
}
if (pair<int, int>(i, location.second) == catLocation) {
return 2;
}
}
// up
for (int i = location.second - 1; i >= 0; i--) {
if (maze[i][location.first] == 1) {
break;
}
if (pair<int, int>(location.first, i) == cheeseLocation) {
cheeseIsVisible = true;
}
if (pair<int, int>(location.first, i) == catLocation) {
return 2;
}
}
// down
for (int i = location.second + 1; i <= 9; i++) {
if (maze[i][location.first] == 1) {
break;
}
if (pair<int, int>(location.first, i) == cheeseLocation) {
cheeseIsVisible = true;
}
if (pair<int, int>(location.first, i) == catLocation) {
return 2;
}
}
if (cheeseIsVisible) {
return 1;
}
return 0;
}
pair<int, int> Mouse::getLocation() {
return location;
}