-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-of-life-ui.ts
122 lines (102 loc) · 2.82 KB
/
game-of-life-ui.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// <reference path="game-of-life.ts" />
namespace Jason {
let grid: boolean[][] = [];
let gameOfLife: GameOfLife | null = null;
let iterateTimer = 0;
/**
* Get the timeout ms set by the user.
*/
function ms(): number {
return $('#ms').val() as number;
}
/**
* Start the timer and enable/disable buttons.
*/
function start() {
iterateTimer = setInterval(iterate, ms());
$('#start').attr('disabled', 'disabled');
$('#stop').removeAttr('disabled');
}
/**
* Stop the timer and enable/disable buttons.
*/
function stop() {
clearInterval(iterateTimer);
$('#stop').attr('disabled', 'disabled');
$('#start').removeAttr('disabled');
}
/**
* Set the game of life to the next iteration, and re-draw the grid.
* If the game of life is null, start it.
*/
function iterate() {
if(gameOfLife == null) {
gameOfLife = new GameOfLife(grid);
gameOfLife = gameOfLife.nextIteration();
}
else {
gameOfLife = gameOfLife.nextIteration();
}
grid = gameOfLife.gridCopy();
buildGrid(false);
}
/**
* Handle the onClick event for a table cell.
* Change the grid and flip the color.
*/
function tableOnClick(x: number, y: number): void {
let newLife = !grid[y][x];
grid[y][x] = newLife;
let $td = $(`#${x}x${y}`);
if(newLife)
$td.attr('class', 'live');
else
$td.attr('class', 'dead');
}
/**
* Build an HTML cell for an x/y pair.
*/
function $buildHtmlCell(x: number, y: number): JQuery<HTMLElement> {
let idString = `${x}x${y}`;
let tdClass = "dead";
if(y < grid.length && x < grid[0].length && grid[y][x])
tdClass = 'live';
let cell = $('<td>', {id: idString, class: tdClass});
cell.on('click', () => tableOnClick(x,y) );
return cell;
}
/**
* Construct and set the table element and the internal grid structure.
*/
function buildGrid(clearGrid: boolean): void {
let $table = $('<table>');
if(clearGrid) {
gameOfLife = null;
grid = [];
}
let width: number = $('#width').val() as number;
let height: number = $('#height').val() as number;
for(let y = 0; y < height; y++) {
let $rowHTML = $('<tr>');
let rowData: boolean[] = [];
for(let x = 0; x < width; x++) {
$rowHTML.append($buildHtmlCell(x, y));
rowData.push(false);
}
$table.append($rowHTML);
grid.push(rowData);
}
$('#table').html('')
$('#table').append($table)
}
window.onload = function() {
buildGrid(true);
$('#width').on('input',() => buildGrid(true));
$('#height').on('input',() => buildGrid(true));
$('#iterate').on('click',iterate);
$('#start').on('click', start);
$('#stop').attr('disabled', 'disabled');
$('#stop').on('click', stop);
$('#clear').on('click', () => buildGrid(true));
}
}