Hangman Script

Only available on StudyMode
  • Topic: Word, Replacements, Unix SUS2008 utilities
  • Pages : 6 (487 words )
  • Download(s) : 927
  • Published : March 27, 2011
Open Document
Text Preview
#!/bin/bash
#hangman game
#author ****************
#purpose: for science

play_again()
{
echo "Would you like to play again?"
read play_again
if [[ "$play_again" =~ ^[yY] ]] ; then
echo "Enjoy another game."
game
else
echo "Thanks for playing. Have a good day."
exit
fi
}
echo
echo " Welcome to Dan's hangman game!"
echo " all letters are lower cased "
sleep 1
echo
echo " Also all words are related to OS2"
echo
sleep 2
game()
{
blank=".................."# must be longer than longest word #List of secret words
secret_word()
{
case $(( $$ % 8 )) in
0 ) echo "linux" ;;1 ) echo "bash";; 2 ) echo "open" ;;
3 ) echo "peguin" ;; 4 ) echo "awesome" ;; 5 ) echo "ubuntu" ;; 6 ) echo "chmod" ;;7 ) echo "file";;
esac
}

actual_word()
{
# This function replaces all '.' in template with guess
# then updates remaining to be the number of empty slots left

letter=1
while [ $letter -le $letters ] ; do
if [ "$(echo $word | cut -c$letter)" = "$guess" ] ; then #cut -c is so that it will only show that letter in the word before="$(( $letter - 1 ))"
after="$(( $letter + 1 ))"
if [ $before -gt 0 ] ; then
tbefore="$(echo $template | cut -c1-$before)"
else
tbefore=
fi
if [ $after -gt $letters ] ; then
template="$tbefore$guess"
else
template="$tbefore$guess$(echo $template | cut -c$after-$letters)" fi
fi
letter=$(( $letter + 1 ))
done

remaining=$(echo $template|sed 's/[^\.]//g'|wc -c|sed 's/[[:space:]]//g') #sed s is to replace portion that matches the word where the "." are g is to append the templete of what the word is "g" appends the the .... remaining=$(( $remaining - 1 ))# fix to ignore '\n'

}
word=$(secret_word)
letters=$(echo $word | wc -c | sed 's/[[:space:]]//g')
letters=$(( $letters - 1 ))# fix character count to ignore \n template="$(echo $blank | cut -c1-$letters)"
remaining=$letters ; guessed= ; guesses=0; badguesses=0
echo
echo "** You're trying to guess a word with $letters letters **" echo
while [[ $badguesses -lt 6 && $remaining -gt 0 ]] ; do #sets up the loop for guessing the word
echo
echo "Word is: $template"
echo "Guess letter now! "
read guess
guesses=$(( $guesses + 1 ))
echo
if echo $guessed | grep -i $guess > /dev/null ; then
echo "You've already guessed "$guess". Try again!"
echo $guessed
elif ! echo $word | grep -i $guess > /dev/null ; then
echo "Sorry, the letter "$guess" is not in the word."
guessed="$guessed$guess"
badguesses=$(( $badguesses + 1 ))
if [ $badguesses -eq 1 ]; then
echo "
-------------
| |
| |
O |
|
|
|
|
|
|
------------ "
echo "Guessed letter"
echo $guessed

elif [ $badguesses -eq 2 ]; then
echo "
-------------
| |
| |
O |
| |
|
|
|
|
|
|
------------"
echo "Guessed letters"
echo $guessed

elif [ $badguesses -eq 3 ]; then
echo "
-------------
| |
| |
O |
\|/ |
|
|
|
|
|
|
------------"
echo "Guessed Letters"
echo $guessed

elif [ $badguesses -eq 4 ] ; then
echo "
-------------
| |
| |
O |
\|/...
tracking img