diff options
-rw-r--r-- | os1eindopdracht/readme.md | 3 | ||||
-rwxr-xr-x | os1eindopdracht/uhm | 58 | ||||
-rwxr-xr-x | os1eindopdracht/uhm.awk | 52 |
3 files changed, 113 insertions, 0 deletions
diff --git a/os1eindopdracht/readme.md b/os1eindopdracht/readme.md new file mode 100644 index 0000000..42a91b1 --- /dev/null +++ b/os1eindopdracht/readme.md @@ -0,0 +1,3 @@ +# uhm (ultra hangman) + + diff --git a/os1eindopdracht/uhm b/os1eindopdracht/uhm new file mode 100755 index 0000000..324a863 --- /dev/null +++ b/os1eindopdracht/uhm @@ -0,0 +1,58 @@ +#!/bin/dash +WORD="" +DICT="/usr/share/dict/words" +ARGC=0 + +usage() { + cat << EOF +usage: $0 [-h] [-w word] [-d file] + +options: + -w set word to \`word\` + -d read dictionary from \`file\` ($DICT by default) + -h show help +EOF + exit $1 +} + +while getopts d:w:h OPT; do + [ $OPTIND -gt $ARGC ] && ARGC=$OPTIND + case $OPT in + h) usage 0 ;; + w) WORD=$OPTARG ;; + d) DICT=$OPTARG ;; + \?|*) usage 1 ;; + esac +done + +if [ -z "$WORD" ]; then + # check if dictionary file exists + [ ! -e "$DICT" ] && echo "dictionary file doesn't exist!" && exit 1 + # check if dictionary file contains at least one line + [ ! "`wc -l "$DICT" | cut -d' ' -f1`" -gt "0" ] && echo "dictionary file is empty!" && exit 1 + + # filter words containing apostrophe or words longer than 12 characters + WORD="`sed -e "/'/d" -e '/.\{13\}/d' "$DICT" | sort -R | head -n1`" + # check if word is empty + [ -z "$WORD" ] && echo "no usable words found in dictionary" && exit 1 +fi + +# convert word to lowercase +WORD="`echo "$WORD" | tr '[:upper:]' '[:lower:]'`" + +pre_exit() { + trap - EXIT # prevent double exit trigger + echo "" + echo "the word was $WORD" + exit +} + +# run pre_exit function on interrupt (Ctrl-C) +trap pre_exit INT + +# main game loop (written in awk) +tput clear # clear screen +tput cup `stty size | cut -f1 -d' '` # move cursor to bottom left corner +./uhm.awk -vword="$WORD" +pre_exit + diff --git a/os1eindopdracht/uhm.awk b/os1eindopdracht/uhm.awk new file mode 100755 index 0000000..c34333f --- /dev/null +++ b/os1eindopdracht/uhm.awk @@ -0,0 +1,52 @@ +#!/bin/awk -f +function prompt() { + printf "> " +} + +BEGIN { + word=word + guessed_letters="" + guesses=10 + + prompt() +} + +function header() { + printf "\n\n\n" + hidden_word = word + # replace all characters not in set `guessed_letters` with "_" + gsub("[^'"guessed_letters"]", "_", hidden_word) + print "current word: " hidden_word + print "you have " guesses " guess" (guesses == 1 ? "" : "es") " left" +} + +function guess(a) { + if (index(guessed_letters, a) != 0) { + print "already guessed " a "!" + return + } + # append guess `a` to guessed_letters + guessed_letters = guessed_letters tolower(a) + # only subtract guess count if letter not in word + if (index(word, a) == 0) guesses -= 1 + + if (guesses == 0) { + print "you have no more guesses left!" + exit 0 + } + + temp = word + if (gsub("[^'"guessed_letters"]", "_", temp) == 0) { # check if all letters are guessed + print "you guessed the word!" + exit 0 + } + + header() +} + +{ + if (length($1) == 1) guess($1) + else print "please input a single letter" + prompt() +} + |