Update lab 12 skeleton

This commit is contained in:
Isaac Shoebottom 2022-10-25 11:15:01 -03:00
parent 243b14b247
commit 8f668a032f
3 changed files with 77 additions and 1 deletions

View File

@ -1,5 +1,5 @@
Title: Lab Twelve Title: Lab Twelve
Date: 2022-10-19T08:30:00 Date: 2022-10-24T08:30:00
Tags: cs2613, lab, javascript Tags: cs2613, lab, javascript
Sample description Sample description

View File

@ -0,0 +1,28 @@
let village = require("../village.js");
let VillageState = village.VillageState;
describe("roadGraph",
function () {
let roadGraph = village.roadGraph;
it("Alice's house",
() => expect(roadGraph["Alice's House"]).toEqual(["Bob's House", "Cabin", "Post Office"]));
it("Bob's house",
() => expect(roadGraph["Bob's House"]).toEqual(
jasmine.objectContaining(["Alice's House"])));
});
describe("VillageState",
function () {
let first = new VillageState(
"Post Office",
[{ place: "Post Office", address: "Alice's House" }]
);
let next = first.move("Alice's House");
it("next place",
() => expect(next.place).toBe("Alice's House"));
it("next parcels",
() => expect(next.parcels).toEqual([]));
it("first place",
() => expect(first.place).toEqual("Post Office"));
}
);

48
labs/L12/village.js Normal file
View File

@ -0,0 +1,48 @@
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"
];
function buildGraph(edges) {
let graph = Object.create(null);
function addEdge(from, to) {
if (graph[from] == null) {
graph[from] = [to];
} else {
graph[from].push(to);
}
}
for (let [from, to] of edges.map(r => r.split("-"))) {
addEdge(from, to);
addEdge(to, from);
}
return graph;
}
const roadGraph = buildGraph(roads);
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);
}
}
}
exports.roadGraph = roadGraph;
exports.VillageState = VillageState;