-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashmaze.sh
executable file
·253 lines (230 loc) · 4.97 KB
/
bashmaze.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
#
#
# Bash Maze ver 1.0
# This game was based on the maze generator from
# Joe Wingbermuehle at https://raw.github.com/joewing/maze/master/maze.sh
# I've added movement and collision to create the game.
#
#
# References:
# https://raw.github.com/joewing/maze/master/maze.sh
# http://lendscripts.blogspot.com.br/2012/10/licao-3-programacao-de-jogos-em.html
#
# Written by Fernando Bolonhezi Giannasi - jun/2013
# Validade bash version
if [ $(echo $BASH_VERSION | awk -F"." '{ if ( ($1 >= 4) ) {print "0"} else {print "1"}}') -ne "0" ]; then
echo "This game only works on bash 4.0 or later"
echo "Your version is $BASH_VERSION"
exit 1
fi
# Inicial menu
setterm -cursor off
while true; do
clear
echo -e '\033[01;33m'
cat << EOF
Bash Maze ver 1.0
*******************************************
Help the red ball find the exit
*******************************************
Keys:
a -> move left
s -> move down
d -> move right
w -> move up
q -> quit (anytime)
*******************************************
EOF
tput sgr0
echo -e '\033[01;31m'
cat << EOF
Select the difficulty:
1-) Easy
2-) Medium
3-) Hard
q-) Exit
EOF
tput sgr0
read -n1 -s DIFIC
case "$DIFIC" in
"1")
MAZE_WIDTH="39"
MAZE_HEIGHT="21"
break
;;
"2")
MAZE_WIDTH="49"
MAZE_HEIGHT="31"
break
;;
"3")
MAZE_WIDTH="59"
MAZE_HEIGHT="31"
break
;;
"q")
exit 0
;;
esac
done
# Initialize the maze array.
function init_maze {
for ((y=0; y<MAZE_HEIGHT; y++)) ; do
for ((x=1; x<$((MAZE_WIDTH-1)); x++)) ; do
maze[$((y * MAZE_WIDTH + x))]=0
done
maze[$((y * MAZE_WIDTH + 0))]=1
maze[$((y * MAZE_WIDTH + (MAZE_WIDTH - 1)))]=1
done
for ((x=0; x<MAZE_WIDTH; x++)) ; do
maze[$x]=1
maze[$(((MAZE_HEIGHT - 1) * MAZE_WIDTH + x))]=1
done
}
# Display the maze array.
function print_maze {
for ((y=0; y<MAZE_HEIGHT; y++)) ; do
for ((x = 0; x < MAZE_WIDTH; x++ )) ; do
if [[ maze[$((y * MAZE_WIDTH + x))] -eq 0 ]] ; then
echo -n "[]"
else
echo -n " "
fi
done
echo
done
}
# Carve the maze starting at the specified offset.
function carve_maze {
local index=$1
local dir=$RANDOM
local i=0
maze[$index]=1
while [ $i -le 4 ] ; do
local offset=0
case $((dir % 4)) in
0) offset=1 ;;
1) offset=-1 ;;
2) offset=$MAZE_WIDTH ;;
3) offset=$((-$MAZE_WIDTH)) ;;
esac
local index2=$((index + offset))
if [[ maze[$index2] -eq 0 ]] ; then
local nindex=$((index2 + offset))
if [[ maze[$nindex] -eq 0 ]] ; then
maze[$index2]=1
carve_maze $nindex
i=0
dir=$RANDOM
index=$nindex
fi
fi
i=$((i + 1))
dir=$((dir + 1))
done
}
# Create a maze:
TMP="/tmp"
if [ ! -d "$TMP" ]; then
mkdir "$TMP"
fi
init_maze
carve_maze $((2 * MAZE_WIDTH + 2))
maze[$((MAZE_WIDTH + 2))]=1
maze[$(((MAZE_HEIGHT - 2) * MAZE_WIDTH + MAZE_WIDTH - 3))]=1
print_maze > $TMP/maze.txt
sed -i '1d' $TMP/maze.txt
sed -i 's/^ //g' $TMP/maze.txt
# Variables
INPUT="0" # Input data
m="0" # Mov 1
n="1" # Mov 2
C="0" # Collision test
x="3" # X coordinate
y="0" # Y coordinate
counter="1" #Counts movements
WINS="$(echo $MAZE_HEIGHT - 3 | bc)" # Detects the exit
#Functions to print maze and ball
function cat_maze() {
echo -ne '\033[01;32m'
cat $TMP/maze.txt
tput sgr0
echo "X coordinate = $x"
echo "Y coordinate = $y"
echo "Moves = $counter"
}
function cat_ball() {
echo -ne '\033[01;31m'O
tput sgr0
}
# inicial position
clear
tput cup 0 0
cat_maze
tput cup $y $x
cat_ball
# Moving the ball:
while [ $INPUT != "q" ];do
read -n1 -s INPUT
if [ $INPUT = a ];then
let "m = x"
let "n = y + 1"
C=$(cat $TMP/maze.txt | sed -n "$n"p 2> /dev/null | cut -c"$m" 2> /dev/null) # If C is not empty than we hit a wall
if [ -z $C ];then
let "x = x - 1"
else
let counter--
fi
fi
if [ $INPUT = d ];then
let "m = x + 2"
let "n = y + 1"
C=$(cat $TMP/maze.txt | sed -n "$n"p 2> /dev/null | cut -c"$m" 2> /dev/null)
if [ -z $C ];then
let "x = x + 1"
else
let counter--
fi
fi
if [ $INPUT = w ];then
let "m = x + 1"
let "n = y"
C=$(cat $TMP/maze.txt | sed -n "$n"p 2> /dev/null | cut -c"$m" 2> /dev/null)
if [ -z $C ];then
let "y = y - 1"
else
let counter--
fi
fi
if [ $INPUT = s ];then
let "m = x + 1"
let "n = y + 2"
C=$(cat $TMP/maze.txt | sed -n "$n"p 2> /dev/null | cut -c"$m" 2> /dev/null)
if [ -z $C ];then
let "y = y + 1"
else
let counter--
fi
fi
if [ "$y" -lt "0" ]; then y=0; let counter--; fi
# Check Wins
if [ "$y" -gt "$WINS" ]; then
tput cup $(echo $MAZE_HEIGHT + 3 | bc) 0
echo -e '\033[01;31m'
echo You WON!!!!!
echo "Score: $counter moves"
tput sgr0
echo
setterm -cursor on
exit 0
fi
clear
cat_maze
# Prints the ball on new location
tput cup $y $x
cat_ball
let counter++
done
clear
# End of script