-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
120 lines (109 loc) · 2.26 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
process.stdin.setRawMode(true);
process.stdin.on('data', key => {
if (key == '\u001B\u005B\u0041') {
if(direction != 'down') {
direction = 'up';
}
}
if (key == '\u001B\u005B\u0043') {
if(direction != 'left') {
direction = 'right';
}
}
if (key == '\u001B\u005B\u0042') {
if(direction != 'up') {
direction = 'down';
}
}
if (key == '\u001B\u005B\u0044') {
if(direction != 'right') {
direction = 'left';
}
}
// Return key or Ctrl+C
if (key == '\u000D' || key == '\u0003') {
console.clear();
process.exit();
}
});
let score = 0,
width = 39,
height = 21,
direction = 'right',
headY = Math.ceil(height / 2 - 1),
headX = Math.ceil(width / 2 - 1),
bodyY = [headY],
bodyX = [headX],
rewardY,
rewardX;
const EMPTY = [];
// Initialize EMPTY board
for(let y = 0; y < height; y++) {
EMPTY.push([]);
for(let x = 0; x < width; x++) {
EMPTY[y].push(' ');
}
}
let board = JSON.parse(JSON.stringify(EMPTY));
const addReward = () => {
do {
rewardY = Math.floor(Math.random() * (height));
rewardX = Math.floor(Math.random() * (width));
} while (board[rewardY][rewardX] !== ' ');
}
addReward();
const updateBoard = () => {
if(direction === 'right') {
headX += 1;
if(headX == width) {
headX = 0;
}
}
if(direction === 'left') {
headX -= 1;
if(headX < 0) {
headX = width - 1;
}
}
if(direction === 'up') {
headY += 1;
if(headY == height) {
headY = 0;
}
}
if(direction === 'down') {
headY -= 1;
if(headY < 0) {
headY = height - 1;
}
}
bodyX.unshift(headX);
bodyY.unshift(headY);
if (headX === rewardX && headY === rewardY) {
score += 1;
addReward();
}
else if (board[headY][headX] === '▒') {
clearInterval(gameOn);
console.clear();
console.log('Score: ' + score)
return;
}
else {
bodyX.pop();
bodyY.pop();
}
board = JSON.parse(JSON.stringify(EMPTY));
board[rewardY][rewardX] = '•';
for(let i in bodyX) {
board[bodyY[i]][bodyX[i]] = '▒';
}
console.clear();
console.log(' ' + '█'.repeat(width));
for(let y = height - 1; y >= 0; y--) {
console.log('█' + board[y].join('') + '█')
}
console.log(' ' + '█'.repeat(width));
console.log(' '.repeat(Math.ceil(width/2-3 - score.toString().length)) + 'Score: ' + score);
}
const gameOn = setInterval(function () {updateBoard()}, 100);