-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.vbs
More file actions
60 lines (56 loc) · 1.33 KB
/
hangman.vbs
File metadata and controls
60 lines (56 loc) · 1.33 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
Dim secretWord, guessedLetters, numGuesses
secretWord = "programming"
guessedLetters = ""
numGuesses = 10
While true
document.write("<p>Current word: " & getVisibleWord() & " Guesses left: " & numGuesses & "</p>")
guessedLetter = InputBox("Enter a letter:", "Hangman Game")
guessedLetters = guessedLetters & " " & guessedLetter
if isGuessCorrect(guessedLetter) then
if isWordGuessed() then
document.write("<p>Congratulations! You guessed the word.</p>")
Exit While
else
document.write("<p>Correct guess!</p>")
end if
else
numGuesses = numGuesses - 1
if numGuesses = 0 then
document.write("<p>You ran out of guesses. The word was " & secretWord & ".</p>")
Exit While
else
document.write("<p>Incorrect guess!</p>")
end if
end if
Wend
Function getVisibleWord()
arr = Split(secretWord)
ret = ""
for i = 0 to UBound(arr)
if isGuessCorrect(arr(i)) then
ret = ret & arr(i)
else
ret = ret & "_ "
end if
next
getVisibleWord = ret
End Function
Function isGuessCorrect(letter)
for i = 0 to UBound(guessedLetters)
if Trim(guessedLetters(i)) = letter then
isGuessCorrect = True
Exit Function
end if
next
isGuessCorrect = False
End Function
Function isWordGuessed()
for i = 0 to UBound(secretWord)
if isGuessCorrect(secretWord(i)) then
else
isWordGuessed = False
Exit Function
end if
next
isWordGuessed = True
End Function