Commit ede34aac authored by Adrian Kosmaczewski's avatar Adrian Kosmaczewski

Changed the TypeScript World class to use a Map

parent 8749f8a6
......@@ -3,9 +3,6 @@ export enum Cell {
dead
}
export function tableDescription(cell: Cell): string {
if (cell == Cell.alive) {
return " x |"
}
return " |"
export function cellToString(cell: Cell | undefined): string {
return cell == Cell.alive ? " x |" : " |";
}
import { Cell } from "./Cell"
import { tableDescription } from "./Cell"
import { Coord } from "./Coord"
import {Cell, cellToString} from "./Cell"
import {Coord} from "./Coord"
// Adapted from
// https://stackoverflow.com/a/14760377/133764
......@@ -17,7 +16,7 @@ let makeRange = (size: number): number[] => {
}
export class World {
cells: { [coord: string]: Cell } = {}
cells = new Map<string, Cell>();
constructor(public size: number, aliveCells: string[]) {
this.size = size
......@@ -26,10 +25,10 @@ export class World {
for (let b of range) {
let coord = `${a}:${b}`
if (aliveCells !== null && aliveCells.indexOf(coord) >= 0) {
this.cells[coord] = Cell.alive
this.cells.set(coord, Cell.alive)
}
else {
this.cells[coord] = Cell.dead
this.cells.set(coord, Cell.dead)
}
}
}
......@@ -38,8 +37,7 @@ export class World {
evolve(): World {
let alive = Array<string>()
let range = [-1, 0, 1]
Object.keys(this.cells).forEach(coordText => {
let cell = this.cells[coordText]
this.cells.forEach((cell, coordText) => {
let c = Coord.createFromString(coordText)
let count = 0
......@@ -49,7 +47,7 @@ export class World {
}).filter((c) => {
return c != coordText
}).map((c) => {
return this.cells[c]
return this.cells.get(c)
}).filter((o) => {
return o == Cell.alive
}).length
......@@ -70,8 +68,7 @@ export class World {
}
}
})
let world = new World(this.size, alive)
return world
return new World(this.size, alive)
}
toString(): string {
......@@ -90,8 +87,8 @@ export class World {
str += ` ${paddingLeft(a, " ")}|`
for (let b of range) {
let coord = `${b}:${a}`
let cell = this.cells[coord]
let cellDescription = tableDescription(cell)
let cell = this.cells.get(coord)
let cellDescription = cellToString(cell)
str += `${cellDescription}`
}
str += "\n"
......@@ -99,53 +96,52 @@ export class World {
return str
}
static blinker(coord: Coord): string[] {
static blinker(origin: Coord): string[] {
return [
`${coord.x}:${coord.y}`,
`${coord.x + 1}:${coord.y}`,
`${coord.x + 2}:${coord.y}`
`${origin.x}:${origin.y}`,
`${origin.x + 1}:${origin.y}`,
`${origin.x + 2}:${origin.y}`
]
}
static beacon(coord: Coord): string[] {
static beacon(origin: Coord): string[] {
return [
`${coord.x}:${coord.y}`,
`${coord.x + 1}:${coord.y}`,
`${coord.x}:${coord.y + 1}`,
`${coord.x + 1}:${coord.y + 1}`,
`${coord.x + 2}:${coord.y + 2}`,
`${coord.x + 3}:${coord.y + 2}`,
`${coord.x + 2}:${coord.y + 3}`,
`${coord.x + 3}:${coord.y + 3}`
`${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}`
]
}
static glider(coord: Coord): string[] {
static glider(origin: Coord): string[] {
return [
`${coord.x + 2}:${coord.y + 2}`,
`${coord.x + 1}:${coord.y + 2}`,
`${coord.x}:${coord.y + 2}`,
`${coord.x + 2}:${coord.y + 1}`,
`${coord.x + 1}:${coord.y}`
`${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}`
]
}
static block(coord: Coord): string[] {
static block(origin: Coord): string[] {
return [
`${coord.x}:${coord.y}`,
`${coord.x + 1}:${coord.y}`,
`${coord.x}:${coord.y + 1}`,
`${coord.x + 1}:${coord.y + 1}`
`${origin.x}:${origin.y}`,
`${origin.x + 1}:${origin.y}`,
`${origin.x}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y + 1}`
]
}
static tub(coord: Coord): string[] {
static tub(origin: Coord): string[] {
return [
`${coord.x + 1}:${coord.y}`,
`${coord.x}:${coord.y + 1}`,
`${coord.x + 2}:${coord.y + 1}`,
`${coord.x + 1}:${coord.y + 2}`
`${origin.x + 1}:${origin.y}`,
`${origin.x}:${origin.y + 1}`,
`${origin.x + 2}:${origin.y + 1}`,
`${origin.x + 1}:${origin.y + 2}`
]
}
}
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