LDP/LDP/guide/docbook/abs-guide/petals.sh

195 lines
5.2 KiB
Bash

#!/bin/bash -i
# petals.sh
#########################################################################
# Petals Around the Rose #
# #
# Version 0.1 Created by Serghey Rodin #
# Version 0.2 Modded by ABS Guide Author #
# #
# License: GPL3 #
# Used in ABS Guide with permission. #
# ##################################################################### #
hits=0 # Correct guesses.
WIN=6 # Mastered the game.
ALMOST=5 # One short of mastery.
EXIT=exit # Give up early?
RANDOM=$$ # Seeds the random number generator from PID of script.
# Bones (ASCII graphics for dice)
bone1[1]="| |"
bone1[2]="| o |"
bone1[3]="| o |"
bone1[4]="| o o |"
bone1[5]="| o o |"
bone1[6]="| o o |"
bone2[1]="| o |"
bone2[2]="| |"
bone2[3]="| o |"
bone2[4]="| |"
bone2[5]="| o |"
bone2[6]="| o o |"
bone3[1]="| |"
bone3[2]="| o |"
bone3[3]="| o |"
bone3[4]="| o o |"
bone3[5]="| o o |"
bone3[6]="| o o |"
bone="+---------+"
# Functions
instructions () {
clear
echo -n "Do you need instructions? (y/n) "; read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
clear
echo -e '\E[34;47m' # Blue type.
# "cat document"
cat <<INSTRUCTIONSZZZ
The name of the game is Petals Around the Rose,
and that name is significant.
Five dice will roll and you must guess the "answer" for each roll.
It will be zero or an even number.
After your guess, you will be told the answer for the roll, but . . .
that's ALL the information you will get.
Six consecutive correct guesses admits you to the
Fellowship of the Rose.
INSTRUCTIONSZZZ
echo -e "\033[0m" # Turn off blue.
else clear
fi
}
fortune ()
{
RANGE=7
FLOOR=0
number=0
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
let "number %= $RANGE" # 1 - 6.
done
return $number
}
throw () { # Calculate each individual die.
fortune; B1=$?
fortune; B2=$?
fortune; B3=$?
fortune; B4=$?
fortune; B5=$?
calc () { # Function embedded within a function!
case "$1" in
3 ) rose=2;;
5 ) rose=4;;
* ) rose=0;;
esac # Simplified algorithm.
# Doesn't really get to the heart of the matter.
return $rose
}
answer=0
calc "$B1"; answer=$(expr $answer + $(echo $?))
calc "$B2"; answer=$(expr $answer + $(echo $?))
calc "$B3"; answer=$(expr $answer + $(echo $?))
calc "$B4"; answer=$(expr $answer + $(echo $?))
calc "$B5"; answer=$(expr $answer + $(echo $?))
}
game ()
{ # Generate graphic display of dice throw.
throw
echo -e "\033[1m" # Bold.
echo -e "\n"
echo -e "$bone\t$bone\t$bone\t$bone\t$bone"
echo -e \
"${bone1[$B1]}\t${bone1[$B2]}\t${bone1[$B3]}\t${bone1[$B4]}\t${bone1[$B5]}"
echo -e \
"${bone2[$B1]}\t${bone2[$B2]}\t${bone2[$B3]}\t${bone2[$B4]}\t${bone2[$B5]}"
echo -e \
"${bone3[$B1]}\t${bone3[$B2]}\t${bone3[$B3]}\t${bone3[$B4]}\t${bone3[$B5]}"
echo -e "$bone\t$bone\t$bone\t$bone\t$bone"
echo -e "\n\n\t\t"
echo -e "\033[0m" # Turn off bold.
echo -n "There are how many petals around the rose? "
}
# ============================================================== #
instructions
while [ "$petal" != "$EXIT" ] # Main loop.
do
game
read petal
echo "$petal" | grep [0-9] >/dev/null # Filter response for digit.
# Otherwise just roll dice again.
if [ "$?" -eq 0 ] # If-loop #1.
then
if [ "$petal" == "$answer" ]; then # If-loop #2.
echo -e "\nCorrect. There are $petal petals around the rose.\n"
(( hits++ ))
if [ "$hits" -eq "$WIN" ]; then # If-loop #3.
echo -e '\E[31;47m' # Red type.
echo -e "\033[1m" # Bold.
echo "You have unraveled the mystery of the Rose Petals!"
echo "Welcome to the Fellowship of the Rose!!!"
echo "(You are herewith sworn to secrecy.)"; echo
echo -e "\033[0m" # Turn off red & bold.
break # Exit!
else echo "You have $hits correct so far."; echo
if [ "$hits" -eq "$ALMOST" ]; then
echo "Just one more gets you to the heart of the mystery!"; echo
fi
fi # Close if-loop #3.
else
echo -e "\nWrong. There are $answer petals around the rose.\n"
hits=0 # Reset number of correct guesses.
fi # Close if-loop #2.
echo -n "Hit ENTER for the next roll, or type \"exit\" to end. "
read
if [ "$REPLY" = "$EXIT" ]; then exit
fi
fi # Close if-loop #1.
clear
done # End of main (while) loop.
###
exit $?
# Resources:
# ---------
# 1) http://en.wikipedia.org/wiki/Petals_Around_the_Rose
# (Wikipedia entry.)
# 2) http://www.borrett.id.au/computing/petals-bg.htm
# (How Bill Gates coped with the Petals Around the Rose challenge.)