summaryrefslogtreecommitdiff
path: root/os1eindopdracht/uhm.awk
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-03-26 21:15:31 +0200
committerlonkaars <loek@pipeframe.xyz>2023-03-26 21:15:31 +0200
commit1ea23e3b923ea89b97a51edc75e1a8d13264e758 (patch)
treeb32d8b096682b0aae7904bd1a76537eab9a50de9 /os1eindopdracht/uhm.awk
parent060238054edbe95b3b517fbab493488863d25908 (diff)
initial version of `uhm` (ultimate hangman)
Diffstat (limited to 'os1eindopdracht/uhm.awk')
-rwxr-xr-xos1eindopdracht/uhm.awk52
1 files changed, 52 insertions, 0 deletions
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()
+}
+