-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
281 lines (253 loc) · 7.25 KB
/
script.js
File metadata and controls
281 lines (253 loc) · 7.25 KB
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
var directions = window.directions = {
UP: "up",
RIGHT: "right",
DOWN: "down",
LEFT: "left"
};
var grid;
var gridEl;
var gridMap;
var snake;
var food;
var currentDirection;
var snakeMovement;
var C = 8; //constant: number of columns
var R = 8; //constant: number of rows
var keyboardEventHandlerRegistered = false;
function initializeGame() {
initializeGrid();
initializeSnake();
positionNewFood();
drawSnake();
if (!keyboardEventHandlerRegistered) {
registerKeyboardEventHandler();
}
startSnake();
}
function startSnake() {
//console.log('snake started');
if (snakeMovement != null && typeof snakeMovement != 'undefined') {
clearInterval(snakeMovement);
//console.log('interval cleared. snakeMovement: ' + snakeMovement);
}
movePeriodically();
}
function restartSnake(direction) {
if (snakeMovement != null && typeof snakeMovement != 'undefined') {
clearInterval(snakeMovement);
}
var gameOver = moveSnake(direction);
if (gameOver) {
restartGame();
}
else {
movePeriodically();
}
}
function movePeriodically() {
snakeMovement = setInterval(function () {
var gameOver = moveSnake(currentDirection);
if (gameOver) {
restartGame();
}
},
700
)
}
function restartGame() {
clearInterval(snakeMovement);
alert('Game over');
initializeGame();
}
function registerKeyboardEventHandler() {
$(document).keyup(function (event) {
//console.log('keyup!');
window.ev = event;
var direction;
switch (event.keyCode) {
case 37:
direction = directions.LEFT;
break;
case 38:
direction = directions.UP;
break;
case 39:
direction = directions.RIGHT;
break;
case 40:
direction = directions.DOWN;
break;
default:
return;
}
if (isAllowedDirection(direction)) {
console.log('allowed direction');
restartSnake(direction);
}
else {
//console.warn('NOT allowed direction');
}
});
keyboardEventHandlerRegistered = true;
}
function isAllowedDirection(direction) {
return !((((currentDirection == directions.LEFT) || (currentDirection == directions.RIGHT)) && ((direction == directions.RIGHT) || (direction == directions.LEFT)))
|| (((currentDirection == directions.UP) || (currentDirection == directions.DOWN)) && ((direction == directions.UP) || (direction == directions.DOWN)))
)
}
function createNewRowElement() {
return $('<div/>')
.addClass('row');
}
function createNewCellElement() {
return $('<div/>')
.addClass('cell');
}
function initializeSnake() {
snake = [{y: 0, x: C - 3}, {y: 0, x: C - 2}, {y: 0, x: C - 1}];
for (var count = 0; count < snake.length; count++) {
var snakeCell = snake[count];
gridMap[snakeCell.y][snakeCell.x] = 1;
}
currentDirection = directions.LEFT;
}
function initializeGrid() {
gridEl = $("#grid").empty();
grid = [];
gridMap = [];
for (var rowCount = 0; rowCount < R; rowCount++) {
var rowEl = createNewRowElement();
gridEl.append(rowEl);
var row = grid[rowCount] = [];
var gridMapRow = gridMap[rowCount] = [];
for (var cellCount = 0; cellCount < C; cellCount++) {
var cellEl = createNewCellElement();
rowEl.append(cellEl);
row[cellCount] = cellEl;
gridMapRow[cellCount] = 0;
}
}
}
function drawSnake() {
//console.log('started drawSnake()');
$("#grid .row .cell.active").removeClass('active food');
for (var i = 0; i < snake.length; i++) {
var snakeCell = snake[i];
grid[snakeCell.y][snakeCell.x].addClass('active');
}
drawFood();
}
function drawFood() {
grid[food.y][food.x].addClass('active food');
}
function moveSnake(direction) {
//console.log('started moveSnake()');
currentDirection = direction;
var _snakeTail = snake[snake.length - 1];
var snakeTail = {y: _snakeTail.y, x: _snakeTail.x};
for (var i = snake.length - 1; i > 0; i--) {
var currentCell = snake[i];
var higherCell = snake[i - 1];
currentCell.x = higherCell.x;
currentCell.y = higherCell.y;
}
var snakeHead = snake[0];
switch (direction) {
case directions.UP:
if (snakeHead.y == 0) {
snakeHead.y = grid.length - 1;
}
else {
snakeHead.y -= 1;
}
break;
case directions.RIGHT:
if (snakeHead.x == grid[0].length - 1) {
snakeHead.x = 0;
}
else {
snakeHead.x += 1;
}
break;
case directions.DOWN:
if (snakeHead.y == grid.length - 1) {
snakeHead.y = 0;
}
else {
snakeHead.y += 1;
}
break;
case directions.LEFT:
if (snakeHead.x == 0) {
snakeHead.x = grid[0].length - 1;
}
else {
snakeHead.x -= 1;
}
break;
}
for (var k = 1; k < snake.length; k++) {
var snakeCell = snake[k];
var gameOver = false;
if (snakeHead.x == snakeCell.x && snakeHead.y == snakeCell.y) { // snake head hit body
gameOver = true;
}
else if (snakeHead.x == snakeTail.x && snakeHead.y == snakeTail.y) { // snake head hit its tail
gameOver = true;
}
if (gameOver) {
return true;
}
}
gridMap[snakeHead.y][snakeHead.x] = 1;
if (snakeHead.y == food.y && snakeHead.x == food.x) {
snake.push(snakeTail);
positionNewFood();
}
else {
gridMap[snakeTail.y][snakeTail.x] = 0
}
drawSnake();
return false;
}
function rand(min, max, integer) {
if (!integer) {
return Math.random() * (max - min) + min;
} else {
return Math.floor(Math.random() * (max - min + 1) + min);
}
}
function positionNewFood() {
if (typeof food == 'undefined' || food == null) {
food = {y: null, x: null};
}
var availableSpace = C * R - snake.length;
console.log("availableSpace: " + availableSpace);
var position = rand(0, availableSpace - 1, true);
console.log("next food position: " + position);
var count = 0;
var log = "";
/* <debugging code> */
for (var n = 0; n < R; n++) {
for (var m = 0; m < C; m++) {
log = log + gridMap[n][m] + "\t";
}
log = log + '\n';
}
console.log(log);
/* </debugging code> */
for (var i = 0; i < R; i++) {
for (var j = 0; j < C; j++) {
if (gridMap[i][j] == 0) { // no snake cell in this grid cell
if (count == position) {
food.y = i;
food.x = j;
console.log('food positioned: {y: ' + food.y + ', x: ' + food.x + '}');
return;
}
count++;
}
}
}
}
initializeGame();