dcsimg
Results 1 to 10 of 10

Thread: Help with hangman project.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Help with hangman project.

    Hey guys,
    I am doing a class project for hangman and I having trouble figuring out how to properly work with the button counter, I want it to count every time I push a button and a letter appears, if a letter doesn't appear I want it to pull up the next Image. I spend the last few hours trying figure it out but I am not understanding what to do properly. Any help would be much appreciated!




    Code:
    Option Explicit On
    Option Strict On
    
    Public Class frmHangman
        '-----------------------------------------------------------------------------------------
        'Program: Final Project
        'Author: Joseph 
        'Date: 3/6/2018
        'Description: Game will start with and user will have to press start, user guesses the word if too many incorrect guesses, game over. if user guesses the word in
        ' 7  tries, user wins, victory screen shows up.
        '-------------------------------------------------------------------------------------------------
    
        ' ============= Arrays =================
        'Alphabet array
        Dim strAlphabet() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
        'Button array
        Dim btnAlphabet(25) As Button
        ' Guess array
        'Dim strGuessArray(30) As String
        ' Dashes
        Dim strDashes(30) As String
        ' Word to be guessed
        Dim strWord As String = ""
        Dim wordLength As Integer
        'Dictionary array
        Dim strDictionary() As String = {"Token", "Ballroom", "Cheese", "Telephone", "Russia", "America",
        "Rose", "Tarantula", "Positive", "Negative", "Explosion", "Machine", "Contraband", "Lemonade",
        "Raspberry", "Operator", "Doctor", "Officer", "Soldier", "Marina"}
        'Image array
        Dim imgHangman(7) As Image
        '=======================================
        'random number generator
        Dim rnd As New Random
        Dim rndmX As Integer
        Dim intMyRandomNumber As Integer
        'counter
        Dim intX As Integer = 0
        Dim CountButton As Integer = 0
        ' Try Counter?
        Dim wrgTry As Integer = 0
        Dim rghtTry As Integer = 0
        ' ======================================
        Sub ButtonHandler() Handles btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click,
        btnG.Click, btnH.Click, btnI.Click, btnJ.Click, btnK.Click, btnL.Click, btnM.Click, btnN.Click,
        btnO.Click, btnP.Click, btnQ.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click,
        btnW.Click, btnX.Click, btnY.Click, btnZ.Click
            '-----------------------------------------------------------------------------------------
            'Subroutine: Button Handeler
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Handles each button.
            '-------------------------------------------------------------------------------------------------
            Dim strInput As String
            Dim strOutput As String = String.Empty
            Dim blnActiveFound As Boolean = False
            Dim intIndex As Integer = 0
            '============================
            'Sets buttonAlphabet to the active button.
            Dim blnActiveFound2 As Boolean = True
            Dim intB As Integer = 0
            Do While blnActiveFound2 = True
                If Me.ActiveControl.Name = btnAlphabet(intB).Name Then
                    blnActiveFound2 = False
                Else
                    intB = intB + 1
                End If
            Loop
            '============================
            strInput = btnAlphabet(intB).Text
            ' TO UPPER
            strInput = strInput.ToUpper
            strOutput = strOutput.ToUpper
            strWord = strWord.ToUpper
            ' call subroutine to search for character in secret word
            Call SearchAndReplace(strInput)
            'Do Something
            Do Until blnActiveFound = True
                If Me.ActiveControl.Name = btnAlphabet(intIndex).Name Then
                    btnAlphabet(intIndex).Enabled = False
                    For intX = 0 To strWord.Length - 1
                        strOutput = strOutput & strDashes(intX) + " "
                    Next
                    lblOutput.Text = strOutput
                    blnActiveFound = True
                Else
                    intIndex = intIndex + 1
                End If
            Loop
    
            ' Label2.Text = wrgTry.ToString
            CheckForWinning()
        End Sub
        '================================================================
    
    
        Private Sub frmHangman_Load(Sender As Object, e As EventArgs) Handles Me.Load
            '-----------------------------------------------------------------------------------------
            'Subroutine: Form Load
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Enter what happens when form loads
            '-------------------------------------------------------------------------------------------------
            boxAlphabet.Visible = False
            btnExit.Visible = True
            btnReset.Visible = False
            lblOutput.Visible = False
            lblInformation.Visible = False
            lblInstruction.Visible = False
            picHangmanStart.Visible = True
            'Initialize word randomizer
            WordRandomizer()
            'Start Picture
            picHangmanStart.Image = Image.FromFile("..\Images\" & "StartupImage" & ".png")
            'For Next loop to put each image into the Image array
            'Now you can reference each image by indexing into the Image array
    
            'Btn Array
            btnAlphabetArray()
            'Dash reset
            For intX = 0 To strWord.Length - 1
                strDashes(intX) = "-"
    
            Next
            '
    
        End Sub
        Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
            '-----------------------------------------------------------------------------------------
            'Subroutine: button reset
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Resets the form.
            '-------------------------------------------------------------------------------------------------
            Dim intIndex As Integer = 0
            '===================
            lblOutput.Text = ""
            boxAlphabet.Visible = False
            btnExit.Visible = False
            btnReset.Visible = False
            lblOutput.Visible = False
            lblInformation.Visible = False
            lblInstruction.Visible = False
            btnStartGame.Visible = True
            picHangmanStart.Visible = True
            picHangmanImage.Image = imgHangman(0)
            picHangmanImage.Visible = False
            'initImages()
            '
            rghtTry = 0
            wrgTry = 0
            '==========================
            'Picks a random word from the dictionary
            WordRandomizer()
            'For loop to reset all the buttons?
            For intX = 0 To btnAlphabet.Length - 1
                If btnAlphabet(intIndex).Enabled = False Then
                    btnAlphabet(intIndex).Enabled = True
                End If
                intIndex = intIndex + 1
            Next
            'For loop to reset the word string
            For intX = 0 To strWord.Length - 1
                strDashes(intX) = "-"
            Next
    
        End Sub
        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            '-----------------------------------------------------------------------------------------
            'Subroutine: button exit
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Exits the form
            '-------------------------------------------------------------------------------------------------
            Me.Close()
        End Sub
    
        Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
            btnStartGame.Visible = False
            boxAlphabet.Visible = True
            picHangmanStart.Visible = False
            btnExit.Visible = True
            btnReset.Visible = True
            lblOutput.Visible = True
            lblInformation.Visible = True
            lblInstruction.Visible = True
            btnA.Focus()
            'Load first image into picture box
            picHangmanImage.Visible = True
            picHangmanImage.Image = imgHangman(0)
            initImages()
        End Sub
        Sub initImages()
            '-----------------------------------------------------------------------------------------
            'Subroutine: Initialize Images
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Initializes the images for hangman
            '-------------------------------------------------------------------------------------------------
            picHangmanImage.Visible = True
            For intX = 0 To imgHangman.Length - 1
    
                imgHangman(intX) = Image.FromFile("..\Images\" & "hangman" & intX.ToString & ".png")
    
            Next
            picHangmanImage.Image = imgHangman(0)
            'load first image into picture box
            'picHangmanImage.Image = imgHangman(0)
            'Dim blnButtonFound As Boolean = False
            'Dim intCounter As Integer = 0
            'Do While blnButtonFound = False Or intCounter > 4
            '    If radSelectPic(intCounter).disabled Then
            '        blnButtonFound = True
            '        picHangmanImage.Image = imgHangman(intCounter)
            '    Else
            '        intCounter = intCounter + 1
            '    End If
    
            'Loop
        End Sub
        Sub btnAlphabetArray()
            '-----------------------------------------------------------------------------------------
            'Subroutine: Button Alphabet array
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: assigns each button a val of alphabet.
            '-------------------------------------------------------------------------------------------------
            btnAlphabet(0) = btnA
            btnAlphabet(1) = btnB
            btnAlphabet(2) = btnC
            btnAlphabet(3) = btnD
            btnAlphabet(4) = btnE
            btnAlphabet(5) = btnF
            btnAlphabet(6) = btnG
            btnAlphabet(7) = btnH
            btnAlphabet(8) = btnI
            btnAlphabet(9) = btnJ
            btnAlphabet(10) = btnK
            btnAlphabet(11) = btnL
            btnAlphabet(12) = btnM
            btnAlphabet(13) = btnN
            btnAlphabet(14) = btnO
            btnAlphabet(15) = btnP
            btnAlphabet(16) = btnQ
            btnAlphabet(17) = btnR
            btnAlphabet(18) = btnS
            btnAlphabet(19) = btnT
            btnAlphabet(20) = btnU
            btnAlphabet(21) = btnV
            btnAlphabet(22) = btnW
            btnAlphabet(23) = btnX
            btnAlphabet(24) = btnY
            btnAlphabet(25) = btnZ
    
        End Sub
        Sub SearchAndReplace(strSearchLetter As String)
            '--------------------------------------------------------------
            'Subroutine:  SearchAndReplace
            'Description: Search the String and determine if it contains the letter you are looking for
            '             if found replace the corresponding "-" in the array 
            'Parms:       one-character String
            '------------------------------------------------------------
    
            'Search for letter
            Dim count As Integer = strWord.Length - 1
            For intX = 0 To strWord.Length - 1
                If strWord.Substring(intX, 1) = strSearchLetter Then
                    strDashes(intX) = strSearchLetter
                    rghtTry = rghtTry + 1
                    Label1.Text = rghtTry.ToString
                End If
            Next
    
        End Sub
        Sub WordRandomizer()
            '-----------------------------------------------------------------------------------------
            'Subroutine: Word Randomizer
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Picks a random word from the dictionary.
            '-------------------------------------------------------------------------------------------------
            '------------Random number generator
    
            intMyRandomNumber = rnd.Next(0, 20)
            rndmX = intMyRandomNumber
            '------------------------------
            strWord = strDictionary(rndmX)
    
        End Sub
        Sub CheckForWinning()
            '-----------------------------------------------------------------------------------------
            'Subroutine: Check for Winning
            'Author: Joseph 
            'Date: 3/6/2018
            'Description: Checks if the user won the game or not, if user won, promts user with msgbox to reset or exit game.
            '-------------------------------------------------------------------------------------------------
            wordLength = strWord.Length
            If rghtTry = wordLength Then
                'MsgBox("You Won")
                If MsgBox("You Won! Do you want to play again? Click Yes to reset the game, Click No to exit.", MsgBoxStyle.YesNo, "Winner!") = MsgBoxResult.Yes Then
                    btnReset.PerformClick()
                Else
                    Me.Close()
                End If
    
            End If
        End Sub
    
    End Class

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,658

    Re: Help with hangman project.

    Have you tried setting a breakpoint on a line in your buttonhandler sub and then examine what values various variables are, and step through the execution of the code and observe whether if does what you expect it to do?

    Running the code one step at a time and observing what happens compared to what you expect to happen is usually the quickest way to find where the code goes wrong. If you're just looking at the code, and trying to step through it mentally, it will most likely "work" since that it probably the method you used to write it in the first place.

    Debugging the running code gets you a chance to have an objective look at what the code does, rather than your subjective opinion of what it should be doing.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Re: Help with hangman project.

    Quote Originally Posted by passel View Post
    Have you tried setting a breakpoint on a line in your buttonhandler sub and then examine what values various variables are, and step through the execution of the code and observe whether if does what you expect it to do?

    Running the code one step at a time and observing what happens compared to what you expect to happen is usually the quickest way to find where the code goes wrong. If you're just looking at the code, and trying to step through it mentally, it will most likely "work" since that it probably the method you used to write it in the first place.

    Debugging the running code gets you a chance to have an objective look at what the code does, rather than your subjective opinion of what it should be doing.
    I spent a few hours doing that, the program is functional, I'm still struggling with the fact that I don't know where the guess count should go, I tried all over.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,756

    Re: Help with hangman project.

    My first thought was that this wasn't .NET, because that button handler is lacking the arguments. I'd have to say that I've never seen Me.ActiveControl, but I guess it's just that I've never seen it used. I'm surprised that the method would function without the arguments, but if you added the arguments, then the sender argument IS the button that raised the event, so you can write:

    clickedButton= directCast(sender, Button)

    and clickedButton will be the button that raised the event. Probably doesn't matter any, though, as whatever active control will pretty much have to be the button, too. However, you could make that much more efficient, and even eliminate the first Do loop, though that would likely take one extra step: Every control has a Tag property, which can hold whatever you want it to hold, so if you made the Tag hold the letter for the button, then you could get your strInput by just doing this:

    strInput = DirectCast(sender,Button).Tag.ToString

    On my way to see CheckForWinning, I noticed that WordRandomizer can be written as a single line, so the whole function probably could be scrapped.

    You really are going to have to step through this, though. I think I ended up misunderstanding the question, cause I was looking at whether it checked for winning correctly, but now I see you were talking about a counter, which isn't necessarily the same thing.
    My usual boring signature: Nothing

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,756

    Re: Help with hangman project.

    I was posting when you replied, so my final statement is now irrelevant.

    The guess count is the number of buttons that have been clicked, right? So that seems to be a counter that gets incremented in any of the letter button clicks.
    My usual boring signature: Nothing

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,658

    Re: Help with hangman project.

    So, the game is functional, you just don't know when to change the image for a wrong letter choice.

    First, ask yourself where you determine that the letter chosen exists (hint, where you increment rightTry)

    You should use a local flag in the method and when you find a letter and increment rightTry, you set the flag.
    After the search, if the flag isn't set you know you didn't find a letter so you should increment your image pointer and update the hangman image, and also see if the game is over at that point (you've hung him).

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Re: Help with hangman project.

    Quote Originally Posted by Shaggy Hiker View Post
    I was posting when you replied, so my final statement is now irrelevant.

    The guess count is the number of buttons that have been clicked, right? So that seems to be a counter that gets incremented in any of the letter button clicks.
    Sorry for the miscommunication. So the program works at the moment, each button has a val of the alphabet assigned to it, when you run the program it works, if you click a button it will take that letter and see if that letter is part of the word, if the letter is part of word it will replace the dash with the letter. What I need to do is, if the button clicked doesn't match to any letter in the word then it needs to switch to the next picture of hangman, so every time the button is incorrect a counter will add +1 to varIncorrect, I stepped through my program and I can't figure out when the button doesn't replace anything to add 1 to varIncorrect. Then varIncorrect will go into the image function and every time you enter an incorrect letter the picture will change.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Re: Help with hangman project.

    Name:  forum2.PNG
Views: 36
Size:  15.3 KB

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Re: Help with hangman project.

    Quote Originally Posted by passel View Post
    So, the game is functional, you just don't know when to change the image for a wrong letter choice.

    First, ask yourself where you determine that the letter chosen exists (hint, where you increment rightTry)

    You should use a local flag in the method and when you find a letter and increment rightTry, you set the flag.
    After the search, if the flag isn't set you know you didn't find a letter so you should increment your image pointer and update the hangman image, and also see if the game is over at that point (you've hung him).
    How would I go about doing that? I've been looking at a computer screen all day so my brain can't think straight.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,756

    Re: Help with hangman project.

    Ah, I misunderstood what you were counting. Passel has it right, then.

    The other points I made are still valid. You can greatly simplify the button click handler if you give it the right signature. I would still have assumed that it wouldn't even compile as you posted it, since the event handler takes two arguments, yet the method you have handling the button click event takes no arguments at all. I'm not all that curious as to how MS worked around that, but you really DO want those arguments, because you could get rid of both of those loops and really tighten up the code if you made use of the Sender argument, and also made use of the .Tag property to hold either the index of the letter, or the letter itself.

    What Passel was saying is that you put a flag variable (a Boolean) into the SearchAndReplace() method. This starts out as False. Right below the line where you increment rightTry, set it to True. Therefore, if you get out of the loop and it is still False, then you have never found the letter, because if you HAD found the letter, you would have set it to True. So, if it is still False, you know you haven't found the letter, so advance the image.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width