-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavascriptSnake.js
More file actions
384 lines (303 loc) · 7.33 KB
/
Copy pathJavascriptSnake.js
File metadata and controls
384 lines (303 loc) · 7.33 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
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
class Coord {
x;
y;
constructor(x = 0, y = 0) {
this.set(x, y);
}
set(x, y) {
this.x = x;
this.y = y;
}
isEqual(coord) {
return coord.x === this.x && coord.y == this.y;
}
}
class Grid {
element;
size;
grid;
constructor(size) {
this.grid = Array.from(Array(size), () => new Array(size)); // make 2d array with dimensions: size x size
this.size = size;
this.render();
}
render() {
const container = document.createElement("div");
container.style = "height: 100vh; width: 100vw; position: fixed; top: 0px; left: 0px; display: flex; justify-content:center; align-items:center";
container.id = "JavaScriptSnakeGameContainer";
document.body.appendChild(container);
this.element = document.createElement("div");
this.element.style = `display: grid; grid-template: repeat(${this.size}, 1fr) / repeat(${this.size}, 1fr); background-color: lightgray; height: 90vmin; width: 90vmin;`;
this.element.id = "JavascriptSnakeGameGrid";
container.appendChild(this.element);
}
get(coord) {
return this.grid[coord.x][coord.y]
}
set(coord, value) {
this.grid[coord.x][coord.y] = value;
}
clear(coord) {
this.set(coord, undefined);
}
isEmpty(coord) {
return typeof this.get(coord) === 'undefined'
}
getRandomCoord() {
const getRandomIndex = () => {
return Math.floor(Math.random() * this.size);
}
const coord = new Coord(getRandomIndex(), getRandomIndex());
while (!this.isEmpty(coord)) {
coord.x = getRandomIndex();
coord.y = getRandomIndex();
}
return coord;
}
}
class GameObject {
grid;
coord;
constructor(grid, coord) {
this.grid = grid;
this.coord = coord;
}
addToGrid() {
this.grid.set(this.coord, this);
}
removeFromGrid() {
this.grid.clear(this.coord);
}
}
class OnePixelObject extends GameObject {
element;
colour;
constructor(grid, coord, colour) {
super(grid, coord);
this.colour = colour;
this.render();
}
render() {
super.addToGrid();
this.element = document.createElement("div");
this.element.style = `grid-area: ${this.coord.x + 1} / ${this.coord.y + 1}; background-color: ${this.colour}`;
this.grid.element.append(this.element);
}
derender() {
super.removeFromGrid();
this.element.remove();
}
}
class FoodObject extends OnePixelObject {
constructor(grid, coord) {
super(grid, coord, 'red');
}
}
class SnakeObject extends OnePixelObject {
constructor(grid, coord) {
super(grid, coord, 'grey');
}
}
class Node {
value;
nextNode;
constructor(value) {
this.value = value;
}
setNextNode(node) {
this.nextNode = node;
}
getNextNode() {
return this.nextNode;
}
}
class Queue {
head = null;
tail = null;
length = 0;
constructor(value = null) {
if (value !== null) {
this.enqueue(value);
}
}
enqueue(value) {
let newTail = new Node(value);
if (this.length > 0) {
this.tail.setNextNode(newTail);
} else {
this.head = newTail;
}
this.tail = newTail;
this.length++;
}
dequeue() {
if (this.length > 0) {
let newHead = this.head.getNextNode();
let oldHead = this.head;
oldHead.setNextNode(null);
this.head = newHead;
this.length--;
return oldHead.value;
}
return null;
}
peak() {
return this.head.value;
}
}
class InputHandler {
maxLength = 4;
buffer;
constructor() {
this.buffer = new Queue();
this.bindKeyBoardInput();
}
getInput() {
return this.buffer.dequeue();
}
bindKeyBoardInput() {
const inputBuffer = this.buffer;
const maxLength = this.maxLength;
const handleInput = (event) => {
switch (event.key) {
case "ArrowUp":
inputBuffer.enqueue(Game.Directions.UP);
break;
case "ArrowRight":
inputBuffer.enqueue(Game.Directions.RIGHT);
break;
case "ArrowDown":
inputBuffer.enqueue(Game.Directions.DOWN);
break;
case "ArrowLeft":
inputBuffer.enqueue(Game.Directions.LEFT);
break;
}
}
document.addEventListener("keydown", function (event) {
if (inputBuffer.length < maxLength) {
handleInput(event)
}
});
}
}
class SnakeAI {
snake;
grid;
movementDirection; // direction the snake will travel in
directionLastMoved;
constructor(snake, grid) {
this.snake = snake;
this.grid = grid;
this.movementDirection = Game.Directions.UP;
this.directionLastMoved = this.movementDirection;
}
mod(dividend, devisor) {
return ((dividend % devisor) + devisor) % devisor;
};
getNextLocation() {
const location = this.snake.tail.value.coord;
const nextLocation = new Coord();
nextLocation.x = this.mod((location.x + this.movementDirection.x), this.grid.size);
nextLocation.y = this.mod((location.y + this.movementDirection.y), this.grid.size);
return nextLocation;
}
propogate(coord, grow = false) {
const newSnakePart = new SnakeObject(this.grid, coord);
this.snake.enqueue(newSnakePart);
if (!grow) {
const oldSnakePart = this.snake.dequeue();
oldSnakePart.derender();
}
this.directionLastMoved = this.movementDirection;
}
moveSnake(grow = false) {
const coord = this.getNextLocation();
this.propogate(coord, grow);
}
setMovementDirection(direction) {
const isSnakeOneLong = this.snake.length <= 1;
const isDirectionIntoBody = direction.isEqual(Game.getOppositeDirection(this.directionLastMoved));
if (isSnakeOneLong || !isDirectionIntoBody) {
this.movementDirection = direction;
}
}
}
class Game {
static Directions = Object.freeze({
UP: new Coord(-1, 0),
DOWN: new Coord(1, 0),
LEFT: new Coord(0, -1),
RIGHT: new Coord(0, 1)
})
static getOppositeDirection = (direction) => {
switch (direction) {
case Game.Directions.UP:
return Game.Directions.DOWN;
case Game.Directions.DOWN:
return Game.Directions.UP;
case Game.Directions.LEFT:
return Game.Directions.RIGHT;
case Game.Directions.RIGHT:
return Game.Directions.LEFT;
}
}
static PlayState = Object.freeze({
PLAYING: Symbol("Playing"),
LOST: Symbol("Lost")
})
framerate;
playState;
snakeAI;
inputHandler;
constructor(size, framerate = 200, startCoord = new Coord(2, 2)) {
this.grid = new Grid(size);
this.framerate = framerate;
const snakeStartPart = new SnakeObject(this.grid, startCoord);
this.snake = new Queue(snakeStartPart);
this.snakeAI = new SnakeAI(this.snake, this.grid);
this.inputHandler = new InputHandler();
this.start();
}
generateFood() {
const coord = this.grid.getRandomCoord();
const food = new FoodObject(this.grid, coord);
}
gameLoop() {
const directionalInput = this.inputHandler.getInput();
if (directionalInput !== null) {
this.snakeAI.setMovementDirection(directionalInput);
}
const nextLocation = this.snakeAI.getNextLocation();
if (this.grid.isEmpty(nextLocation)) {
this.snakeAI.moveSnake();
} else {
const gameObject = this.grid.get(nextLocation);
if (gameObject === this.snake.head.value){
this.snakeAI.moveSnake();
} else if (gameObject instanceof SnakeObject) {
this.playState = Game.PlayState.LOST;
} else if (gameObject instanceof FoodObject) {
this.snakeAI.moveSnake(true);
gameObject.derender();
this.generateFood();
}
}
}
start() {
this.playState = Game.PlayState.PLAYING;
this.generateFood();
const intervalID = setInterval(
() => {
this.gameLoop();
switch (this.playState) {
case Game.PlayState.LOST:
clearInterval(intervalID);
window.alert("YOU LOST");
break;
}
}, this.framerate
);
}
}
const game = new Game(30, 100);