-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (171 loc) · 4.91 KB
/
index.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
import { emitKeypressEvents } from 'readline';
const { stdin, stdout } = process;
// ==================== CONSTANTS ====================
const TICK_TIMEOUT = 150;
const BOX = {
TOP_LEFT_CORNER: '\u250C',
TOP_RIGHT_CORNER: '\u2510',
BOTTOM_LEFT_CORNER: '\u2514',
BOTTOM_RIGHT_CORNER: '\u2518',
HORIZONTAL_LINE: '\u2500',
VERTICAL_LINE: '\u2502',
};
const SNAKE_CHARACTER = '*';
// ==================== CLI ====================
const { columns, rows } = stdout;
const cursorTo = (row = 1, column = 1) => stdout.cursorTo(column - 1, row - 1);
const moveCursor = (h, v) => stdout.moveCursor(h, v);
const clearScreen = () => stdout.write('\x1b[2J');
const showCursor = () => stdout.write('\x1b[?25h');
const hideCursor = () => stdout.write('\x1b[?25l');
const output = data => stdout.write(data);
const fillPoint = (row = 1, column = 1, char = SNAKE_CHARACTER) => {
let c = column > 1 ? column : 2;
c = c < columns ? c : columns - 1;
let r = row > 1 ? row : 2;
r = r < rows ? r : rows - 1;
cursorTo(r, c);
output(char);
};
const fillPoints = (points, char = SNAKE_CHARACTER) => {
for (const point of points) fillPoint(point[0], point[1], char);
};
const clearPoints = points => fillPoints(points, ' ');
// ==================== BUSINESS LOGIC ====================
let mainTicks = null;
let dirVertical = 0;
let dirHorizontal = 2;
let foodRow = 1;
let foodColumn = 1;
const snakeBody = [
[2, 6],
[2, 4],
[2, 2],
];
const isGameBoardCoordinate = (v, h) => {
return h > 1 && h < columns && v > 1 && v < rows;
};
const isOppositeDirection = (newV, newH) => {
if (dirVertical + newV === 0 && dirHorizontal + newH === 0) return true;
return false;
};
const isSnakeBodyPart = (v, h) => {
return snakeBody.some(([sV, sH]) => sV === v && sH === h);
};
const endGame = (message) => {
showCursor();
cursorTo(1, 1);
clearScreen();
console.log(message);
return process.exit(0);
};
stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
return endGame('See you soon again :)');
}
let newVertical = dirVertical;
let newHorizontal = dirHorizontal;
switch (key.name) {
case 'up':
newVertical = -1;
newHorizontal = 0;
break;
case 'right':
newVertical = 0;
newHorizontal = 2;
break;
case 'down':
newVertical = 1;
newHorizontal = 0;
break;
case 'left':
newVertical = 0;
newHorizontal = -2;
break;
}
if (!isOppositeDirection(newVertical, newHorizontal)) {
dirVertical = newVertical;
dirHorizontal = newHorizontal;
}
});
const randomInteger = (minimum, maximum) => {
const min = Math.ceil(minimum);
const max = Math.floor(maximum);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const drawBoard = () => {
let i = -1;
clearScreen();
cursorTo(1, 1);
for (i = 0; i < columns; ++i) {
if (i === 0) output(BOX.TOP_LEFT_CORNER);
else if (i === columns - 1) output(BOX.TOP_RIGHT_CORNER);
else output(BOX.HORIZONTAL_LINE);
}
cursorTo(2, columns);
for (i = 0; i < rows; ++i) {
if (i === rows - 1) output(BOX.BOTTOM_RIGHT_CORNER);
else output(BOX.VERTICAL_LINE);
moveCursor(0, 1);
}
cursorTo(rows, columns - 1);
for (i = 0; i < columns - 1; ++i) {
if (i === columns - 2) output(BOX.BOTTOM_LEFT_CORNER);
else output(BOX.HORIZONTAL_LINE);
moveCursor(-2, 0);
}
cursorTo(rows - 1, 1);
for (i = 0; i < rows - 2; ++i) {
output(BOX.VERTICAL_LINE);
moveCursor(-1, -1);
}
};
const moveSnake = (snake, dirH, dirV) => {
const headVerticalChange = snake[0][0] + dirV;
const headHorizontalChange = snake[0][1] + dirH;
if (
isGameBoardCoordinate(headVerticalChange, headHorizontalChange) &&
!isSnakeBodyPart(headVerticalChange, headHorizontalChange)
) {
snake.unshift([headVerticalChange, headHorizontalChange]);
snake.pop();
} else {
clearInterval(mainTicks);
endGame('You loose! Your score is: ' + (snakeBody.length - 3));
}
};
const generateFood = () => {
foodRow = 1;
foodColumn = 1;
while (foodRow % 2 !== 0) foodRow = randomInteger(2, rows - 1);
while (foodColumn % 2 !== 0) foodColumn = randomInteger(2, columns - 1);
fillPoint(foodRow, foodColumn, '$');
};
const shouldResizeSnake = () => {
if (snakeBody[0][0] === foodRow && snakeBody[0][1] === foodColumn) {
snakeBody.push(snakeBody[snakeBody.length - 1]); // double the tail
generateFood();
}
};
const setupGame = () => {
fillPoints(snakeBody);
generateFood();
};
const render = () => {
clearPoints(snakeBody);
moveSnake(snakeBody, dirHorizontal, dirVertical);
shouldResizeSnake();
fillPoints(snakeBody);
};
// ==================== MAIN THREAD ====================
emitKeypressEvents(stdin);
stdin.setRawMode(true);
stdin.setEncoding('utf8');
hideCursor();
drawBoard();
setupGame();
mainTicks = setInterval(render, TICK_TIMEOUT);
stdout.on('drain', () => {
clearInterval(mainTicks);
mainTicks = setInterval(render, TICK_TIMEOUT);
});