r/visualbasic • • Jan 23 '16

Visual Basic word guess program

I don't know how to keep track of the letters guessed in an array with meaningful indexes. Can somebody help me?

Private Sub btnPlayGame_Click(sender As Object, e As EventArgs) Handles btnPlayGame.Click
    Const SECRET_WORD As String = "VELOCITY"
    Const FLAG As Char = "!"
    Const GUESS_PROMPT As String = "Enter a letter or " & FLAG & " to guess word:"
    Dim numGuesses As Integer = 0
    Dim letterGuess As Char
    Dim wordGuess As String
    Dim tempWord As String
    Dim endGame As Boolean

    Dim wordGuessedSoFar As String = ""
    Dim length As Integer = SECRET_WORD.Length
    wordGuessedSoFar = wordGuessedSoFar.PadLeft(length, "-")
    Me.lblSecretWord.Text = wordGuessedSoFar

    Dim tempLetterGuess = InputBox(GUESS_PROMPT, Me.Text)
    If tempLetterGuess = Nothing Then
        endGame = True
    Else
        letterGuess = tempLetterGuess
    End If

    Do While letterGuess <> FLAG And wordGuessedSoFar <> SECRET_WORD _
    And Not endGame
        numGuesses += 1
        For letterPos As Integer = 0 To SECRET_WORD.Length - 1
            If SECRET_WORD(letterPos) = Char.ToUpper(letterGuess) Then
                tempWord = wordGuessedSoFar.Remove(letterPos, 1)
                wordGuessedSoFar = tempWord.Insert(letterPos, Char.ToUpper(letterGuess))
                Me.lblSecretWord.Text = wordGuessedSoFar
            End If
        Next letterPos

        If wordGuessedSoFar <> SECRET_WORD Then
            tempLetterGuess = InputBox(GUESS_PROMPT, Me.Text)
            If tempLetterGuess = Nothing Then
                endGame = True
            Else
                letterGuess = tempLetterGuess
            End If
        End If
    Loop

    If wordGuessedSoFar = SECRET_WORD Then
        MessageBox.Show("You guessed it in " & numGuesses & " guesses!")
    ElseIf letterGuess = FLAG Then
        wordGuess = InputBox("Enter a word: ", Me.Text)
        If wordGuess.ToUpper = SECRET_WORD Then
            MessageBox.Show("You guessed it in " & numGuesses & " guesses!")
            Me.lblSecretWord.Text = SECRET_WORD
        Else
            MessageBox.Show("Sorry you lose.")
        End If
    Else
        MessageBox.Show("Game over.")
    End If
End Sub
5 Upvotes

6 comments sorted by

View all comments

4

u/TheFotty Jan 23 '16

Why do you make a whole new reddit account and repost a question you posted just yesterday?