@@ -46,7 +46,7 @@ This network of roads forms a _((graph))_. A graph is a collection of
46
46
points (places in the village) with lines between them (roads). This
47
47
graph will be the world that our robot moves through.
48
48
49
- {{index "roadsFrom object"}}
49
+ {{index "roadGraph object"}}
50
50
51
51
That array of strings isn't very easy to work with. Instead, we'll
52
52
typically want to find the destinations that we can reach from a given
@@ -70,7 +70,7 @@ function buildGraph(edges) {
70
70
return graph;
71
71
}
72
72
73
- const roadsFrom = buildGraph(roads);
73
+ const roadGraph = buildGraph(roads);
74
74
```
75
75
76
76
Given a an array of edges, ` buildGraph ` creates a map object that, for
@@ -146,7 +146,7 @@ class WorldState {
146
146
}
147
147
148
148
move(destination) {
149
- if (!roadsFrom [this.place].includes(destination)) {
149
+ if (!roadGraph [this.place].includes(destination)) {
150
150
return this;
151
151
} else {
152
152
let parcels = this.parcels.map(p => {
@@ -297,7 +297,7 @@ function randomPick(array) {
297
297
}
298
298
299
299
function randomRobot(state) {
300
- return {direction: randomPick(roadsFrom [state.place])};
300
+ return {direction: randomPick(roadGraph [state.place])};
301
301
}
302
302
```
303
303
@@ -320,10 +320,10 @@ sophisticated robot to work.
320
320
WorldState.random = function(parcelCount = 5) {
321
321
let parcels = [];
322
322
for (let i = 0; i < parcelCount; i++) {
323
- let address = randomPick(Object.keys(roadsFrom ));
323
+ let address = randomPick(Object.keys(roadGraph ));
324
324
let place;
325
325
do {
326
- place = randomPick(Object.keys(roadsFrom ));
326
+ place = randomPick(Object.keys(roadGraph ));
327
327
} while (place == address);
328
328
parcels.push({place, address});
329
329
}
@@ -453,11 +453,11 @@ shortest routes, if there are more than one) to the goal.
453
453
This is a function that does this:
454
454
455
455
``` {includeCode: true}
456
- function findRoute(from, to) {
456
+ function findRoute(graph, from, to) {
457
457
let work = [{at: from, route: []}];
458
458
for (let i = 0; i < work.length; i++) {
459
459
let {at, route} = work[i];
460
- for (let place of roadsFrom [at]) {
460
+ for (let place of graph [at]) {
461
461
if (place == to) return route.concat(place);
462
462
if (!work.some(w => w.at == place)) {
463
463
work.push({at: place, route: route.concat(place)});
@@ -505,9 +505,9 @@ function goalOrientedRobot({place, parcels}, route) {
505
505
if (route.length == 0) {
506
506
let parcel = parcels[0];
507
507
if (parcel.place != place) {
508
- route = findRoute(place, parcel.place);
508
+ route = findRoute(roadGraph, place, parcel.place);
509
509
} else {
510
- route = findRoute(place, parcel.address);
510
+ route = findRoute(roadGraph, place, parcel.address);
511
511
}
512
512
}
513
513
return {direction: route[0], memory: route.slice(1)};
0 commit comments