Debian Codegraph @

petals.sh

raw symbols SCIP
1 #!/bin/bash -i
2 # petals.sh
3
4 #########################################################################
5 # Petals Around the Rose #
6 # #
7 # Version 0.1 Created by Serghey Rodin #
8 # Version 0.2 Modded by ABS Guide Author #
9 # #
10 # License: GPL3 #
11 # Used in ABS Guide with permission. #
12 # ##################################################################### #
13
14 hits=0 # Correct guesses.
15 WIN=6 # Mastered the game.
16 ALMOST=5 # One short of mastery.
17 EXIT=exit # Give up early?
18
19 RANDOM=$$ # Seeds the random number generator from PID of script.
20
21
22 # Bones (ASCII graphics for dice)
23 bone1[1]="| |"
24 bone1[2]="| o |"
25 bone1[3]="| o |"
26 bone1[4]="| o o |"
27 bone1[5]="| o o |"
28 bone1[6]="| o o |"
29 bone2[1]="| o |"
30 bone2[2]="| |"
31 bone2[3]="| o |"
32 bone2[4]="| |"
33 bone2[5]="| o |"
34 bone2[6]="| o o |"
35 bone3[1]="| |"
36 bone3[2]="| o |"
37 bone3[3]="| o |"
38 bone3[4]="| o o |"
39 bone3[5]="| o o |"
40 bone3[6]="| o o |"
41 bone="+---------+"
42
43
44
45 # Functions
46
47 instructions () {
48
49 clear
50 echo -n "Do you need instructions? (y/n) "; read ans
51 if [ "$ans" = "y" -o "$ans" = "Y" ]; then
52 clear
53 echo -e '\E[34;47m' # Blue type.
54
55 # "cat document"
56 cat <<INSTRUCTIONSZZZ
57 The name of the game is Petals Around the Rose,
58 and that name is significant.
59 Five dice will roll and you must guess the "answer" for each roll.
60 It will be zero or an even number.
61 After your guess, you will be told the answer for the roll, but . . .
62 that's ALL the information you will get.
63
64 Six consecutive correct guesses admits you to the
65 Fellowship of the Rose.
66 INSTRUCTIONSZZZ
67
68 echo -e "\033[0m" # Turn off blue.
69 else clear
70 fi
71
72 }
73
74
75 fortune ()
76 {
77 RANGE=7
78 FLOOR=0
79 number=0
80 while [ "$number" -le $FLOOR ]
81 do
82 number=$RANDOM
83 let "number %= $RANGE" # 1 - 6.
84 done
85
86 return $number
87 }
88
89
90
91 throw () { # Calculate each individual die.
92 fortune; B1=$?
93 fortune; B2=$?
94 fortune; B3=$?
95 fortune; B4=$?
96 fortune; B5=$?
97
98 calc () { # Function embedded within a function!
99 case "$1" in
100 3 ) rose=2;;
101 5 ) rose=4;;
102 * ) rose=0;;
103 esac # Simplified algorithm.
104 # Doesn't really get to the heart of the matter.
105 return $rose
106 }
107
108 answer=0
109 calc "$B1"; answer=$(expr $answer + $(echo $?))
110 calc "$B2"; answer=$(expr $answer + $(echo $?))
111 calc "$B3"; answer=$(expr $answer + $(echo $?))
112 calc "$B4"; answer=$(expr $answer + $(echo $?))
113 calc "$B5"; answer=$(expr $answer + $(echo $?))
114 }
115
116
117
118 game ()
119 { # Generate graphic display of dice throw.
120 throw
121 echo -e "\033[1m" # Bold.
122 echo -e "\n"
123 echo -e "$bone\t$bone\t$bone\t$bone\t$bone"
124 echo -e \
125 "${bone1[$B1]}\t${bone1[$B2]}\t${bone1[$B3]}\t${bone1[$B4]}\t${bone1[$B5]}"
126 echo -e \
127 "${bone2[$B1]}\t${bone2[$B2]}\t${bone2[$B3]}\t${bone2[$B4]}\t${bone2[$B5]}"
128 echo -e \
129 "${bone3[$B1]}\t${bone3[$B2]}\t${bone3[$B3]}\t${bone3[$B4]}\t${bone3[$B5]}"
130 echo -e "$bone\t$bone\t$bone\t$bone\t$bone"
131 echo -e "\n\n\t\t"
132 echo -e "\033[0m" # Turn off bold.
133 echo -n "There are how many petals around the rose? "
134 }
135
136
137
138 # ============================================================== #
139
140 instructions
141
142 while [ "$petal" != "$EXIT" ] # Main loop.
143 do
144 game
145 read petal
146 echo "$petal" | grep [0-9] >/dev/null # Filter response for digit.
147 # Otherwise just roll dice again.
148 if [ "$?" -eq 0 ] # If-loop #1.
149 then
150 if [ "$petal" == "$answer" ]; then # If-loop #2.
151 echo -e "\nCorrect. There are $petal petals around the rose.\n"
152 (( hits++ ))
153
154 if [ "$hits" -eq "$WIN" ]; then # If-loop #3.
155 echo -e '\E[31;47m' # Red type.
156 echo -e "\033[1m" # Bold.
157 echo "You have unraveled the mystery of the Rose Petals!"
158 echo "Welcome to the Fellowship of the Rose!!!"
159 echo "(You are herewith sworn to secrecy.)"; echo
160 echo -e "\033[0m" # Turn off red & bold.
161 break # Exit!
162 else echo "You have $hits correct so far."; echo
163
164 if [ "$hits" -eq "$ALMOST" ]; then
165 echo "Just one more gets you to the heart of the mystery!"; echo
166 fi
167
168 fi # Close if-loop #3.
169
170 else
171 echo -e "\nWrong. There are $answer petals around the rose.\n"
172 hits=0 # Reset number of correct guesses.
173 fi # Close if-loop #2.
174
175 echo -n "Hit ENTER for the next roll, or type \"exit\" to end. "
176 read
177 if [ "$REPLY" = "$EXIT" ]; then exit
178 fi
179
180 fi # Close if-loop #1.
181
182 clear
183 done # End of main (while) loop.
184
185 ###
186
187 exit $?
188
189 # Resources:
190 # ---------
191 # 1) http://en.wikipedia.org/wiki/Petals_Around_the_Rose
192 # (Wikipedia entry.)
193 # 2) http://www.borrett.id.au/computing/petals-bg.htm
194 # (How Bill Gates coped with the Petals Around the Rose challenge.)