#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)"