-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
424 lines (349 loc) · 13.7 KB
/
core.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
require([ "dojo/_base/declare", "dojo/dom-construct", "dojo/dom-style", "dojo/on", "dojo/query", "dojo/dom-class", "dojo/_base/lang", "dojo/dom", "dojox/timing" ],
function(declare, domConstruct, domStyle, on, query, domClass, lang, dom) {
declare("fenouiltonic.game100.gamemodel", null, {
_gameGraph: null,
_gameState: [],
_currentVertex: null,
_xmax: null,
_ymax: null,
_game: null,
_gameController: null,
constructor: function() {
},
setGameParameters: function(xmax, ymax, gameGraph) {
if (gameGraph == null) {
gameGraph = this._createDefaultGraph(xmax, ymax);
}
this._gameGraph = gameGraph;
this._xmax = xmax;
this._ymax = ymax;
// Fire notifications
if (this._gameController != null) {
this._gameController.gameParametersChanged();
}
},
_createDefaultGraph: function(xmax, ymax) {
var graph = {};
var mvtx = [2, 3, 2, 0, -2, -3, -2, 0];
var mvty = [2, 0, -2, -3, -2, 0, 2, 3];
for (var x = 0; x < xmax; x++) {
for (var y = 0; y < ymax; y++) {
var edges = [];
for (var m = 0; m < mvtx.length; m++) {
var newx = x + mvtx[m];
var newy = y + mvty[m];
if (newx >= 0 && newy >= 0 && newx < xmax && newy < ymax) { // in game boundaries ?
edges.push(":" + newx + "-" + newy);
}
}
graph[":" + x + "-" + y] = edges;
}
}
return graph;
},
getGameWidth: function () {
return this._xmax;
},
getGameHeight: function () {
return this._ymax;
},
setController: function(controller) {
this._gameController = controller;
},
isStarted: function() {
return this._gameState.length > 0;
},
isWon: function() {
return this._gameState.length == (this._xmax * this._ymax);
},
getCurrentCount: function() {
return this._gameState.length;
},
isLost: function() {
if (! this.isStarted()) {
return false;
}
if (this.isWon()) {
return false;
}
var edges = this.getEdges(this._currentVertex);
var cannotMove = true;
for (var i = 0; i < edges.length; i++) {
var cannotMoveToThisEdge = false;
for (var j = 0; j < this._gameState.length; j++) {
if (this._gameState[j] == edges[i]) {
cannotMoveToThisEdge = true;
break;
}
}
cannotMove = cannotMove && cannotMoveToThisEdge;
}
return cannotMove;
},
setGameState: function(currentVertex, gameState) {
if (currentVertex == null) {
throw "Missing arg 'currentVertex'";
}
this._currentVertex = currentVertex;
this._gameState = typeof gameState !== 'undefined' ? gameState : [];
// Fire notifications
if (this._gameController != null) {
this._gameController.gameStateChanged();
}
},
getGameState: function() {
return this._gameState;
},
startNewGame: function() {
this._currentVertex = null;
this._gameState = [];
// Fire notifications
if (this._gameController != null) {
this._gameController.gameStateChanged();
}
},
undo: function() {
if (this._currentVertex == null) {
return; // Very beginning, nothing to do !
}
this._gameState.pop();
if (this._gameState.length > 0) {
this._currentVertex = this._gameState[this._gameState.length - 1];
} else {
this._currentVertex = null;
}
// Fire notifications
if (this._gameController != null) {
this._gameController.gameStateChanged();
}
},
canMoveTo: function(vertex) {
if (vertex == null) {
throw "Argument 'vertex' cannot be null";
}
try {
vertex = this.ensureValidVertex(vertex);
} catch (e) {
return false;
}
// Cannot move if the edges is already visited
for (var i = 0; i < this._gameState.length; i++) {
if (this._gameState[i] == vertex) {
return false;
}
}
if (this._currentVertex != null) {
// Can move if edge is in the current vertex's edge list
var edges = this.getEdges(this._currentVertex);
for (var i = 0; i < edges.length; i++) {
if (edges[i] == vertex) {
return true;
}
}
return false;
}
return true;
},
moveTo: function(vertex) {
if (! this.canMoveTo(vertex)) {
throw "Cannot move to vertex '" + vertex + "'";
}
this._gameState.push(vertex);
this._currentVertex = vertex;
},
getEdges: function(vertex) {
vertex = this.ensureValidVertex(vertex);
var edges = this._gameGraph[vertex];
return edges;
},
isValidVertex: function(vertex) {
if (vertex == null) {
throw "Argument 'vertex' cannot be null";
}
try {
this.ensureValidVertex(vertex);
} catch (e) {
return false;
}
return true;
},
ensureValidVertex: function(vertex) {
if (vertex == null && this._currentVertex == null) {
throw "Argument 'vertex' is null and there is no current vertex.";
}
var v = vertex != null ? vertex : this._currentVertex;
if (! this._gameGraph.hasOwnProperty(v)) {
throw "There is no vertex by that name '" + v + "'";
}
return v;
},
getCurrentVertex: function() {
return this._currentVertex;
}
});
declare("fenouiltonic.game100.gamecontroller", null, {
_gameModel: null,
_gameBoard: null,
_infoView: null,
constructor: function() {
},
setModel: function(model) {
this._gameModel = model;
},
setBoard: function(board) {
this._gameBoard = board;
},
setInfoView: function(view) {
this._infoView = view;
},
gameParametersChanged: function() {
if (this._gameBoard != null) {
this._gameBoard.createDOM();
// TODO
}
},
gameStateChanged: function() {
if (this._gameBoard != null) {
this._gameBoard.clearDOM();
// TODO
}
if (this._infoView != null) {
this._infoView.setMessage("");
this._infoView.setClock("00:00");
// TODO
}
},
moveTo: function(cell) {
if (this._gameModel.isLost() || this._gameModel.isWon()) {
return false;
}
var allowed = this._gameModel.canMoveTo(cell);
if (! allowed) {
this._infoView.setMessage("You cannot move to that cell");
return false;
}
this._infoView.setMessage("");
this._gameModel.moveTo(cell);
if (this._gameModel.isLost()) {
this._infoView.setMessage("You lost !");
}
if (this._gameModel.isWon()) {
this._infoView.setMessage("You won !");
}
return true;
},
});
declare("fenouiltonic.game100.infoview", null, {
_infoViewRootNode: null,
_messageNode: null,
_clockNode: null,
constructor: function(domNode) {
this._infoViewRootNode = domNode;
this.createDOM();
},
createDOM: function() {
var domNode = this._infoViewRootNode;
// Clear everything
domConstruct.empty(domNode);
// and build the DOM structure
var tableNode = domConstruct.create("table", { 'class': 'game-info' }, domNode);
var tr = domConstruct.create("tr", {}, tableNode);
this._messageNode = domConstruct.create("td", { 'class': "game-message" }, tr);
this._clockNode = domConstruct.create("td", { 'class': "game-clock" }, tr);
},
setMessage: function(str) {
this._messageNode.textContent = str;
},
setClock: function(str) {
this._clockNode.textContent = str;
}
});
declare("fenouiltonic.game100.gameboard", null, {
_gameModel: null,
_gameController: null,
_gameTableNode: null,
_gameBoardRootNode: null,
_displayPossibleCells: false,
constructor: function(domNode) {
this._gameBoardRootNode = domNode;
},
setModel: function(model) {
this._gameModel = model;
},
setDisplayPossibleCells: function(b) {
this._displayPossibleCells = b;
},
setController: function(controller) {
this._gameController = controller;
},
createDOM: function() {
var xmax = this._gameModel.getGameWidth();
var ymax = this._gameModel.getGameHeight();
var domNode = this._gameBoardRootNode;
// Clear everything
domConstruct.empty(domNode);
// and build the DOM structure
this._gameTableNode = domConstruct.create("table", { 'class': 'game-board' }, domNode);
for (var x = 0; x < xmax; x++) {
var tr = domConstruct.create("tr", {}, this._gameTableNode);
for (var y = 0; y < ymax; y++) {
var td = domConstruct.create("td", { 'class': ((x + y) % 2 == 0 ? 'even-cell' : 'odd-cell'),
id: ":" + x + "-" + y }, tr);
on(td, "click", dojo.hitch(this, "_evtClick"));
}
}
// Adjust the table height to be equal to its height
var width = domStyle.get(this._gameTableNode, "width");
domStyle.set(this._gameTableNode, "height", width + "px");
},
clearDOM: function() {
// Clear CSS Classes
query(".active-cell, .used-cell, .possible-cell, .impossible-cell", this._gameTableNode)
.removeClass("active-cell")
.removeClass("used-cell")
.removeClass("possible-cell")
.removeClass("impossible-cell");
// Remove text content
query("td", this._gameTableNode)
.forEach(function (node) {
node.textContent = "";
});
var usedCells = this._gameModel.getGameState();
var cell = null;
for (var i = 0; i < usedCells.length; i++) {
cell = dom.byId(usedCells[i]);
cell.textContent = (i + 1)
domClass.add(cell, "used-cell");
}
if (cell != null) {
domClass.add(cell, "active-cell");
this._displayPossibleCellsIfNeeded();
}
},
_displayPossibleCellsIfNeeded: function() {
if (this._displayPossibleCells) {
var possibleCells = this._gameModel.getEdges();
for (var i = 0; i < possibleCells.length; i++) {
var cell = possibleCells[i];
domClass.add(cell, domClass.contains(cell, "used-cell") ? "impossible-cell" : "possible-cell");
}
}
},
_evtClick: function(evt) {
var cellID = evt.target.id;
var cellNode = evt.target;
var allowed = this._gameController.moveTo(cellID)
if (allowed) {
// Clear CSS Classes
query(".active-cell, .possible-cell, .impossible-cell", this._gameTableNode)
.removeClass("active-cell")
.removeClass("possible-cell")
.removeClass("impossible-cell");
cellNode.textContent = this._gameModel.getCurrentCount();
domClass.add(cellNode, "active-cell");
domClass.add(cellNode, "used-cell");
this._displayPossibleCellsIfNeeded();
}
}
});
});