-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
134 lines (114 loc) · 2.74 KB
/
ui.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
"use strict";
const React = require("react");
const { useState, useEffect, useContext } = require("react");
const { Text, Box, useInput } = require("ink");
const useInterval = require("./useInteval");
const importJsx = require("import-jsx");
const EndScreen = importJsx("./endScreen.js");
const FIELD_SIZE = 16;
const FIELD_ROW = [...new Array(FIELD_SIZE).keys()];
let foodItem = {
x: Math.floor(Math.random() * FIELD_SIZE),
y: Math.floor(Math.random() * FIELD_SIZE),
};
const Direction = {
RIGHT: { x: 1, y: 0 },
LEFT: { x: -1, y: 0 },
TOP: { x: 0, y: -1 },
B0TTON: { x: 0, y: 1 },
};
function getItem(x, y, snakeSegments) {
if (foodItem.x === x && foodItem.y === y) {
return <Text color="red"> ♥ </Text>;
}
for (const segment of snakeSegments) {
if (segment.x === x && segment.y === y) {
return <Text color="green"> ■ </Text>;
}
}
}
function limitByField(j) {
if (j >= FIELD_SIZE) {
return 0;
}
if (j < 0) {
return FIELD_SIZE - 1;
}
return j;
}
function newSnakePosition(segments, direction) {
const [head] = segments;
const newHead = {
x: limitByField(head.x + direction.x),
y: limitByField(head.y + direction.y),
};
if (collidesWithFood(newHead, foodItem)) {
foodItem = {
x: Math.floor(Math.random() * FIELD_SIZE),
y: Math.floor(Math.random() * FIELD_SIZE),
};
return [newHead, ...segments];
}
return [newHead, ...segments.slice(0, -1)];
}
function collidesWithFood(head, foodItem) {
return head.x === foodItem.x && head.y == foodItem.y;
}
const App = () => {
const [snakeSegments, setSnakeSegments] = useState([
{ x: 8, y: 8 },
{ x: 8, y: 7 },
{ x: 8, y: 6 },
]);
const [direction, setDirection] = useState(Direction.LEFT);
useInput((input, key) => {
if (input === "q") {
exit();
}
if (key.leftArrow) {
setDirection(Direction.LEFT);
}
if (key.rightArrow) {
setDirection(Direction.RIGHT);
}
if (key.upArrow) {
setDirection(Direction.TOP);
}
if (key.downArrow) {
setDirection(Direction.B0TTON);
}
});
const [head, ...tail] = snakeSegments;
const intersectsWithItself = tail.some(
(segment) => segment.x === head.x && segment.y === head.y
);
useInterval(
() => {
setSnakeSegments((segments) => newSnakePosition(segments, direction));
},
intersectsWithItself ? null : 50
);
return (
<Box flexDirection="column" alignItems="center">
<Text>
<Text color="green">Snake</Text> Game
</Text>
{intersectsWithItself ? (
<EndScreen size={FIELD_SIZE} />
) : (
<Box flexDirection="column">
{FIELD_ROW.map((y) => (
<Box key={y}>
{FIELD_ROW.map((x) => (
<Box key={x}>
<Text>{getItem(x, y, snakeSegments) || " . "}</Text>
</Box>
))}
</Box>
))}
</Box>
)}
</Box>
);
};
module.exports = App;