-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
137 lines (115 loc) · 3.22 KB
/
ui.js
File metadata and controls
137 lines (115 loc) · 3.22 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
'use strict';
const React = require('react');
const {useState, useEffect, useContext} = require('react');
const PropTypes = require('prop-types');
const {Box, Text, Color, StdinContext} = require('ink');
const importJsx = require('import-jsx');
const useInterval = require('./useInterval');
const EndScreen = importJsx('./endScreen');
const ARROW_UP = '\u001B[A';
const ARROW_RIGHT = '\u001B[C';
const ARROW_DOWN = '\u001B[B';
const ARROW_LEFT = '\u001B[D';
const FIELD_SIZE = 16;
const FIELD_ROW =[...new Array(FIELD_SIZE).keys()];
const DIRECTION = {
TOP: {x: 0, y: -1},
RIGHT: {x: 1, y: 0},
BOTTOM: {x: 0, y: 1},
LEFT: {x: -1, y: 0}
};
let foodItem = {};
setFoodItem();
function App() {
const [snakeSegments, setSnakeSegments] = useState([
{x: 8, y: 8},
{x: 7, y: 8},
{x: 6, y: 8}
]);
const [direction, setDirection] = useState(DIRECTION.RIGHT);
const {stdin, setRawMode} = useContext(StdinContext);
useEffect(() => {
setRawMode(true);
stdin.on('data', data => {
const value = data.toString();
switch (value) {
case ARROW_UP: return setDirection(DIRECTION.TOP);
case ARROW_RIGHT: return setDirection(DIRECTION.RIGHT);
case ARROW_DOWN: return setDirection(DIRECTION.BOTTOM);
case ARROW_LEFT: return setDirection(DIRECTION.LEFT);
}
});
}, []);
const [head, ...tail] = snakeSegments;
const isIntersectsWithItself = tail.some(segment => segment.x === head.x && segment.y === head.y);
useInterval(() => {
setSnakeSegments(segments => getNewSnakePosition(segments, direction))
}, isIntersectsWithItself ? null : 100);
return (
<Box flexDirection="column" alignItems="center">
<Text>
<Color green>Snake</Color> game
</Text>
{isIntersectsWithItself ? (
<EndScreen size={FIELD_SIZE} />
) : (
<Box flexDirection="column" marginTop={1}>
{FIELD_ROW.map(y => (
<Box key={y}>
{FIELD_ROW.map(x => (
<Box key={x} alignItems="center">{getItem(x, y, snakeSegments) || ' · '}</Box>
))}
</Box>
))}
</Box>
)}
</Box>
)
}
function getItem(x, y, snakeSegments) {
if (foodItem.x === x && foodItem.y === y) {
return <Color red> 🍅 </Color>
}
for (const segment of snakeSegments) {
if (segment.x === x && segment.y === y) {
return <Color green> ■ </Color>
}
}
}
function getNewSnakePosition(snakeSegments, direction) {
const [head] = snakeSegments;
const newHead = {
x: limitByField(head.x + direction.x),
y: limitByField(head.y + direction.y)
};
if (isCollideWithFood(head, foodItem)) {
setFoodItem();
return [newHead, ...snakeSegments]
}
return [newHead, ...snakeSegments.slice(0, -1)]
}
function limitByField(j) {
if (j >= FIELD_SIZE) {
return 0
}
if (j < 0) {
return FIELD_SIZE - 1
}
return j
}
function isCollideWithFood(head, foodItem) {
return head.x === foodItem.x && head.y === foodItem.y
}
function setFoodItem() {
foodItem = {
x: Math.floor(Math.random() * FIELD_SIZE),
y: Math.floor(Math.random() * FIELD_SIZE)
}
}
App.propTypes = {
name: PropTypes.string
};
App.defaultProps = {
name: 'Stranger'
};
module.exports = App;