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

Show parent comments

1

u/[deleted] Jan 25 '16

I'm confused on what you're asking, are you asking how to keep track of the letters guessed in the word, or letters guessed not in the word?

1

u/JoeyBattts Jan 25 '16

I need to modify this program to keep track of the total letters guessed in an array with meaningful indexes. I also need to include in the modification code so that it displays a message box if the user enters the same guess twice.

1

u/JoeyBattts Jan 25 '16

ThanksOldSpice I tried so hard to figure this coding thing out. If you help me out with this, I'm going to be using old spice everyday from now on.

1

u/[deleted] Jan 25 '16 edited Jan 25 '16

I'm on mobile and sick right now but, since there's no way to know how many guesses they will enter, you could declare an array with 1 string, and after every guess you resize the array by 1 by using redim and preserve. so after every guess, you resize the array by 1, and add the letter they guessed. Obviously you don't want to add the same letter twice, so once you get the users guess, you could use a loop to compare the users guess to every element in the strLettersGuessed array, if strUsersGuess = strLettersGuessed(intCount), then you display the message box saying letter has been guessed, Else redim the array and preserve and add the guessed letter to it