{"size": 955, "ext": "js", "max_stars_repo_path": "chapter7-project-robot/project/Village.js", "max_stars_repo_name": "alpha-beta-soup/eloquent-javascript-3", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "chapter7-project-robot/project/Village.js", "max_issues_repo_name": "alpha-beta-soup/eloquent-javascript-3", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "chapter7-project-robot/project/Village.js", "max_forks_repo_name": "alpha-beta-soup/eloquent-javascript-3", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.8108108108, "max_line_length": 56, "alphanum_fraction": 0.6209424084}
const {roadGraph} = require('./data');
const {randomPick} = require('./utils');

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

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

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);
};

module.exports = VillageState;
