-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
174 lines (138 loc) · 4.44 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
// game =========================================
const Game = (() => {
let listener
return {
init
}
function init(Snake, Food, speed) {
Food.place()
if (isIn(Snake.body(), Food.position()))
return init(Snake, Food, speed)
listener = Listener(Snake)
listener.start()
return play(Snake, Food, speed)
}
function play(Snake, Food, speed) {
return setTimeout(round, speed, Snake, Food)
function round(Snake, Food) {
const food = Food.position()
drawBord(Snake, Food)
Snake.move()
if (Snake.clash()) return
if (!Snake.meets(food)) return play(Snake, Food, speed)
Snake.eats(food)
return init(Snake, Food, speed - speed * 1 / 54)
}
function drawBord(Snake, Food) {
render()
Snake.render()
Food.render()
}
}
})()
function Listener(snake){
const keyHandler = ({ code }) => {
if (!code.includes('Arrow')) return
snake.turn(directions[code.replace('Arrow','').toLocaleLowerCase()])
}
return {
start: () => document.addEventListener("keydown",keyHandler),
stop: () => document.removeEventListener("keydown",keyHandler)
}
}
// utils ===================================
function render(color = 'white', { x, y, l } = { x: 0, y: 0, l: width }) {
const canvas = document.getElementById('board').getContext('2d')
canvas.fillStyle = color
canvas.fillRect(x, y, l, l)
}
function cell(x, y) {
return {
x: isNaN(x) ?
Math.floor(Math.random() * width / length) * length :
x * length,
y: isNaN(y) ?
Math.floor(Math.random() * width / length) * length :
y * length,
l: length
}
}
function coordinates({ x, y }) {
return {
x: Math.floor(x / length),
y: Math.floor(y / length)
}
}
const directions = (function (directions = {}) {
['left', 'up', 'right', 'down'].forEach((direction, i) => {
directions[direction] = i
directions[i] = direction
})
return directions
})()
function shift({ x, y }, direction, i = 1) {
if (direction === directions[0]) return { x: x - i, y } // left
if (direction === directions[1]) return { x, y: y - i } // up
if (direction === directions[2]) return { x: x + i, y } // rigth
if (direction === directions[3]) return { x, y: y + i } // down
}
function place({ x, y }, max = Math.floor(width / length) - 1) {
if (x < 0) x = max
if (y < 0) y = max
if (x > max) x = 0
if (y > max) y = 0
return { x, y, } = cell(x, y)
}
function equals({ x: ax, y: ay }, { x: bx, y: by }) {
return ax === bx && ay === by
}
function isIn(line, point) {
return line.some((part) => equals(part, point))
}
// models =========================================
const Food = ((food) => {
return {
place: () => { return food = cell() },
position: () => (food),
render: () => render('green', food)
}
})()
const Snake = ((
direction = Math.floor(Math.random() * 4),
snake = ((head = cell(), len = 4) => {
let snake = new Array(len).fill(head)
return snake.map(({ x, y }, i) => place(
shift(coordinates({ x, y }), directions[direction], i * -1)))
})(),
_stack = []
) => {
return {
head: () => snake[0],
body: () => snake,
move: () => snake = move(),
turn: (where) => turn(where),
meets: (thing) => equals(snake[0], thing),
clash: (thing = snake[0]) => isIn(snake.slice(1), thing),
eats: (food) => _stack.push(food),
render: () => snake.forEach(p => render('black', p)),
}
function turn(code) {
if (code === 0 && direction === directions['right']) return
if (code === 1 && direction === directions['down']) return
if (code === 2 && direction === directions['left']) return
if (code === 3 && direction === directions['up']) return
direction = directions[directions[code]]
}
function move() {
snake.splice(0, 0,
place(shift(coordinates(snake[0]), directions[direction]))
)
snake.pop()
if (_stack.length > 0 && !isIn(snake, _stack[0])) {
snake.push(_stack.pop())
}
return snake
}
})()
// run ===================================
Game.init(Snake, Food, 150)