I'm trying to make hangman game from VB, it went fine but I need help with keeping track of the letters that the user has inputted in the inputbox. If the user has entered the same letter twice then a messagebox appears saying that the user has already guessed that letter. Heres the code:
Code:Option Explicit Private Sub cmdPlayGame_Click() Const strSentinel As String = "!" Dim strSecretWord As String, intSecretWordLength As Integer Dim intNumberOfGuesses As Integer Dim strGuess As String, strWordGuessedSoFar As String Dim intLetterPos As Integer strSecretWord = "magic" intSecretWordLength = Len(strSecretWord) strWordGuessedSoFar = String(intSecretWordLength, "-") lblWord.Caption = strWordGuessedSoFar intNumberOfGuesses = 0 strGuess = InputBox("Guess a letter (! to guess word)", "Hangman") Do While strGuess <> strSentinel intNumberOfGuesses = intNumberOfGuesses + 1 For intLetterPos = 1 To intSecretWordLength If StrComp(strGuess, Mid(strSecretWord, intLetterPos, 1), vbTextCompare) = 0 Then Mid(strWordGuessedSoFar, intLetterPos, 1) = strGuess End If Next intLetterPos lblWord.Caption = strWordGuessedSoFar strGuess = InputBox("Guess a letter (! to guess word)", "Hangman") Loop If strGuess = strSentinel Then strGuess = InputBox("Guess the word") End If If StrComp(strGuess, strSecretWord, vbTextCompare) = 0 Then MsgBox "You win! It took you " & intNumberOfGuesses & " guesses." Else MsgBox "You lose. Press OK to display secret word." End If lblWord.Caption = strSecretWord End Sub Private Sub cmdDone_Click() Unload Me End Sub




Reply With Quote