#!/usr/bin/env bash
#
# If you are reading this, you have wandered out of bounds
# and are reading the code that drives the game.
#
#                    Congratulations!
#
# Learning Linux is all about curiosity, so read this code and see
# if you can figure out what it does.
#
# When you're ready to continue playing the game, though, stick to
# the scrolls. If you're stuck, go to Gitlab and create an issue.
# We're happy to provide hints.
#

#import global functions from toplevel directory
FUNCTIONS=$(pwd)
FUNCTIONS=${FUNCTIONS%entrance*}entrance/.functions
. $FUNCTIONS

roll() {
    printf "Enter a number:  "
    read NUM
    ALPHA=$RANDOM
    OMEGA=$RANDOM
    A=`echo $ALPHA | cut -b 1-2`
    O=`echo $OMEGA | cut -b 1-2`
    let "NUM=$ALPHA % $OMEGA"
    MONSTER=$RANDOM
    MONSTER=`echo $MONSTER | cut -b 1-2`
    NUM=`echo $NUM | cut -b 1-2`
    echo "The monster rolled " $MONSTER
    echo "You rolled " $NUM
    resolve
}

resolve() {
    if ! grep  --quiet --only-matching 'sword' <<< $I; then
        cat << EOF
You do not have a sword!

Your attacks will be less effective.

EOF
        let NUM=NUM-10
    fi
    if [[ $MONSTER -eq $NUM ]]; then
        cat << EOF
You each parry one another's attacks.  Roll again...

EOF
	roll
    fi

    if [[ $MONSTER -lt $NUM ]]; then
        cat << EOF
A hit! A palpable hit!  You have slain the beast.

EOF
        cat << EOF > treasure
#!/usr/bin/env bash
if ! grep  --quiet --only-matching crown <<< \$I; then
    cat << eof
You have found a **crown**!  Add it to your inventory.

eof

else
    echo "This treasure has already been taken."
fi

EOF
        chmod +x treasure
        mv monster carcass
    else
        cat << EOF

Your attack is deflected off the monster's leathery flesh.
You take 5 points damage. Deduct this from your HP.

let HP=HP-5

EOF
        let HP=HP-5
        export HP
        if [ "$HP" -le 0 ]; then
            cat << EOF
You have been slain by the monster.

GAME OVER!

EOF
            gameover
            exit 1
        fi
	roll
    fi
}

if [ "$0" = "./carcass" ]; then
    cat << EOF
You find the carcass of a foul beast.  Nothing of interest
lies within its remains.

EOF
    exit 0
fi

cat << EOF
A hulking three-legged beast, with a mouth full of fangs and
a barbed tail and 8 arms, lumbers toward you.

If you have a sword, you can attack.  Otherwise, you should
run.

EOF
printf "Do you want to attack? y/n  "
read RESP

if [ "$RESP" = "y" -o "$RESP" = "Y" ];then
    roll
else
    cat << EOF
You wisely turn and flee.  Quickly cd to the previous
directory.

EOF
fi
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
##
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#

#
# Well that didn't work!
