summaryrefslogtreecommitdiff
path: root/os1eindopdracht/uhm.awk
blob: c57f7c517b9ba1694fd5948a9819905a22cf5aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/awk -f
function prompt() { printf "> " }

BEGIN {
	word=word
	guessed_letters=""
	guesses=10

	prompt()
}

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
	}

	# print 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 "guessed letters: " guessed_letters
	print "you have " guesses " guess" (guesses == 1 ? "" : "es") " left"
}

{
	if (length($1) == 1) guess($1)
	else print "please input a single letter"
	prompt()
}