-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplus.js
283 lines (251 loc) · 6.91 KB
/
plus.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @constructor
*/
function Stream() {
this.listeners = [];
};
/**
* Adds a function as callback.
* @param {Function.<Object>} cb callback function that gets event.
*/
Stream.prototype.addCallback = function(cb) {
this.listeners.push(cb);
};
/**
* Triggers the event.
*/
Stream.prototype.trigger = function(evt) {
for (var i = 0; i < this.listeners.length; i++) {
try {
this.listeners[i](evt);
} catch(e) {
console.log(e);
}
}
};
/**
* @constructor
* @param {Number} N
*/
function Game(N) {
this.N = N;
this.state = [];
this.streams = [];
this.moveStream = new Stream();
for (var i = 0; i < N; i++) {
var row = [];
var streamrow = [];
for (var j = 0; j < N; j++) {
row.push(false);
streamrow.push(new Stream());
}
this.state.push(row);
this.streams.push(streamrow);
}
}
/**
* Play at position (i, j)
* @param {Number} i row number. (0 to N - 1)
* @param {Number} j col number. (0 to N - 1)
*/
Game.prototype.play = function(i, j) {
if (this.bounds(i, j)) {
var flips = [ [i, j], [i - 1, j], [i + 1, j], [i, j -1], [i, j + 1]];
for (var c = 0; c < flips.length; c++) {
this.flip(flips[c][0], flips[c][1]);
}
this.moveStream.trigger({ i : i, j : j});
}
};
/**
* Checks if i is within bounds.
* @param {Number} i row number. (0 to N - 1)
*/
Game.prototype.bound_chk = function(i) {
return 0 <= i && i < this.N;
};
/**
* Checks if (i, j) is within bounds.
*
* @param {Number} i row number. (0 to N - 1)
* @param {Number} j col number. (0 to N - 1)
*/
Game.prototype.bounds = function(i, j) {
return this.bound_chk(i) && this.bound_chk(j);
};
/**
* Flip that location.
* If invalid position no action is taken.
*
* @param {Number} i row number. (0 to N - 1)
* @param {Number} j col number. (0 to N - 1)
*/
Game.prototype.flip = function(i, j) {
if (this.bounds(i, j)) {
this.state[i][j] = !(this.state[i][j]);
this.streams[i][j].trigger(this.state[i][j])
}
};
/**
* Adds a callback that will get triggered if cell(i, j) changes
* with the new value of the cell.
*/
Game.prototype.addEventListener = function(evt, cb) {
evt.addCallback(this, cb);
};
/**
* @constructor
*
* @param {Number} i
* @param {Number} j
*/
function FlipEvent(i, j) {
this.i = i;
this.j = j;
}
FlipEvent.prototype.addCallback = function(game, cb) {
if (game.bounds(this.i, this.j)) {
game.streams[this.i][this.j].addCallback(cb);
}
};
/**
* @constructor
*/
function MoveEvent() {
}
MoveEvent.prototype.addCallback = function(game, cb) {
game.moveStream.addCallback(cb);
}
function newNode(parent, type) {
var n = document.createElement(type);
parent.appendChild(n);
return n;
}
function newTextNode(parent, txt) {
var n = document.createTextNode(txt);
parent.appendChild(n);
return n;
}
function parseList(hashSnip) {
var vals = [];
var qps = hashSnip.split(';');
for (var i = 0; i < qps.length; i++) {
var entry = qps[i];
var entrySplits = entry.split(',');
var list = [];
for (var j = 0; j < entrySplits.length; j++) {
list.push(entrySplits[j])
}
vals.push(list);
}
return vals;
}
function parseHash(hash) {
var vals = {};
if (hash) {
if (hash[0] == '#') {
var qps = hash.substr(1).split('&');
for (var i = 0; i < qps.length; i++) {
var entry = qps[i];
var entrySplits = entry.split('=');
var key = decodeURIComponent(entrySplits[0]);
var val = decodeURIComponent(entrySplits[1]);
vals[key] = val;
}
}
}
return vals;
}
function main(mainDiv, hash, hashcb) {
var hashStruct = parseHash(hash);
var N = hashStruct['N'];
if (N == null) {
N = 5;
hashcb('N=5');
} else {
N = parseInt(N);
}
var game = new Game(N);
var resetButton = newNode(mainDiv, 'input');
resetButton.setAttribute('type', 'button');
resetButton.setAttribute('value', 'Reset');
resetButton.addEventListener('click', function() {
hashcb('#N=' + N);
window.location.reload();
})
var nextLevel = newNode(mainDiv, 'input');
nextLevel.setAttribute('type', 'button');
nextLevel.setAttribute('value', 'Next Level');
nextLevel.addEventListener('click', function() {
hashcb('#N=' + (N+1));
window.location.reload();
})
var prevLevel = newNode(mainDiv, 'input');
prevLevel.setAttribute('type', 'button');
prevLevel.setAttribute('value', 'Previous Level');
prevLevel.addEventListener('click', function() {
hashcb('#N=' + (N-1));
window.location.reload();
})
var boardDiv = newNode(mainDiv, 'div');
boardDiv.style.display = 'table';
boardDiv.style.border = '2px solid #888888';
for (var i = 0; i < N; i++) {
var row = newNode(boardDiv, 'div');
row.style.display = 'table-row';
for (var j = 0; j < N; j++) {
var cell = newNode(row, 'div');
cell.style.display = 'table-cell';
cell.style.height = '50px';
cell.style.width = '50px';
cell.style['border'] = '2px solid black';
cell.style.padding = '2px';
cell.style['background-color'] = '#BBBBBB'
cell.addEventListener('click', (function(i_, j_) {
return function() {
game.play(i_, j_);
}
})(i, j));
game.addEventListener(new FlipEvent(i, j), (function(cell_) {
return function(val) {
if (val) {
cell_.style['background-color'] = '#333333';
} else {
cell_.style['background-color'] = '#BBBBBB';
};
}
})(cell));
}
}
var moves = [];
game.addEventListener(new MoveEvent(), function(evt) {
moves.push(evt);
var moveStrList = [];
for (var c = 0; c < moves.length; c++) {
moveStrList.push(moves[c].i + ',' + moves[c].j);
}
var snip = 'N=' + N + '&moves=' + moveStrList.join(';');
hashcb(snip);
})
if (hash) {
var moveSplits = hashStruct['moves'];
if (moveSplits) {
moveSplits = parseList(moveSplits);
}
if (moveSplits) {
console.log(moveSplits)
for (var c = 0; c < moveSplits.length; c++) {
if (moveSplits[c].length >= 2) {
game.play(parseInt(moveSplits[c][0]), parseInt(moveSplits[c][1]));
}
}
}
}
}
main(
document.getElementById('board'),
window.location.hash,
function(newhash) {
window.location.hash = newhash;
}
);