CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Hangman Problem

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Hangman Problem

    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

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Hangman Problem

    You could use a string to hold all the previous guesses. Use InStr() to check if the letter has already been guessed, and if not, add it to the end of the string.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Feb 2005
    Posts
    4

    Re: Hangman Problem

    Quote Originally Posted by WizBang
    You could use a string to hold all the previous guesses. Use InStr() to check if the letter has already been guessed, and if not, add it to the end of the string.
    So, how would that go?

  4. #4
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    148

    Re: Hangman Problem

    Here's what I would do. Create a boolean array (1 to 26) with initial values of false for all cells. Let each array index value represent a letter (i.e. 1=a, 2=b, etc.). When a letter is guessed check to see if the indexed value for that letter is set to true. If it is true, ask for a new letter. If false, change the value to true and see if the letter is in the secret word.
    Death is life's special way of telling you you're fired.

    For I do not seek to understand in order to believe, but I believe in order to understand. For I believe this: unless I believe, I will not understand. - Anselm of Canterbury (1033–1109)

  5. #5
    Join Date
    Sep 2002
    Location
    England
    Posts
    530

    Re: Hangman Problem

    Hi

    Knocked up the following. Loops thru array of letters to see if previously selected, if so chucks out a message, else adds letter to array for next pass in the loop..

    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
    
        Dim strArrGuess()   As String
        Dim x               As Long
        Dim blnDuplicate    As Boolean
    
        strSecretWord = "magic"
        intSecretWordLength = Len(strSecretWord)
        strWordGuessedSoFar = String(intSecretWordLength, "-")
        lblWord.Caption = strWordGuessedSoFar
    
        intNumberOfGuesses = 0
        ReDim strArrGuess(0)
        
        strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
    
        Do While strGuess <> strSentinel
            
            blnDuplicate = False
            
            For x = 0 To UBound(strArrGuess)
    
                If strGuess = strArrGuess(x) Then
                    blnDuplicate = True
                    Exit For
                End If
    
            Next ' x
                
            If blnDuplicate = False Then
            
                If intNumberOfGuesses = 0 Then
                    ReDim strArrGuess(0)
                Else
                    ReDim Preserve strArrGuess(intNumberOfGuesses)
                End If
                
                strArrGuess(intNumberOfGuesses) = strGuess
                
                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
            
            Else
            
                MsgBox "already used letter " & strGuess
            
            End If
                
            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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured