|
|
@ -2,21 +2,21 @@
|
|
|
|
* Implemented in TypeScript
|
|
|
|
* Implemented in TypeScript
|
|
|
|
* To learn more about TypeScript, please visit http://www.typescriptlang.org/
|
|
|
|
* To learn more about TypeScript, please visit http://www.typescriptlang.org/
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
module Conway {
|
|
|
|
namespace Conway {
|
|
|
|
|
|
|
|
|
|
|
|
export class Cell {
|
|
|
|
export class Cell {
|
|
|
|
public row: number;
|
|
|
|
public row: number;
|
|
|
|
public col: number;
|
|
|
|
public col: number;
|
|
|
|
public live: boolean;
|
|
|
|
public live: boolean;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(row: number, col: number, live: boolean) {
|
|
|
|
constructor(row: number, col: number, live: boolean) {
|
|
|
|
this.row = row;
|
|
|
|
this.row = row;
|
|
|
|
this.col = col;
|
|
|
|
this.col = col;
|
|
|
|
this.live = live
|
|
|
|
this.live = live;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class GameOfLife {
|
|
|
|
export class GameOfLife {
|
|
|
|
private gridSize: number;
|
|
|
|
private gridSize: number;
|
|
|
|
private canvasSize: number;
|
|
|
|
private canvasSize: number;
|
|
|
@ -28,8 +28,8 @@ module Conway {
|
|
|
|
private cellSize: number;
|
|
|
|
private cellSize: number;
|
|
|
|
private context: CanvasRenderingContext2D;
|
|
|
|
private context: CanvasRenderingContext2D;
|
|
|
|
private world;
|
|
|
|
private world;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
constructor() {
|
|
|
|
this.gridSize = 50;
|
|
|
|
this.gridSize = 50;
|
|
|
|
this.canvasSize = 600;
|
|
|
|
this.canvasSize = 600;
|
|
|
@ -42,14 +42,14 @@ module Conway {
|
|
|
|
this.world = this.createWorld();
|
|
|
|
this.world = this.createWorld();
|
|
|
|
this.circleOfLife();
|
|
|
|
this.circleOfLife();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public createWorld() {
|
|
|
|
public createWorld() {
|
|
|
|
return this.travelWorld( (cell : Cell) => {
|
|
|
|
return this.travelWorld( (cell : Cell) => {
|
|
|
|
cell.live = Math.random() < this.initialLifeProbability;
|
|
|
|
cell.live = Math.random() < this.initialLifeProbability;
|
|
|
|
return cell;
|
|
|
|
return cell;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public circleOfLife() : void {
|
|
|
|
public circleOfLife() : void {
|
|
|
|
this.world = this.travelWorld( (cell: Cell) => {
|
|
|
|
this.world = this.travelWorld( (cell: Cell) => {
|
|
|
|
cell = this.world[cell.row][cell.col];
|
|
|
|
cell = this.world[cell.row][cell.col];
|
|
|
@ -57,8 +57,8 @@ module Conway {
|
|
|
|
return this.resolveNextGeneration(cell);
|
|
|
|
return this.resolveNextGeneration(cell);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setTimeout( () => {this.circleOfLife()}, this.animationRate);
|
|
|
|
setTimeout( () => {this.circleOfLife()}, this.animationRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public resolveNextGeneration(cell : Cell) {
|
|
|
|
public resolveNextGeneration(cell : Cell) {
|
|
|
|
var count = this.countNeighbors(cell);
|
|
|
|
var count = this.countNeighbors(cell);
|
|
|
|
var newCell = new Cell(cell.row, cell.col, cell.live);
|
|
|
|
var newCell = new Cell(cell.row, cell.col, cell.live);
|
|
|
@ -66,7 +66,7 @@ module Conway {
|
|
|
|
else if(count == 3) newCell.live = true;
|
|
|
|
else if(count == 3) newCell.live = true;
|
|
|
|
return newCell;
|
|
|
|
return newCell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public countNeighbors(cell : Cell) {
|
|
|
|
public countNeighbors(cell : Cell) {
|
|
|
|
var neighbors = 0;
|
|
|
|
var neighbors = 0;
|
|
|
|
for(var row = -1; row <=1; row++) {
|
|
|
|
for(var row = -1; row <=1; row++) {
|
|
|
@ -79,12 +79,12 @@ module Conway {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return neighbors;
|
|
|
|
return neighbors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public isAlive(row : number, col : number) {
|
|
|
|
public isAlive(row : number, col : number) {
|
|
|
|
if(row < 0 || col < 0 || row >= this.gridSize || col >= this.gridSize) return false;
|
|
|
|
if(row < 0 || col < 0 || row >= this.gridSize || col >= this.gridSize) return false;
|
|
|
|
return this.world[row][col].live;
|
|
|
|
return this.world[row][col].live;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public travelWorld(callback) {
|
|
|
|
public travelWorld(callback) {
|
|
|
|
var result = [];
|
|
|
|
var result = [];
|
|
|
|
for(var row = 0; row < this.gridSize; row++) {
|
|
|
|
for(var row = 0; row < this.gridSize; row++) {
|
|
|
@ -93,20 +93,20 @@ module Conway {
|
|
|
|
rowData.push(callback(new Cell(row, col, false)));
|
|
|
|
rowData.push(callback(new Cell(row, col, false)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(rowData);
|
|
|
|
result.push(rowData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public draw(cell : Cell) {
|
|
|
|
public draw(cell : Cell) {
|
|
|
|
if(this.context == null) this.context = this.createDrawingContext();
|
|
|
|
if(this.context == null) this.context = this.createDrawingContext();
|
|
|
|
if(this.cellSize == 0) this.cellSize = this.canvasSize/this.gridSize;
|
|
|
|
if(this.cellSize == 0) this.cellSize = this.canvasSize/this.gridSize;
|
|
|
|
|
|
|
|
|
|
|
|
this.context.strokeStyle = this.lineColor;
|
|
|
|
this.context.strokeStyle = this.lineColor;
|
|
|
|
this.context.strokeRect(cell.row * this.cellSize, cell.col*this.cellSize, this.cellSize, this.cellSize);
|
|
|
|
this.context.strokeRect(cell.row * this.cellSize, cell.col*this.cellSize, this.cellSize, this.cellSize);
|
|
|
|
this.context.fillStyle = cell.live ? this.liveColor : this.deadColor;
|
|
|
|
this.context.fillStyle = cell.live ? this.liveColor : this.deadColor;
|
|
|
|
this.context.fillRect(cell.row * this.cellSize, cell.col*this.cellSize, this.cellSize, this.cellSize);
|
|
|
|
this.context.fillRect(cell.row * this.cellSize, cell.col*this.cellSize, this.cellSize, this.cellSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public createDrawingContext() {
|
|
|
|
public createDrawingContext() {
|
|
|
|
var canvas = <HTMLCanvasElement> document.getElementById('conway-canvas');
|
|
|
|
var canvas = <HTMLCanvasElement> document.getElementById('conway-canvas');
|
|
|
|
if(canvas == null) {
|
|
|
|
if(canvas == null) {
|
|
|
|