-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakes.sh
264 lines (242 loc) · 4.73 KB
/
Snakes.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
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
#
# Snakes.sh
#
# A game of snakes and ladders.
# Script syntax described below.
trap "" SIGINT SIGQUIT SIGTERM
# A function for 1.????????????.
invalid()
{
echo "Invalid parameters!"
echo -e "\nInitialise mode syntax: Snakes.sh N L S directory\n"
echo "N for number of cells."
echo "S for number of snakes."
echo "L for number of ladders."
echo "directory should not exist."
echo -e "\nPreset mode syntax: Snakes.sh directory\n"
echo -e "directory must exist.\n"
}
initialise()
{
# Initialisation Phase.
echo "Entering Initialisation phase"
N=$1;L=$2;S=$3
DIR=$4
# Check 2.????????????.
if [ -d $DIR ]
then
echo -e "3.????????????\n"
exit 1
else
echo -e "4.????????????\n"
fi
# Checks.
if [ $N -lt 20 ]
then
N=20
echo "5.????????????"
fi
MAX=`expr 15 \* $N / 100`
MIN=`expr 5 \* $N / 100`
if [ $L -gt $MAX ]
then
echo "6.????????????"
L=$MAX
fi
if [ $L -lt $MIN ]
then
echo "7.????????????"
L=$MIN
fi
if [ $S -gt $MAX ]
then
echo "8.????????????"
S=$MAX
fi
if [ $S -lt $MIN ]
then
echo "Not enough snakes: increasing to $MIN"
S=$MIN
fi
# Determine the position of the ladders and snakes now.
# Define OBJECT to contain 0 for empty, 1 for snake head
# 2 for snake tail, 3 for ladder foot and 4 for
# ladder head.
# POINT is where an object (1,3) point to, this is
# necessary for the file generation.
# Initialise OBJECT and POINT
X=2
while [ $X -lt $N ]
do
OBJECT[$X]=0
POINT[$X]=0
let X=X+1
done
# Snakes first
X=0
while [ $X -lt $S ]
do
ONE=`expr $RANDOM % \( $N - 2 \) + 2`
if [ ${OBJECT[$ONE]} -eq 0 ]
then
TWO=`expr $RANDOM % \( $N - 2 \) + 2`
if [ ${OBJECT[$TWO]} -eq 0 ] && [ $ONE -ne $TWO ]
then
if [ $ONE -gt $TWO ]
then
OBJECT[$ONE]=1
POINT[$ONE]=$TWO
OBJECT[$TWO]=2
else
OBJECT[$TWO]=1
POINT[$TWO]=$ONE
OBJECT[$ONE]=2
fi
echo "9.????????????"
let X=X+1
fi
fi
done
# Ladders next
X=0
while [ $X -lt $L ]
do
ONE=`expr $RANDOM % \( $N - 2 \) + 2`
if [ ${OBJECT[$ONE]} -eq 0 ]
then
TWO=`expr $RANDOM % \( $N - 2 \) + 2`
if [ ${OBJECT[$TWO]} -eq 0 ] && [ $ONE -ne $TWO ]
then
if [ $ONE -gt $TWO ]
then
OBJECT[$ONE]=4
POINT[$TWO]=$ONE
OBJECT[$TWO]=3
else
OBJECT[$TWO]=4
POINT[$ONE]=$TWO
OBJECT[$ONE]=3
fi
echo "10.????????????"
let X=X+1
fi
fi
done
# Now generate the directories and object files.
mkdir $DIR
cd $DIR
X=1
while [ $X -le $N ]
do
mkdir $X
echo $X ${OBJECT[$X]} ${POINT[$X]}
case ${OBJECT[$X]} in
1) echo ${POINT[$X]} >> $X/snake
;;
3) echo ${POINT[$X]} >> $X/ladder
;;
*) ;;
esac
let X=X+1
done
cd $SAVE
echo -e "\nInitialisation Complete"
}
check()
{
# Run a check on the directory to make sure the cells
# are all there and to obtain the value for N.
ls -1F | grep "^[0-9]*/$" | sort -n > temp.txt
START=`grep -n "^1/$" temp.txt | cut -d":" -f 1`
N=`tail -1 temp.txt | cut -d"/" -f 1`
tail +$START temp.txt > temp2.txt
X=`wc -l < temp2.txt`
rm temp.txt
rm temp2.txt
if [ $X -eq $N ]
then
echo "Directory checks out: $N cells"
else
echo "Directory has missing cells."
exit 1
fi
}
game()
{
DIR=$1
echo "Entering game phase: $N cells"
cd $1
C=1;
cd $C
GO=1
while [ $GO -eq 1 ]
do
echo "In cell $C: press return to continue"
read $JUNK
ROLL=`expr $RANDOM % 6 + 1`
echo "You rolled a $ROLL"
let D=ROLL+C
if [ $D -gt $N ]
then
echo "That would 11.????????????"
fi
if [ $D -eq $N ]
then
echo "Moving into cell $N..."
echo "You have 12.????????????"
GO=0
break
fi
if [ $D -lt $N ]
then
let C=D
echo "Moving into cell 13.????????????"
cd ../$C
# Test for a snake or a ladder.
# This uses the generation rule that no snake
# or ladder points to a cell containing another
# snake or ladder.
if [ -f snake ]
then
DOWN=`cat snake`
echo "Sliding down the snake to $DOWN :( "
C=$DOWN
cd ../$DOWN
elif [ -f ladder ]
then
UP=`cat ladder`
echo "Climbing up the ladder to $UP :) "
C=$UP
cd ../$UP
fi
fi
done
}
# ----------------------------------------------------------------------------
# This is the "main" part of the script.
# Record the present working directory before moving around.
SAVE=`pwd`
if [ $# -eq 1 ] # Test for preset mode.
then
echo "Preset mode"
if [ -d $1 ]
then
echo "14.????????????"
cd $1
check
cd $SAVE
game $1 $N
else
echo -e "Directory $1 does not exist!\n"
exit 1
fi
elif [ $# -eq 4 ] # Test for new setup mode.
then
echo "New setup mode"
initialise $1 $2 $3 $4
game $4 $N
else
invalid;exit 1
fi
exit 0