-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
545 lines (439 loc) · 12.3 KB
/
snake.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
(function ( globals ) {
// Defining helpers
var $ = globals.document;
/**
* Query Selector shorthand
* @param selector
* @param context
* @returns {*}
*/
var $$ = function (selector, context) {
return selector ? $.querySelector(selector, context || $) : $;
};
/**
* Extend an object with default values
* @param options
* @param defaults
* @returns {*|{}}
*/
var $$d = function (options, defaults) {
var _options = options || {};
for (var opt in defaults)
if (defaults.hasOwnProperty(opt) && !_options.hasOwnProperty(opt))
_options[opt] = defaults[opt];
return _options;
};
/**
* Emit event on board element
* @param event
*/
var $$e = function(eventName, context) {
var event,
context = context || $;
if ($.createEvent) {
event = $.createEvent("HTMLEvents");
event.initEvent(eventName, true, true);
} else {
event = $.createEventObject();
event.eventType = eventName;
}
event.eventName = eventName;
if ($.createEvent) {
context.dispatchEvent(event);
} else {
context.fireEvent("on" + event.eventType, event);
}
};
var Directions = {
UP: 0,
RIGHT: 1,
DOWN: 2,
LEFT: 3
};
var DirectionsInverse = {
0: 'up',
1: 'right',
2: 'down',
3: 'left'
};
/**
* Board object, manage the elements positioning and limits
* @param options
* @returns {Board}
* @constructor
*/
var Board = function( options ) {
// calling constructor if not invoked
if (this.__proto__.constructor !== Board) {
return new Board(options);
}
// Fullfill options with default values
var _defaults = {
'element': $$('.board'),
'columns': 32,
'rows': 16,
'cell-width': 24,
'cell-height': 24,
'timing': 300,
'food-count': 1,
'food-replace': true
};
options = $$d(options, _defaults);
// Private properties
var self = this;
var _board,
_$board = options.element,
_snakes;
var _loop;
var _moveSnakes = function() {
_snakes.forEach(function(s) { s.move() });
}
/**
* Process cicle
* @private
*/
var _cicle = function() {
$$e('snake-cicle-start');
_moveSnakes();
$$e('snake-cicle-end');
};
/**
* Find board cell
* @param x
* @param y
* @returns {*}
*/
var _cell = function(x,y) {
// the board include walls
return _board[y+1][x+1];
};
// Public properties
/**
* Initialize the board and register snakes to it
* @returns {Board}
*/
self.init = function() {
// Setup board
_board = [];
for (var y = -1; y <= options.rows; y++) {
// setting up as array of rows ...
var row = [];
for (var x = -1; x <= options.columns; x++) {
// each row contains an array of columns (cells)
row.push({
x: x,
y: y,
block: null,
isFood: false,
isEmpty: true,
isWall: ((y < 0) || (x < 0) || (y == options.rows) || (x == options.columns))
});
}
_board.push(row);
}
// override the board size
_$board.style.height = options.rows * options['cell-height'];
_$board.style.width = options.columns * options['cell-width'];
return self;
};
/**
* Register snake(s) to the board
* @param snakes
* @returns {boolean}
*/
self.register = function( snakes ) {
if (snakes.__proto__.constructor === Snake) snakes = [snakes];
// Validate snakes
var fakeSnakes = [];
snakes.forEach(function(s, i) {
if (s.__proto__.constructor !== Snake)
fakeSnakes.push(i+1);
});
if (fakeSnakes.length) {
console.error('Some snakes given are not valid ' + fakeSnakes.toString());
return false;
}
_snakes = snakes;
_snakes.forEach(function(s) { s.setBoardHook(self) });
return self;
}
/**
* Start game cicles
* @returns {Board}
*/
self.play = function() {
_loop = setInterval(_cicle, options.timing);
return self;
};
/**
* Stop game cicles
* @returns {Board}
*/
self.pause = function() {
clearInterval(_loop);
_loop = null;
_snakes.forEach(function(s) { s.stop() });
return self;
};
self.stop = self.pause;
self.togglePause = function() {
if (_loop) return self.pause();
else return self.play();
};
self.placeBlock = function( block, x, y, snake ) {
if (typeof y === 'undefined') y = 0;
if (typeof x === 'undefined') x = 0;
if (typeof block === 'undefined')
return null;
var boardX = x * options['cell-width'],
boardY = y * options['cell-height'],
$el = block.element;
// empty the leaving board cell
_cell(block.x, block.y).block = null;
// setting current block position
block.x = x;
block.y = y;
// moving DOM element to the right position
$el.style.left = boardX;
$el.style.top = boardY;
var enteringCell = _cell(x,y);
// if is the head moving check for collisions
if (block.isHead) {
if ( (enteringCell.block) || (enteringCell.isWall) ) {
snake.gameover();
return self;
}
if (enteringCell.isFood) {
self.removeFood(x,y);
snake.extend();
return self;
}
}
// update cell load
enteringCell.block = block;
};
self.placeFood = function(x, y) {
if ( (typeof y === 'undefined') || (typeof x === 'undefined') ) {
// generate position
do {
x = Math.floor(Math.random() * options.columns);
y = Math.floor(Math.random() * options.rows);
} while ( (_cell(x, y).block) || (_cell(x, y).$el) );
}
var boardX = x * options['cell-width'],
boardY = y * options['cell-height'],
$food;
$food = $.createElement('div');
$food.className = 'food';
// moving DOM element to the right position
$food.style.left = boardX;
$food.style.top = boardY;
_$board.appendChild($food);
_cell(x, y).isFood = true;
_cell(x, y).$el = $food;
return self;
};
self.removeFood = function(x,y) {
_cell(x,y).isFood = false;
_cell(x,y).$el.parentNode.removeChild(_cell(x,y).$el);
_cell(x,y).$el = null;
if(options['food-replace'])
self.placeFood();
};
return self.init();
};
/**
* Snake object, any snake action pass through this
* @param options
* @returns {Snake}
* @constructor
*/
var Snake = function( options ) {
// calling constructor if not invoked
if (this.__proto__.constructor !== Snake) {
return new Snake(options);
}
// Fullfill options with default values
var _defaults = {
'element': $$('.snake'),
'x': 0,
'y': 0,
'size': 4
};
options = $$d(options, _defaults);
// Private properties
var self = this;
var _board = null,
_$snake = options.element;
var _blocks,
_direction,
_newDirection,
_isDirectionChanged,
_isGameOver;
/**
* Set a new direction in "updating" state (wait next pulse) and check if the direction is valid
* @param d
* @returns {Snake}
* @private
*/
var _updateDirection = function( d ) {
// if is the same direction
if (d === _direction)
return self;
// avoid snake turning
if ( (d === Directions.UP) && (_direction === Directions.DOWN) ) return self;
if ( (d === Directions.DOWN) && (_direction === Directions.UP) ) return self;
if ( (d === Directions.LEFT) && (_direction === Directions.RIGHT) ) return self;
if ( (d === Directions.RIGHT) && (_direction === Directions.LEFT) ) return self;
// update current direction
_newDirection = d;
_isDirectionChanged = true;
return self;
}
// add block to the DOM inside snake element
var _addBlock = function() {
var fifo = [],
previousBlock,
newBlock,
x = options.x,
y = options.y;
// if appending block to snake (new block is not the head)
if (_blocks.length) {
// loading tail
previousBlock = _blocks[_blocks.length-1];
// defining new fifo as a clone of the previous with an extra null movement
fifo = previousBlock.fifo.slice(0);
fifo.push(null);
x = previousBlock.x;
y = previousBlock.y;
}
// creating DOM element
$block = $.createElement('div');
$block.className = 'block';
// setting up logic block element
newBlock = {
fifo: fifo,
element: $block,
x: x,
y: y,
lastDirection: null, // TODO: check if needed
isHead: (!_blocks.length)
};
// adding new logic block
_blocks.push(newBlock);
// appending block to the DOM
_$snake.appendChild($block);
// move the DOM element to the right position
if (_board)
_board.placeBlock(newBlock, x, y, self);
};
/**
* Move a snake block
* @param $b
* @param movement
* @private
*/
var _moveBlock = function(block, m) {
var x = block.x,
y = block.y;
if (m === Directions.UP) {
x = block.x; y = block.y-1;
} else if (m === Directions.RIGHT) {
x = block.x+1; y = block.y;
} else if (m === Directions.DOWN) {
x = block.x; y = block.y+1;
} else if (m === Directions.LEFT) {
x = block.x-1; y = block.y;
}
_board.placeBlock(block, x, y, self);
block.lastDirection = m;
};
// Public properties
self.init = function() {
// Setup snake
_blocks = [];
_isGameOver = false;
_$snake.classList.remove('gameover');
// removing all previously added blocks to the DOM
while (_$snake.firstChild) {
_$snake.removeChild(_$snake.firstChild);
}
// push head (mandatory)
_addBlock();
// push tail blocks (optional)
for (var b = 0; b < options.size-1; b++) _addBlock();
return self;
};
self.setBoardHook = function(board) {
_board = board;
_blocks.forEach(function(b) {
_board.placeBlock(b, b.x, b.y, self);
});
return self;
};
/**
* Mode the snake
* @returns {Snake}
*/
self.move = function() {
if (_isGameOver) return self;
if (_isDirectionChanged) {
// remove old direction class
_$snake.classList.remove('up');
_$snake.classList.remove('right');
_$snake.classList.remove('down');
_$snake.classList.remove('left');
// add new direction class
_$snake.classList.add(DirectionsInverse[_newDirection]);
_isDirectionChanged = false;
_direction = _newDirection;
$$e('snake-direction-changed');
}
if (_direction === null) return self;
for ( var i = 0; i < _blocks.length; i++) {
// feed the block FIFO with a movement
_blocks[i].fifo.push(_direction);
// consume a movement from the block FIFO
var movement = _blocks[i].fifo.shift();
_moveBlock(_blocks[i], movement);
}
return self;
};
self.extend = function() {
_addBlock();
};
self.gameover = function() {
_isGameOver = true;
_$snake.classList.add('gameover');
return self;
};
self.stop = function() {
_$snake.classList.remove(_direction);
return self;
};
self.goUp = function () {
$$e('snake-direction-up', _$snake);
_updateDirection(Directions.UP);
return self;
};
self.goRight = function () {
$$e('snake-direction-right', _$snake);
_updateDirection(Directions.RIGHT);
return self;
};
self.goDown = function () {
$$e('snake-direction-down', _$snake);
_updateDirection(Directions.DOWN);
return self;
};
self.goLeft = function () {
$$e('snake-direction-left', _$snake);
_updateDirection(Directions.LEFT);
return self;
};
return self.init();
};
// Exposing objects
globals.Board = Board;
globals.Snake = Snake;
})(this);