forked from srackley/the-game-of-life
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
134 lines (116 loc) · 4.8 KB
/
game.js
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
123
124
125
126
127
128
129
130
131
132
133
134
var mainElement = document.getElementById('main')
if (mainElement) {
var game = Life(mainElement)
// Connect #step_btn to the step function
document.getElementById('step_btn')
.addEventListener('click', game.step)
// TODO: Connect other buttons.
}
function Life(container, width=12, height=12) {
// Create boards for the present and future.
// Game boards are somewhat expensive to create, so we're going
// to be reusing them. Each time we step the game, `future`
// becomes `present` and vice versa.
var present = new Board(width, height);
var future = new Board(width, height);
// Create a <table> to hold our cells.
var table = createTable();
// Put the table in our container
container.appendChild(table);
// Add a mouse down listener to our table
table.addEventListener('mousedown', toggleCellFromEvent)
function createTable() {
// create <table> element
var table = document.createElement('table'); // <table
table.classList.add('board') // class='board'>
for (var r = 0; r < height; r++) {
var tr = document.createElement('tr'); // <tr>
for (var c = 0; c < width; c++) { // For instance, at r=2, c=3:
var td = document.createElement('td'); // <td
td.id = `${r}-${c}` // id="2-3">
// We'll put the coordinate on the cell
// Element itself, letting us fetch it
// in a click listener later.
td.coord = [r, c];
tr.appendChild(td); // </td>
}
table.appendChild(tr); // </tr>
} // </table>
return table
}
function toggleCellFromEvent(event) {
// FIXME: This currently always toggles cell (0, 0).
// How do we get the coordinate of the cell that was clicked on?
// HINT: https://developer.mozilla.org/en-US/docs/Web/API/Event/target
var cell = document.getElementById('0-0'); // ⬅️ Fix me
present.toggle(cell.coord)
paint()
}
function paint() {
// TODO:
// 1. For each <td> in the table:
// a. If its cell is alive, give the <td> the `alive` CSS class.
// b. Otherwise, remove the `alive` class.
//
// To find all the <td>s in the table, you might query the DOM for them, or you
// could choose to collect them when we create them in createTable.
//
// HINT:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
}
function step() {
// Hello, destructuring assignment:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
;[present, future] = tick(present, future); // tick is from board.js
// ⬆️ Why is there a semicolon at the beginning of this line?
//
// It's not necessary, but we have it there to avoid a confusing problem.
// Let's say you put a console.log at the start of this function, but
// forget the semicolon at the end of the line:
//
// console.log('entered step')
// [present, future] = tick(present, future);
//
// This is the one case in which automatic semicolon insertion can really bite
// you. Since the line begins with a `[` token, JS will assume that it *continues*
// the previous expression. That is, it will condense your code to this:
//
// console.log('entered step')[present, future] = tick(present, future);
//
// Which will throw the confusing error:
//
// TypeError: Cannot read property '#<Board>' of undefined
//
// Since it's trying to look up `present` inside the return value of `console.log`,
// which is undefined.
//
// In general, you should either be fastidious about ending lines with semicolons,
// or you should be sure to *begin* with a semicolon any line that could continue
// an expression—that's any line that starts with a (, [, or ` character.
// Paint the new present
paint();
}
function play() {
// TODO:
// Start playing by running the `step` function
// automatically repeatedly every fixed time interval
// HINT:
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
}
function stop() {
// TODO: Stop autoplay.
// HINT:
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval
}
function togglePlaying() {
// TODO: If we're playing, stop. Otherwise, start playing.
}
function clear() {
// TODO: Clear the board
}
function random() {
// TODO: Randomize the board
}
return {play, step, stop, togglePlaying, random, clear}
};