-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy patherraticVacuum.js
More file actions
68 lines (58 loc) · 1.42 KB
/
erraticVacuum.js
File metadata and controls
68 lines (58 loc) · 1.42 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
class VacuumWorld {
constructor(floorCount) {
this.floorCount = (floorCount == undefined) ? 2 : floorCount;
this.dirt = new Array(this.floorCount).fill(false);
this.robotLocation = 0;
}
moveLeft() {
this.robotLocation = Math.max(this.robotLocation - 1, 0);
}
moveRight() {
this.robotLocation = Math.min(this.robotLocation + 1, this.floorCount - 1);
}
clean(i) {
if (i >= 0 && i < this.floorCount) {
this.dirt[i] = false;
}
}
makeDirt(i) {
if (i >= 0 && i < this.floorCount) {
this.dirt[i] = true;
}
}
suck() {
this.clean(this.robotLocation);
}
randomize() {
for (let i = 0; i < this.floorCount; i++) {
this.dirt[i] = (Math.random() < 0.5);
}
this.robotLocation = Math.floor(Math.random() * this.floorCount);
}
}
class ErraticWorld extends VacuumWorld {
constructor(floorCount) {
super(floorCount);
}
erraticSuck() {
let i = this.robotLocation;
if (this.dirt[i]) {
//If the tile is dirty,
this.clean(i);
if (Math.random() < 0.75) {
//Clean up adjacent tiles with 50% probability
this.clean(i - 1);
}
if (Math.random() < 0.75) {
//Clean up adjacent tiles with 50% probability
this.clean(i + 1);
}
} else {
//if tile clean
if (Math.random() < 0.75) {
//Make the tile dirty with 50% probability
this.makeDirt(i);
}
}
}
};