-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.sh
executable file
·187 lines (151 loc) · 2.79 KB
/
snake.sh
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
#!/bin/sh
array_put()
{
local x="$1"
local y="$2"
local char="$3"
eval ARRAY_${x}_${y}='$char'
}
array_get()
{
local x="$1"
local y="$2"
# set a global var
eval FIELD="\"\${ARRAY_${x}_${y}:- }\""
}
draw_border()
{
local i
for i in $( seq 2 $(( $PLAYFIELD_MAX_X - 1 )) ); do {
array_put "$i" '1' "—"
array_put "$i" "$PLAYFIELD_MAX_Y" "—"
} done
for i in $( seq 2 $(( $PLAYFIELD_MAX_Y - 1 )) ); do {
array_put "1" "$i" "|"
array_put "$PLAYFIELD_MAX_X" "$i" "|"
} done
}
redraw_screen()
{
local x=0
local y=0
local line
echo -ne '\033[H' # home position (0,0)
while [ $y -lt $PLAYFIELD_MAX_Y ]; do {
y=$(( $y + 1 ))
line=
while [ $x -lt $PLAYFIELD_MAX_X ]; do {
x=$(( x + 1 ))
array_get "$x" "$y"
line="${line}${FIELD}"
} done
x=0
echo "$line"
} done
echo "Points: $BONUS"
}
add_head()
{
array_put "$X" "$Y" "$SNAKE"
LIST_SNAKE="$LIST_SNAKE $X,$Y"
}
remove_tail()
{
set -- $LIST_SNAKE
local x="${1%,*}"
local y="${1#*,}" # 8,3 -> 8 + 3
array_put "$x" "$y" ' '
# shift list and loose last element = tail
shift
LIST_SNAKE="$@"
}
random_int()
{
local max="$1"
# a portable PRNG:
# the initial seed is the size of our environment vars
# on each iteration, we add user-entropy (e.g. pressed keys/moved fields)
if [ -z "$SEED" ]; then
SEED=$(set)
SEED=${#SEED}
ENTROPY=1
else
SEED=$(( $SEED * $SEED * $ENTROPY ))
fi
# greater then $FFFF?
while [ ${#SEED} -gt 5 ]; do {
SEED=$(( $SEED / 256 ))
} done
echo "$(( ($SEED % $max) + 1 ))"
}
drop_new_food()
{
local x y
while true; do {
x="$( random_int $(( $PLAYFIELD_MAX_X - 2 )) )"
y="$( random_int $(( $PLAYFIELD_MAX_Y - 2 )) )"
array_get "$x" "$y"
[ "$FIELD" = ' ' ] && {
array_put "$x" "$y" "$FOOD"
return 0
}
} done
}
loop_get_userkey()
{
local key
while true; do {
read -s -n1 key
case "$key" in
a) DIRECTION='left' ;;
w) DIRECTION='up' ;;
s) DIRECTION='down' ;;
d) DIRECTION='right' ;;
esac
# TODO: dont write to file but use global var
echo >"LAST_KEY" "$DIRECTION"
} done
}
PLAYFIELD_MAX_X=41
PLAYFIELD_MAX_Y=21
X=9
Y=8
LIST_SNAKE="9,9 $X,$Y"
FOOD=':'
SNAKE='O'
BONUS=0
ENTROPY=1
I=0
draw_border
drop_new_food
while true; do {
let I+=1
redraw_screen
[ -e "LAST_KEY" ] && {
read DIRECTION <"LAST_KEY"
rm "LAST_KEY"
}
case "$DIRECTION" in
right) X=$(( $X + 1 )) ;;
left) X=$(( $X - 1 )) ;;
down) Y=$(( $Y + 1 )) ;;
up|*) Y=$(( $Y - 1 )) ;;
esac
# collision?
array_get "$X" "$Y"
NEXT_FIELD="$FIELD"
if [ "$NEXT_FIELD" = ' ' -o "$NEXT_FIELD" = "$FOOD" ]; then
add_head
if [ "$NEXT_FIELD" = "$FOOD" ]; then
BONUS=$(( $BONUS + 1 ))
ENTROPY=$(( $ENTROPY + $BONUS + $I ))
drop_new_food
else
remove_tail
fi
else
echo "you are dead"
exit 0
fi
} done &
loop_get_userkey