-
hangman Game
Hi
Im kinda a newbie and would like some help Please!!
Im writing a hangman Game for 2 players.
How it works is the user is prompted for a new word by means of a INput box . The assiged label then displays the word as "******" .
Now what I want 2 know is how to set the Eg: Word equel to an
array Eg: Secret word.
Eg:
word = inputbox (".....")
now how do i go about setting this returned value = to an array
Eg :secretword()
so that the guesses can be compared to that array
please help
Thanx
-
This will take a string from an InputBox and split it into an Array
Code:
Dim Word As String
Dim vWord() As String
Word = InputBox("Enter word")
For i = 1 To Len(Word)
ReDim Preserve vWord(i - 1)
vWord(i - 1) = Mid(Word, i, 1)
Next i
-
And you can use this function for the guessing of a letter:
Code:
Function GuessLetter(ByVal sLetter As String, vArray As Variant)
For i = 0 To UBound(vArray)
'Make sure that only the 1st letter is chosen
If vArray(i) = Left(sLetter, 1) Then MsgBox "Yes": Exit Function
Next i
MsgBox "No"
End Function
Usage:
Code:
GuessLetter "a", vArray
-
This is no big deal, but it's probably better to ReDim the array only once:
Code:
Dim Word As String
Dim vWord() As String
Word = InputBox("Enter word")
ReDim vWord(Len(Word) - 1)
For i = 1 To Len(Word)
vWord(i - 1) = Mid(Word, i, 1)
Next i
Like I said, no biggie.
-
Yeah, you're right; it will probably save time in the long run.