-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
366 lines (331 loc) · 7.89 KB
/
Copy pathState.cpp
File metadata and controls
366 lines (331 loc) · 7.89 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "State.h"
#include <climits>
//State Class Functions:
//this sets up the starting state of a new run
void state::initState()
{
//we clear all data if it exists
for(int i = 0; i<info.size();i++)
{
info.at(i).clear();
}
if(info.size()>0)
info.clear();
//this creates the grid inside the 2d vector and adds all walls in their correct spot, no opening so far
for(int i = 0; i<totalSize; i++)
{
info.push_back(vector<Tile>());
for(int j = 0; j<totalSize;j++)
{
if(j == totalSize/2 || j==(totalSize/2)-1||i == totalSize/2 || i==(totalSize/2)-1)
info.at(i).push_back(WALL);
else
info.at(i).push_back(EMPTY);
}
}
//sets the position of lock and key
info.at(lockY)[lockX] = LOCK;
info.at(keyY)[keyX] = KEY;
//generates a valid space for the actor to start
while(true)
{
int r1 = rand()%totalSize;
int r2 = rand()%totalSize;
if(info.at(r1)[r2]==EMPTY)
{
info.at(r1)[r2]=AGENT;
agentX = r2;
agentY = r1;
break;
}
}
//DOORS GO HERE
//Left
info.at(roomSize+1)[2] = EMPTY;
info.at(roomSize)[2] = EMPTY;
// //right
info.at(roomSize+1)[roomSize+2] = EMPTY;
info.at(roomSize)[roomSize+2] = EMPTY;
// //top
info.at(1.)[roomSize+1] = EMPTY;
info.at(1)[roomSize] = EMPTY;
// //bottom
info.at(roomSize+5)[roomSize+1] = EMPTY;
info.at(roomSize+5)[roomSize] = EMPTY;
//set other variables
acquiredKey = 0;
success = 0;
reachedGoal = 0;
hitWall = 0;
steps = INT_MAX;
currentGoal.x = 0;
currentGoal.y = 0;
}
//moves up
bool state::moveUp()
{
if(agentY==0)
return 0;
switch(upActor)
{
case EMPTY:
// cout<<getTileName(getAgentTileData())<<endl;
upActor=AGENT;
atActor=getAgentTileData();
break;
case LOCK:
upActor=AGENT;
atActor=getAgentTileData();
if(acquiredKey)
success = true;
break;
case KEY:
upActor=AGENT;
atActor=getAgentTileData();
acquiredKey = true;
break;
case WALL:
return 0;
default:
return 0;
}
agentY--;
return 1;
}
//moves down
bool state::moveDown()
{
if(agentY==totalSize-1)
return 0;
switch(downActor)
{
case EMPTY:
// cout<<getTileName(getAgentTileData())<<endl;
atActor=getAgentTileData();
downActor=AGENT;
break;
case LOCK:
downActor=AGENT;
atActor=getAgentTileData();
if(acquiredKey)
success = true;
break;
case KEY:
downActor=AGENT;
atActor=getAgentTileData();
acquiredKey = true;
break;
case WALL:
return 0;
default:
return 0;
}
agentY++;
return 1;
}
//moves left
bool state::moveLeft()
{
if(agentX==0)
return 0;
switch(leftActor)
{
case EMPTY:
leftActor=AGENT;
atActor=getAgentTileData();
break;
case LOCK:
leftActor=AGENT;
atActor=getAgentTileData();
if(acquiredKey)
success = true;
break;
case KEY:
leftActor=AGENT;
atActor=getAgentTileData();
acquiredKey = true;
break;
case WALL:
return 0;
default:
return 0;
}
agentX--;
return 1;
}
//moves right
bool state::moveRight()
{
if(agentX==totalSize-1)
return 0;
switch(rightActor)
{
case EMPTY:
rightActor=AGENT;
atActor=getAgentTileData();
break;
case LOCK:
rightActor=AGENT;
atActor=getAgentTileData();
if(acquiredKey)
success = true;
break;
case KEY:
rightActor=AGENT;
atActor=getAgentTileData();
acquiredKey = true;
break;
case WALL:
return 0;
default:
return 0;
}
agentX++;
return 1;
}
//check's what was initially at a square, used to assist in the 4 move functions
Tile state::getAgentTileData()
{
if(agentX==lockX&&agentY==lockY)
return LOCK;
if(agentY==keyY&&agentX==keyX)
return KEY;
return EMPTY;
}
//this is the reward metric for the upper level AI
double state::checkLocation()
{//this will be used to determine the given reward to the upper level while updating state info
if(getAgentTileData() == KEY && !hasKey())
{
acquiredKey = 1;
return 20.;
}
else if(getAgentTileData()==LOCK && hasKey())
{
success = 1;
return 100.;
}
return 0.;
}
//checks how far you can move in each direction and returns a 4 int struct
//if b is false we check where the agent is, if true we check where the goal is
distanceClear state::getDistanceClear(bool b) const
{
int agentY = this->agentY;
int agentX = this->agentX;
if(b)
{
agentY = currentGoal.y;
agentX = currentGoal.x;
if(agentX>totalSize||agentX<0)
agentX = 0;
if(agentY>totalSize||agentY<0)
agentY = 0;
}
//Top
distanceClear ret;
if(agentY<2)
{
if(agentY==1&&upActor==WALL)
ret.up = 0;
else ret.up = agentY;
}
else if(upActor==WALL)
ret.up = 0;
else if(info[agentY-2][agentX]==WALL)
ret.up = 1;
else ret.up = 2;
//Bottom
if(agentY>totalSize-3)
{
if(agentY==totalSize-2&&downActor==WALL)
ret.down = 0;
else ret.down = totalSize-agentY-1;
}
else if(downActor==WALL)
ret.down = 0;
else if(info[agentY+2][agentX]==WALL)
ret.down = 1;
else ret.down = 2;
//Left side
if(agentX<2)
{
if(agentX==1&&leftActor==WALL)
ret.left = 0;
else ret.left = agentX;
}
else if(leftActor==WALL)
ret.left = 0;
else if(info[agentY][agentX-2]==WALL)
ret.left = 1;
else ret.left = 2;
//Right Side
if(agentX>totalSize-3)
{
if(agentX==totalSize-2&&rightActor==WALL)
ret.right = 0;
else ret.right = totalSize-agentX-1;
}
else if(rightActor==WALL)
ret.right = 0;
else if(info[agentY][agentX+2]==WALL)
ret.right = 1;
else ret.right = 2;
return ret;
}
//checks how far you can move in each direction and returns a 4 int struct
distanceClear state::getDistanceClear(int x, int y) const
{
int agentY = x;
int agentX = y;
//Top
distanceClear ret;
if(agentY<2)
{
if(agentY==1&&upActor==WALL)
ret.up = 0;
else ret.up = agentY;
}
else if(upActor==WALL)
ret.up = 0;
else if(info[agentY-2][agentX]==WALL)
ret.up = 1;
else ret.up = 2;
//Bottom
if(agentY>totalSize-3)
{
if(agentY==totalSize-2&&downActor==WALL)
ret.down = 0;
else ret.down = totalSize-agentY-1;
}
else if(downActor==WALL)
ret.down = 0;
else if(info[agentY+2][agentX]==WALL)
ret.down = 1;
else ret.down = 2;
//Left side
if(agentX<2)
{
if(agentX==1&&leftActor==WALL)
ret.left = 0;
else ret.left = agentX;
}
else if(leftActor==WALL)
ret.left = 0;
else if(info[agentY][agentX-2]==WALL)
ret.left = 1;
else ret.left = 2;
//Right Side
if(agentX>totalSize-3)
{
if(agentX==totalSize-2&&rightActor==WALL)
ret.right = 0;
else ret.right = totalSize-agentX-1;
}
else if(rightActor==WALL)
ret.right = 0;
else if(info[agentY][agentX+2]==WALL)
ret.right = 1;
else ret.right = 2;
return ret;
}