Hangman Game Substring and dash issues

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2097 Views - Last Post: 30 October 2011 - 01:20 PM Rate Topic: -----

#1 blackbyron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-September 11

Hangman Game Substring and dash issues

Posted 20 October 2011 - 01:34 PM

Hello, my teacher assigns me to modify the program hangman game that allow a player to enter a word that contains any letters up to 10 letters. That assignment is from my textbook, Programming with MS Visual Basic 2010 by Diane Zak

Here is my code, it's long I know. Then I'll explain what the specific problem I'm having.

' Name:         Hangman Game Project
' Purpose:      Simulates the Hangman game
' Programmer:   <Byron Luo>
' Date:         <10/18/11>

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain


    Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub mnuFileNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click
        ' simulates the Hangman game
        Dim strWord As String
        Dim strLetter As String
        Dim blnValidWord As Boolean
        Dim blnDashReplaced As Boolean
        Dim blnGameOver As Boolean
        Dim intIncorrect As Integer

        ' hide the picture boxes
        picBottom.Visible = False
        picPost.Visible = False
        picTop.Visible = False
        picRope.Visible = False
        picHead.Visible = False
        picBody.Visible = False
        picRightArm.Visible = False
        picLeftArm.Visible = False
        picRightLeg.Visible = False
        picLeftLeg.Visible = False

        ' get a 1 to 10 letter word from player 1 and convert to uppercase
        strWord = InputBox("Enter a one to ten letter word:",
                           "Hangman Game").ToUpper

        ' determine whether the word contains one to ten letters
        blnValidWord = True     ' assume the word is valid
        If strWord.Length >= 1 AndAlso strWord.Length <= 10 Then
            Dim intIndex As Integer
            Do While intIndex < 10 AndAlso blnValidWord = True
                If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then
                    blnValidWord = False
                End If
                intIndex = intIndex + 1
            Loop

        Else
            blnValidWord = False
        End If

        ' if the word is not valid, display a message
        If blnValidWord = False Then
            MessageBox.Show("One to 10 letter are required.",
                            "Hangman Game",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information)

        Else
            ' display one to ten dashes in lblWord and clear lblIncorrect
            lblWord.Text = "-----"
            lblIncorrect.Text = String.Empty

            ' get a letter from player 2 and convert to uppercase
            strLetter = InputBox("Enter a letter:",
                                 "Letter", "", 600, 400).ToUpper
            ' verify that player 2 entered a letter
            ' and that the game is not over
            Do While strLetter <> String.Empty AndAlso
                blnGameOver = False

                ' search the word for the letter
                For intIndex As Integer = 0 To strWord.Length - 1
                    ' if the letter apeears in the word, then
                    ' replace the dash in lblWord and
                    ' indicate that a replacement was made
                    If strWord.Substring(intIndex, 1) = strLetter Then
                        lblWord.Text = lblWord.Text.Remove(intIndex, 1)
                        lblWord.Text = lblWord.Text.Insert(intIndex, strLetter)
                        blnDashReplaced = True
                    End If
                Next intIndex


                ' determine whether a dash was replaced
                If blnDashReplaced = True Then
                    ' if the word does not contain any dashes,
                    ' the game is over because player 2
                    ' guessed the word; otherwise, reset the
                    ' blnDashReplaced variable for the next search
                    If lblWord.Text.Contains("-") = False Then
                        blnGameOver = True
                        MessageBox.Show("Great guessing!",
                                        "Game Over",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information)
                    Else
                        blnDashReplaced = False

                    End If

                Else   ' processed when no dash was replaced
                    ' display the incorrect letter, then update
                    ' the intIncorrect variable, then show
                    ' the appropriate picture box
                    lblIncorrect.Text =
                        lblIncorrect.Text & " " & strLetter
                    intIncorrect = intIncorrect + 1
                    Select Case intIncorrect
                        Case 1
                            picBottom.Visible = True
                        Case 2
                            picPost.Visible = True
                        Case 3
                            picTop.Visible = True
                        Case 4
                            picRope.Visible = True
                        Case 5
                            picHead.Visible = True
                        Case 6
                            picBody.Visible = True
                        Case 7
                            picRightArm.Visible = True
                        Case 8
                            picLeftArm.Visible = True
                        Case 9
                            picRightLeg.Visible = True
                        Case 10
                            picLeftLeg.Visible = True
                            blnGameOver = True
                            MessageBox.Show("Sorry, the word is " &
                                            strWord & ".", "Game Over",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Information)
                    End Select

                End If
                ' determine whether to get another letter
                If blnGameOver = False Then
                    strLetter = InputBox("Enter a letter", "Letter", "", 600, 400).ToUpper
                End If
            Loop
        End If
    End Sub

End Class




Now here's the problem I'm having trouble with. I don't know if my substrings is wrong or right, but when I type a word, the problem crashes and gives me the error of this.

Posted Image

and another problem I'm having trouble with is how can I have dashes to match with the length of words?

For example, if I type "game", it'll display "----" in the exe program. If I type "cat", it'll display "---" in the exe program as well.

Any ideas?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Hangman Game Substring and dash issues

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,662
  • Joined: 29-May 08

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 01:46 PM

Think through your code and try to work out what happen if the word length is less than 10.
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 01:51 PM

Look at your loop:
Do While intIndex < 10 AndAlso blnValidWord = True
    If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then

You said the word contains up to 10 letters, and you go to 9!!!, then find a substring from index 9 of length 1 which means from 9- 10, so probably 10 ill be out of index because the last index of string is length - 1.
To solve the problem, I advice you use Do While intIndex < (strWord.Length() - 1) AndAlso blnValidWord = True or like so, we make sure here we go to the maximum index available according to the entered string...

EDIT: and please nect time just paste the error message and the line it happens by words, because some of us, cant view pictures here

This post has been edited by smohd: 20 October 2011 - 01:53 PM

Was This Post Helpful? 1
  • +
  • -

#4 blackbyron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-September 11

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 02:16 PM

View Postsmohd, on 20 October 2011 - 01:51 PM, said:

Look at your loop:
Do While intIndex < 10 AndAlso blnValidWord = True
    If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then

You said the word contains up to 10 letters, and you go to 9!!!, then find a substring from index 9 of length 1 which means from 9- 10, so probably 10 ill be out of index because the last index of string is length - 1.
To solve the problem, I advice you use Do While intIndex < (strWord.Length() - 1) AndAlso blnValidWord = True or like so, we make sure here we go to the maximum index available according to the entered string...

EDIT: and please nect time just paste the error message and the line it happens by words, because some of us, cant view pictures here

Thanks for your reply. No problem man.

Well, I added <= on the Do while loop because it'll set numbers to false. I don't really understand why the numbers will be set to true when it's "<".
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 02:31 PM

I dont get what you are talking about, but I was talking about setting loop to loop ten times, while a word can be less than that in length. Also because you are getting the substring(which is not a good way here because you are trying to get only one character, why dont you use Char(index) property of the string to get the character at certain index? then just say:
        If strWord.Chars(Count) Like "[!A-Z]" Then

        End If
I think this will simplify the work of substringing.
Was This Post Helpful? 0
  • +
  • -

#6 blackbyron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-September 11

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 02:55 PM

View Postsmohd, on 20 October 2011 - 02:31 PM, said:

I dont get what you are talking about, but I was talking about setting loop to loop ten times, while a word can be less than that in length. Also because you are getting the substring(which is not a good way here because you are trying to get only one character, why dont you use Char(index) property of the string to get the character at certain index? then just say:
        If strWord.Chars(Count) Like "[!A-Z]" Then

        End If
I think this will simplify the work of substringing.

Wow, that seems easier, but sadly, I haven't learn this one yet because right now, I just learned the string manipulation, and I just looked at the index on the textbook, and it doesn't say anything about using the char property. I'm not going to use the char property yet. I just want to go step by step.
I appreciate your help on fixing my substring issues. Now I think everything works correctly except I'm having difficulties with having "-" string to match the length of the the word I type.
I read the textbook on string manipulation, but can't seem to find out the example about it. I'm thinking I have to use string method. What are you're ideas on it? Unless I figure it out.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 03:01 PM

Just make sure you add "-" equal to the strWord.Length, this will do the trick, just using the loop to go adding "-" character according to strWord.Length
Was This Post Helpful? 0
  • +
  • -

#8 blackbyron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-September 11

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 03:20 PM

Hmmm, but does it really work when it's in Option Strict On? Otherwise, I'll give this one a try.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1948
  • View blog
  • Posts: 8,662
  • Joined: 29-May 08

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 03:24 PM

You don't even need the .Char as Strings implement an Indexer Property.
If strWord(Count) Like "[!A-Z]" Then

End If

This post has been edited by AdamSpeight2008: 20 October 2011 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 03:25 PM

Sure it do, something like:
lblWord.Text = ""
For i As Integer = 0 To strWord.Length - 1
  lblWord.Text += "-"
Next

Was This Post Helpful? 1
  • +
  • -

#11 blackbyron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-September 11

Re: Hangman Game Substring and dash issues

Posted 20 October 2011 - 03:35 PM

Doh, Wow I forgot about that for loop, I'm thinking myself too hard on this. Wow, Thanks for your help man. I appreciated it.

Now, I'm going to correct the other problem on my own.

+rep
Was This Post Helpful? 0
  • +
  • -

#12 ilost1mind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 11

Re: Hangman Game Substring and dash issues

Posted 29 October 2011 - 12:20 PM

so what does your final code look like for the issue of the loop. I am reading and seeing all the input and can't figure out who or what advice was correct. thank you.
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 29 October 2011 - 12:55 PM

bout what loop? post #9 by AdamSpeight2008 shows how to use string and get character at every index, then just add loop in it to loop through the whole string.
If you have the same problem please post your code and we are happy to help you figure it out
Was This Post Helpful? 0
  • +
  • -

#14 ilost1mind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 11

Re: Hangman Game Substring and dash issues

Posted 29 October 2011 - 01:12 PM

View Postsmohd, on 29 October 2011 - 12:55 PM, said:

bout what loop? post #9 by AdamSpeight2008 shows how to use string and get character at every index, then just add loop in it to loop through the whole string.
If you have the same problem please post your code and we are happy to help you figure it out



my code is below, i am having two issues, i have placed the font in bold for both of them: one is the mnufilenew_click, it requires me to put a me.click, it will not accept the text mnufilenew and second it the same problem as the previous user; i used the suggest (count) but do i need to add dim count as integer above it to be recognized...i am not asking for the answer just point me in the right direction. I enjoy vb coding but sometimes i get stuck and right now i am stuck. thanks so much for the help


' Project name:     Hangman Game Project
' Project purpose:  The project simulates the Hangman game.
' Created/revised:  <your name> on <current date>

Option Explicit On
Option Strict On
Option Infer Off


Public Class frmmain

    Private Sub mnufileexit_click(ByVal sender As Object, ByVal e As System.EventArgs)

        Me.Close()
    End Sub

    Private Sub mnufilenew_click(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles [b]Me.Click[/b]
        'simulates the game

        Dim strword As String
        Dim strletter As String
        Dim blnvalidword As Boolean
        Dim blndashreplaced As Boolean
        Dim blngameover As Boolean
        Dim intincorrect As Integer


        'hides pic boxes
        picbottom.Visible = False
        picpost.Visible = False
        pictop.Visible = False
        picrope.Visible = False
        pichead.Visible = False
        picbody.Visible = False
        picrightarm.Visible = False
        picleftarm.Visible = False
        picrightleg.Visible = False
        picleftleg.Visible = False

        'get 5-letter word from player 1 and convert to upper
        strword = InputBox("Enter up to a 10-letter word:",
                           "Hangman Game").ToUpper

        'determines if word contains letters
        blnvalidword = True
        'assume word is valid
        If strword.Length >= 1 AndAlso
            strword.Length <= 10 Then
            Dim intindex As Integer
            Do While intindex <= 10 AndAlso blnvalidword = True
               [b] If strword(count) Like "[!A-Z]" Then[/b]

                End If
                intindex = intindex + 1
            Loop

        Else
            blnvalidword = False
        End If

        'if word in not valid, display message
        If blnvalidword = False Then
            MessageBox.Show("Up to a 10-letter word is required.",
                            "Hangman Game",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information)


        Else
            'display 10 dashes in lblword and clear lblincorrect
            lblword.Text = "----------"
            lblincorrect.Text = String.Empty

            'get a letter from player 2 and convert to upper case
            strletter = InputBox("Enter a letter:",
                                 "Letter", "", 600, 400).ToUpper

            'verify player 2 enters a letter and that game is not over
            Dim intindex As Integer
            Do While strletter <> String.Empty AndAlso
                blngameover = False
                'search the word for a letter
                For intindex = 0 To strword.Length - 1
                    'if letter appears in word then replace dash with letter
                    If strword.Substring(intindex, 1) = strletter Then
                        lblword.Text = lblword.Text.Remove(intindex, 1)
                        lblword.Text = lblword.Text.Insert(intindex, strletter)
                        blndashreplaced = True
                    End If
                Next intindex

                'determine whether a dash was replaced

                If blndashreplaced = True Then
                    'if word doesnt contain dashes
                    'game is over because playerr 2 won
                    'otherwise, reset the blndashpreplaced variable for 
                    'next search
                    If lblword.Text.Contains("-") = False Then
                        blngameover = True
                        MessageBox.Show("Great Guessing!",
                                        "Game Over",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information)
                    Else
                        blndashreplaced = False
                    End If

                Else
                    lblincorrect.Text =
                        lblincorrect.Text & " " & strletter
                    intincorrect = intincorrect + 1
                    Select Case intincorrect
                        Case 1
                            picbottom.Visible = True
                        Case 2
                            picpost.Visible = True
                        Case 3
                            pictop.Visible = True
                        Case 4
                            picrope.Visible = True
                        Case 5
                            pichead.Visible = True
                        Case 6
                            picbody.Visible = True
                        Case 7
                            picrightarm.Visible = True
                        Case 8
                            picleftarm.Visible = True
                        Case 9
                            picrightleg.Visible = True
                        Case 10
                            picleftleg.Visible = True
                            blngameover = True
                            MessageBox.Show("Sorry, the word is " & strword & ".", "Game Over",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Information)
                    End Select

                End If
                'determine where to get another letter
                If blngameover = False Then
                    strletter = InputBox("Enter a letter",
                                         "Letter", "", 600, 400).ToUpper
                End If
            Loop
        End If
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub
End Class



i see it did not bold, it is lines 18 and 52 that are my issues, i believe anyway...thanks
Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1745
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Hangman Game Substring and dash issues

Posted 29 October 2011 - 04:15 PM

What is mnufilenew? if not the current form it should be mnufilenew.Click

Yes your count should be declared, you have no variable called count there. And it is thee one you call intindex, also your Else is in the wrong position. That part of code will be:
If strword.Length >= 1 AndAlso
            strword.Length <= 10 Then
            Dim intindex As Integer = 0
            Do While intindex <= 10 AndAlso blnvalidword = True
                If strword(intindex) Like "[!A-Z]" Then

                Else
                     blnvalidword = False
                End If
                intindex = intindex + 1
            Loop

        
        End If

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2