Skip to content
Snippets Groups Projects
Select Git revision
  • cd760a2c58238c13448674ebe891847d6c281854
  • main default protected
  • solution
3 results

service-worker.js

Blame
  • trains.js 2.72 KiB
    // INSTRUCTIONS: Uncomment this import once you need to call Dijkstra's algorithm.
    // import { dijkstras } from './dijkstras.js';
    
    export class Stop {
      constructor(time, station) {
        this.time = time; // in minutes since the beginning of the journey
        this.station = station; // represented as a string
      }
    }
    
    export class Train {
      constructor(name, stops) {
        this.name = name;
        this.stops = stops;
      }
    
      toString() {
        return `the ${this.name} train`;
      }
    }
    
    // eslint-disable-next-line no-unused-vars -- INSTRUCTIONS: Remove this comment once you use the Action class.
    class Action {
      constructor(delay, train, station, duration, newSituation) {
        this.delay = delay;
        this.train = train;
        this.station = station;
        this.duration = duration;
        this.newSituation = newSituation;
      }
    
      get timeTaken() {
        return this.delay + this.duration;
      }
    
      toString() {
        const wait = this.delay > 0 ? `wait for ${this.delay} minute(s) and then ` : '';
        return `${wait}ride ${this.train} to ${this.station} for ${this.duration} minute(s)`;
      }
    }
    
    export class Schedule {
      constructor(trains) {
        this.trains = trains;
      }
    
      *getPossibleActionsFrom(situation) {
        // INSTRUCTIONS: Implement this generator function to support your design of
        // `planJourney` (see below).
      }
    }
    
    export function planJourney(schedule, fromStation, toStation) {
      // INSTRUCTIONS: Complete this JavaScript function so that it uses a single
      // call to Dijkstra's algorithm to plan a sequence of actions that will get a
      // traveler from `fromStation` at time zero to `toStation` as soon as
      // possible.  You may assume that the only way for the traveler to get between
      // stations is to ride the trains whose stops are described by `schedule`.
      //
      // In your design you will have to determine what class to use for your graph,
      // what class to use for your graph vertices, what class to use for your graph
      // edges, how to create a starting vertex, and how to create a goal predicate
      // to pass to Dijkstra's algorithm.  You will also have to fill in the
      // `getPossibleActionsFrom` generator function above, which will contain the
      // bulk of the logic that you write.
      //
      // You can import Dijkstra's algorithm by uncommenting the import at the top
      // of the file.  Be sure to read that implementation of the algorithm to
      // understand how it differs from the implementation shown in class.
      //
      // As a note, good style is to write your goal predicate as an arrow function.
      // You may find the following article useful as a reminder about how to write
      // an arrow function:
      //
      // * https://javascript.info/arrow-functions-basics
      const path = undefined;
      return path !== undefined ? path.map((action) => `${action}`) : undefined;
    }