Chapter 7 - Project: A Robot

A mail-delivery robot picking up and dropping off parcels..

The village of Meadowfield isn’t very big. It consists of 11 places with 14 roads between them.

const roads = [
  "Alice's House-Bob's House",   "Alice's House-Cabin",
  "Alice's House-Post Office",   "Bob's House-Town Hall",
  "Daria's House-Ernie's House", "Daria's House-Town Hall",
  "Ernie's House-Grete's House", "Grete's House-Farm",
  "Grete's House-Shop",          "Marketplace-Farm",
  "Marketplace-Post Office",     "Marketplace-Shop",
  "Marketplace-Town Hall",       "Shop-Town Hall"

The network of roads in the village forms a graph. A graph is a collection of points (places in the village) with lines between them (roads). This graph will be the world that our robot moves through.

Let’s convert the list of roads to a data structure:

function buildGraph(edges) {
  let graph = Object.create(null);
  function addEdge(from, to) {
    if (graph[from] == null) {
      graph[from] = [to];
    } else {
  for (let [from, to] of => r.split("-"))) {
    addEdge(from, to);
    addEdge(to, from);
  return graph;

const roadGraph = buildGraph(roads);

The task

your first impulse might be to start defining objects for the various elements in the world: a class for the robot, one for a parcel…
This is wrong.
The fact that something sounds like an object does not automatically mean that it should be an object in your program.
Would lead to interconnected objects, and a program hard to understand.

class VillageState {
  constructor(place, parcels) { = place;
    this.parcels = parcels;

  move(destination) {
    if (!roadGraph[].includes(destination)) {
      return this;
    } else {
      let parcels = => {
        if ( != return p;
        return {place: destination, address: p.address};
      }).filter(p => != p.address);
      return new VillageState(destination, parcels);

Doing a delivery:

let first = new VillageState(
  "Post Office",
  [{place: "Post Office", address: "Alice's House"}]
let next = first.move("Alice's House");

// → Alice's House
// → []
// → Post Office

Persistent data

Data structures that don’t change are called immutable or persistent.
They behave a lot like strings and numbers in that they are who they are and stay that way, rather than containing different things at different times.

In JavaScript, just about everything can be changed, so working with values that are supposed to be persistent requires some restraint.
Object.freeze changes an object so that writing to its properties is ignored.
But having updates ignored is just about as likely to confuse someone as having them do the wrong thing.
So I usually prefer to just tell people that a given object shouldn’t be messed with and hope they remember it.

let object = Object.freeze({value: 5});
object.value = 10;
// → 5

When objects change over time, that adds a whole new dimension of complexity.
When the objects in my system are fixed, stable things, I can consider operations on them in isolation.
Anything that makes your code easier to understand makes it possible to build a more ambitious system.


A delivery robot looks at the world and decides in which direction it wants to move. As such, we could say that a robot is a function that takes a VillageState object and returns the name of a nearby place.

function runRobot(state, robot, memory) {
  for (let turn = 0;; turn++) {
    if (state.parcels.length == 0) {
      console.log(`Done in ${turn} turns`);
    let action = robot(state, memory);
    state = state.move(action.direction);
    memory = action.memory;
    console.log(`Moved to ${action.direction}`);

The robot must pick up all parcels by visiting every location that has a parcel and deliver them by visiting every location that a parcel is addressed to, but only after picking up the parcel.

What is the dumbest strategy that could possibly work? The robot could just walk in a random direction every turn. That means, with great likelihood, it will eventually run into all parcels and then also at some point reach the place where they should be delivered.

function randomPick(array) {
  let choice = Math.floor(Math.random() * array.length);
  return array[choice];

function randomRobot(state) {
  return {direction: randomPick(roadGraph[])};

adding a property to the constructor:

VillageState.random = function(parcelCount = 5) {
  let parcels = [];
  for (let i = 0; i < parcelCount; i++) {
    let address = randomPick(Object.keys(roadGraph));
    let place;
    do {
      place = randomPick(Object.keys(roadGraph));
    } while (place == address);
    parcels.push({place, address});
  return new VillageState("Post Office", parcels);

To actually start the program:

runRobot(VillageState.random(), randomRobot);
// → Moved to Marketplace
// → Moved to Town Hall
// → …
// → Done in 63 turns