Commit 4bf8467a authored by Adrian Kosmaczewski's avatar Adrian Kosmaczewski

Adapted code to use Map instead of classic JS object

parent ede34aac
......@@ -5,7 +5,7 @@ export class Coord {
return `${this.x}:${this.y}`
}
static createFromString(string: string): Coord {
static fromString(string: string): Coord {
let parts: string[] = string.split(":")
let c: number[] = parts.map(parseFloat)
return new Coord(c[0], c[1])
......
......@@ -18,37 +18,38 @@ let makeRange = (size: number): number[] => {
export class World {
cells = new Map<string, Cell>();
constructor(public size: number, aliveCells: string[]) {
constructor(public size: number, aliveCells: Array<Coord>) {
this.size = size
let aliveCellStrings : string[] = aliveCells.map(item => item.toString() )
let range = makeRange(size)
for (let a of range) {
for (let b of range) {
let coord = `${a}:${b}`
if (aliveCells !== null && aliveCells.indexOf(coord) >= 0) {
this.cells.set(coord, Cell.alive)
let coord = new Coord(a, b)
if (aliveCellStrings.includes(coord.toString())) {
this.cells.set(coord.toString(), Cell.alive)
}
else {
this.cells.set(coord, Cell.dead)
this.cells.set(coord.toString(), Cell.dead)
}
}
}
}
evolve(): World {
let alive = Array<string>()
let alive = Array<Coord>()
let range = [-1, 0, 1]
this.cells.forEach((cell, coordText) => {
let c = Coord.createFromString(coordText)
let coord = Coord.fromString(coordText)
let count = 0
range.map((a) => {
count += range.map((b) => {
return `${c.x + a}:${c.y + b}`
}).filter((c) => {
range.map(a => {
count += range.map(b => {
return (new Coord(coord.x + a, coord.y + b)).toString()
}).filter(c => {
return c != coordText
}).map((c) => {
}).map(c => {
return this.cells.get(c)
}).filter((o) => {
}).filter(o => {
return o == Cell.alive
}).length
})
......@@ -56,13 +57,13 @@ export class World {
switch (cell) {
case Cell.alive: {
if (count == 2 || count == 3) {
alive.push(coordText)
alive.push(coord)
}
break
}
case Cell.dead: {
if (count == 3) {
alive.push(coordText)
alive.push(coord)
}
break
}
......@@ -96,52 +97,52 @@ export class World {
return str
}
static blinker(origin: Coord): string[] {
static blinker(origin: Coord): Array<Coord> {
return [
`${origin.x}:${origin.y}`,
`${origin.x + 1}:${origin.y}`,
`${origin.x + 2}:${origin.y}`
new Coord(origin.x, origin.y),
new Coord(origin.x + 1, origin.y),
new Coord(origin.x + 2, origin.y)
]
}
static beacon(origin: Coord): string[] {
static beacon(origin: Coord): Array<Coord> {
return [
`${origin.x}:${origin.y}`,
`${origin.x + 1}:${origin.y}`,
`${origin.x}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y + 1}`,
`${origin.x + 2}:${origin.y + 2}`,
`${origin.x + 3}:${origin.y + 2}`,
`${origin.x + 2}:${origin.y + 3}`,
`${origin.x + 3}:${origin.y + 3}`
new Coord(origin.x, origin.y),
new Coord(origin.x + 1, origin.y),
new Coord(origin.x, origin.y + 1),
new Coord(origin.x + 1, origin.y + 1),
new Coord(origin.x + 2, origin.y + 2),
new Coord(origin.x + 3, origin.y + 2),
new Coord(origin.x + 2, origin.y + 3),
new Coord(origin.x + 3, origin.y + 3)
]
}
static glider(origin: Coord): string[] {
static glider(origin: Coord): Array<Coord> {
return [
`${origin.x + 2}:${origin.y + 2}`,
`${origin.x + 1}:${origin.y + 2}`,
`${origin.x}:${origin.y + 2}`,
`${origin.x + 2}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y}`
new Coord(origin.x + 2, origin.y + 2),
new Coord(origin.x + 1, origin.y + 2),
new Coord(origin.x, origin.y + 2),
new Coord(origin.x + 2, origin.y + 1),
new Coord(origin.x + 1, origin.y)
]
}
static block(origin: Coord): string[] {
static block(origin: Coord): Array<Coord> {
return [
`${origin.x}:${origin.y}`,
`${origin.x + 1}:${origin.y}`,
`${origin.x}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y + 1}`
new Coord(origin.x, origin.y),
new Coord(origin.x + 1, origin.y),
new Coord(origin.x, origin.y + 1),
new Coord(origin.x + 1, origin.y + 1)
]
}
static tub(origin: Coord): string[] {
static tub(origin: Coord): Array<Coord> {
return [
`${origin.x + 1}:${origin.y}`,
`${origin.x}:${origin.y + 1}`,
`${origin.x + 2}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y + 2}`
new Coord(origin.x + 1, origin.y),
new Coord(origin.x, origin.y + 1),
new Coord(origin.x + 2, origin.y + 1),
new Coord(origin.x + 1, origin.y + 2)
]
}
}
......@@ -35,10 +35,9 @@ export class WorldView {
this.context.lineWidth = 3
this.context.strokeStyle = "white"
this.context.fillStyle = "black"
Object.keys(this.currentWorld.cells).forEach(coordText => {
let cell = this.currentWorld.cells[coordText]
this.currentWorld.cells.forEach((cell, coordText) => {
if (cell == Cell.alive) {
let coord = Coord.createFromString(coordText)
let coord = Coord.fromString(coordText)
const x = coord.x * horizontalStep
const y = coord.y * verticalStep
this.context?.fillRect(x, y, horizontalStep, verticalStep)
......
......@@ -2,12 +2,13 @@ import { World } from './World'
import { Coord } from './Coord'
import { WorldView } from './WorldView'
let alive = World.blinker(new Coord(0, 1))
alive = alive.concat(World.beacon(new Coord(10, 10)))
alive = alive.concat(World.glider(new Coord(4, 5)))
alive = alive.concat(World.block(new Coord(1, 10)))
alive = alive.concat(World.block(new Coord(18, 3)))
alive = alive.concat(World.tub(new Coord(6, 1)))
let blinker = World.blinker(new Coord(0, 1))
let beacon = World.beacon(new Coord(10, 10))
let glider = World.glider(new Coord(4, 5))
let block1 = World.block(new Coord(1, 10))
let block2 = World.block(new Coord(18, 3))
let tub = World.tub(new Coord(6, 1))
let alive = blinker.concat(beacon).concat(glider).concat(block1).concat(block2).concat(tub)
let canvas = document.getElementById('worldView') as HTMLCanvasElement
let generationLabel = document.getElementById('generationLabel') as HTMLParagraphElement
......@@ -15,8 +16,6 @@ let worldView = new WorldView(canvas)
let world = new World(30, alive)
let generation = 0
console.warn('Starting')
console.log(`${world}`)
worldView.world = world
......
......@@ -14,12 +14,13 @@ const sleep = (ms : number) => {
})
}
let alive = World.blinker(new Coord(0, 1))
alive = alive.concat(World.beacon(new Coord(10, 10)))
alive = alive.concat(World.glider(new Coord(4, 5)))
alive = alive.concat(World.block(new Coord(1, 10)))
alive = alive.concat(World.block(new Coord(18, 3)))
alive = alive.concat(World.tub(new Coord(6, 1)))
let blinker = World.blinker(new Coord(0, 1))
let beacon = World.beacon(new Coord(10, 10))
let glider = World.glider(new Coord(4, 5))
let block1 = World.block(new Coord(1, 10))
let block2 = World.block(new Coord(18, 3))
let tub = World.tub(new Coord(6, 1))
let alive = blinker.concat(beacon).concat(glider).concat(block1).concat(block2).concat(tub)
let world = new World(30, alive)
let generation = 0
......
......@@ -23,10 +23,10 @@ tape("Blinker", (t : any) => {
let alive = World.blinker(new Coord(0, 1));
let original = new World(3, alive);
let gen1 = original.evolve();
let expectedAlive: string[] = [
"1:0",
"1:1",
"1:2"
let expectedAlive: Coord[] = [
new Coord(1, 0),
new Coord(1, 1),
new Coord(1, 2)
];
let expected = new World(3, expectedAlive);
t.equal(JSON.stringify(expected.cells), JSON.stringify(gen1.cells));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment