Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfrac committed Sep 23, 2019
1 parent e92944a commit 4b7a625
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Somewhere in `html>head`:
<script src="./game-of-life.umd.js"></script>
```

Somewhere in JS code`:
Somewhere in JS code:
```js
const params = {
cols: 400, // matrix size: 400x400
Expand All @@ -39,7 +39,7 @@ function play() {

function playGame() {
if (!gameStarted) return;
game.calcGeneration();
game.liveOut();
window.requestAnimationFrame(playGame)
}

Expand Down
19 changes: 14 additions & 5 deletions dist/game-of-life.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ Object.defineProperty(exports, '__esModule', { value: true });
var Game = /** @class */ (function () {
function Game(elementId, options) {
this.options = options;
this.canvas = new Canvas(elementId, this.options);
this.matrix = [];
this.randomize();
this.canvas = new Canvas(elementId, {
rows: this.options.rows,
cols: this.options.cols,
color: this.options.color,
});
if (this.options.seed) {
this.matrix = this.options.seed;
}
else {
this.matrix = [];
this.randomize();
}
this.canvas.draw(this.matrix);
}
Game.prototype.calcGeneration = function () {
Game.prototype.liveOut = function () {
var newGeneration = JSON.parse(JSON.stringify(this.matrix));
for (var i = 0; i < this.options.cols; i++) {
for (var j = 0; j < this.options.rows; j++) {
Expand All @@ -23,13 +32,13 @@ var Game = /** @class */ (function () {
}
this.matrix = newGeneration;
this.canvas.draw(newGeneration);
return newGeneration;
};
Game.prototype.randomize = function () {
for (var i = 0; i < this.options.cols; i++) {
for (var j = 0; j < this.options.rows; j++) {
if (!this.matrix[i])
this.matrix[i] = Array(this.options.cols);
//if (!this.matrix[i][j]) this.matrix[i][j] =
this.matrix[i][j] = Math.random() >= 0.5 ? 1 : 0;
}
}
Expand Down
19 changes: 14 additions & 5 deletions dist/game-of-life.esm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
var Game = /** @class */ (function () {
function Game(elementId, options) {
this.options = options;
this.canvas = new Canvas(elementId, this.options);
this.matrix = [];
this.randomize();
this.canvas = new Canvas(elementId, {
rows: this.options.rows,
cols: this.options.cols,
color: this.options.color,
});
if (this.options.seed) {
this.matrix = this.options.seed;
}
else {
this.matrix = [];
this.randomize();
}
this.canvas.draw(this.matrix);
}
Game.prototype.calcGeneration = function () {
Game.prototype.liveOut = function () {
var newGeneration = JSON.parse(JSON.stringify(this.matrix));
for (var i = 0; i < this.options.cols; i++) {
for (var j = 0; j < this.options.rows; j++) {
Expand All @@ -19,13 +28,13 @@ var Game = /** @class */ (function () {
}
this.matrix = newGeneration;
this.canvas.draw(newGeneration);
return newGeneration;
};
Game.prototype.randomize = function () {
for (var i = 0; i < this.options.cols; i++) {
for (var j = 0; j < this.options.rows; j++) {
if (!this.matrix[i])
this.matrix[i] = Array(this.options.cols);
//if (!this.matrix[i][j]) this.matrix[i][j] =
this.matrix[i][j] = Math.random() >= 0.5 ? 1 : 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/game-of-life.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface IGameOptions {
export declare type CellType = 0 | 1;
export interface IGameOptions extends ICanvasOptions {
seed?: Array<CellType[]>;
}
export interface ICanvasOptions {
rows: number;
cols: number;
color?: string;
Expand All @@ -8,7 +12,7 @@ export declare class Game {
private canvas;
private matrix;
constructor(elementId: string, options: IGameOptions);
calcGeneration(): void;
liveOut(): Array<CellType[]>;
private randomize;
private calcNeighbours;
}
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

function playGame() {
if (!gameStarted) return;
game.calcGeneration();
game.liveOut();
window.requestAnimationFrame(playGame)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "game-of-life",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/game-of-life.cjs.js",
"module": "dist/game-of-life.esm.js",
"author": "mrfrac <fracturer@gmail.com>",
Expand Down
36 changes: 25 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export interface IGameOptions {
export type CellType = 0 | 1;

export interface IGameOptions extends ICanvasOptions {
seed?: Array<CellType[]>
}

export interface ICanvasOptions {
rows: number;
cols: number;
color?: string;
Expand All @@ -7,20 +13,28 @@ export interface IGameOptions {
export class Game {
private options: IGameOptions;
private canvas: Canvas;
private matrix: number[][];
private matrix: Array<CellType[]>;

public constructor(elementId: string, options: IGameOptions) {
this.options = options;
this.canvas = new Canvas(elementId, this.options);
this.matrix = [];

this.randomize();
this.canvas = new Canvas(elementId, {
rows: this.options.rows,
cols: this.options.cols,
color: this.options.color,
});

if (this.options.seed) {
this.matrix = this.options.seed;
} else {
this.matrix = [];
this.randomize();
}

this.canvas.draw(this.matrix);
}

calcGeneration(): void {
const newGeneration: number[][] = JSON.parse(JSON.stringify(this.matrix));
public liveOut(): Array<CellType[]> {
const newGeneration: Array<CellType[]> = JSON.parse(JSON.stringify(this.matrix));

for (let i = 0; i < this.options.cols; i++) {
for (let j = 0; j < this.options.rows; j++) {
Expand All @@ -32,13 +46,13 @@ export class Game {

this.matrix = newGeneration;
this.canvas.draw(newGeneration)
return newGeneration;
}

private randomize(): void {
for (let i = 0; i < this.options.cols; i++) {
for (let j = 0; j < this.options.rows; j++) {
if (!this.matrix[i]) this.matrix[i] = Array(this.options.cols);
//if (!this.matrix[i][j]) this.matrix[i][j] =
this.matrix[i][j] = Math.random() >= 0.5 ? 1 : 0;
}
}
Expand All @@ -64,10 +78,10 @@ export class Game {

class Canvas {
private canvas: HTMLCanvasElement;
private options: IGameOptions;
private options: ICanvasOptions;
private ctx: CanvasRenderingContext2D | null;

public constructor(elementId: string, options: IGameOptions) {
public constructor(elementId: string, options: ICanvasOptions) {
this.canvas = document.getElementById(elementId) as HTMLCanvasElement;
this.options = options;
this.ctx = this.canvas.getContext("2d");
Expand Down

0 comments on commit 4b7a625

Please # to comment.