-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathcleaningRobot.js
47 lines (41 loc) · 1.32 KB
/
cleaningRobot.js
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
// In this simple problem the world includes both the environment and the robot
// but in most problems the environment and world would be separate
class World {
constructor(numFloors) {
this.location = 0;
this.floors = [];
for (let i = 0; i < numFloors; i++) {
this.floors.push({dirty: false});
}
}
markFloorDirty(floorNumber) {
this.floors[floorNumber].dirty = true;
}
simulate(action) {
switch(action) {
case 'SUCK':
this.floors[this.location].dirty = false;
break;
case 'LEFT':
this.location = 0;
break;
case 'RIGHT':
this.location = 1;
break;
}
return action;
}
}
// Rules are defined in code
function reflexVacuumAgent(world) {
if (world.floors[world.location].dirty) { return 'SUCK'; }
else if (world.location == 0) { return 'RIGHT'; }
else if (world.location == 1) { return 'LEFT'; }
return action;
}
// Rules are defined in data, in a table indexed by [location][dirty]
function tableVacuumAgent(world, table) {
let location = world.location;
let dirty = world.floors[location].dirty ? 1 : 0;
return table[location][dirty];
}