-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaningRobot.js
More file actions
68 lines (62 loc) · 1.79 KB
/
cleaningRobot.js
File metadata and controls
68 lines (62 loc) · 1.79 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
// 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, nivel: 0 });
}
}
markFloorDirty(floorNumber) {
this.floors[floorNumber].dirty = true;
this.floors[floorNumber].nivel = 1 + (Math.floor(Math.random() * 10)%3);
}
simulate(action) {
switch (action) {
case 'SUCK':
this.floors[this.location].dirty = false;
break;
case 'LEFT-UP':
this.location = 0;
break;
case 'RIGHT-UP':
this.location = 2;
break;
case 'LEFT-DOWN':
this.location = 1;
break;
case 'RIGHT-DOWN':
this.location = 3;
break;
case 'ESVAZIAR':
this.location = 4;
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-UP';
}
else if (world.location == 2) {
return 'RIGHT-DOWN';
}
else if (world.location == 1) {
return 'LEFT-UP';
}
else if (world.location == 3) {
return 'LEFT-DOWN';
}
}
// 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];
}