Results 1 to 9 of 9

Thread: Hangman Game Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Hangman Game Help

    I am new to vb and am trying to make the infamous hangman game, but what I have so far doesn't seem to work. I am having troubles with when the person enters a word, turning that into " - " dashes for the length of the word, and also incorporating spaces that do not show the dash. Thanks for any help you can give me also the code I have so far I will post. Thanks


    Option Explicit On
    Option Strict On

    Public Class HangmanForm

    Private Sub HangmanForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    'Closes the program
    End
    End Sub

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click

    'declare variables
    Dim strWord As String 'stores the word to be guessed
    Dim strLetter As String 'stores the letter guesses
    Dim blnIsDashReplaced As Boolean 'indicates if a dash was replaced
    Dim blnIsGameOver As Boolean 'indicates if the game is over
    Dim intIncorrectCounter As Integer 'counts the number of incorrect guesses
    Dim intIndex As Integer 'keeps track of the indexes
    Dim intAllowedGuesses As Integer


    'Hide picture boxes
    pctPostBottom.Visible = False
    pctPostSide.Visible = False
    pctPostTop.Visible = False
    pctRope.Visible = False
    pctHead.Visible = False
    pctBody.Visible = False
    pctRightArm.Visible = False
    pctLeftArm.Visible = False
    pctRightLeg.Visible = False
    pctLeftLeg.Visible = False


    'Get a word from the first player
    strWord = InputBox("Enter A Secret Word:", "Secret Word")

    'Convert word to uppercase

    strLetter = strWord.ToUpper()

    'Count characters in string
    Dim intLength As Integer
    intLength = strWord.Length

    'Varible for dashes to be placed
    Dim strDash As String

    'Display dashes for word entered
    Dim intCount As Integer
    For intCount = 0 To intLength - 1
    strDash = strDash & "-"
    Next intCount

    'declare variable for a space
    Dim strSpace As String = " "

    'declare position variable to check for space
    Dim intChkSpace As Integer

    'Declare a starting position variable
    Dim intStart As Integer = 1

    'Set loop counter to 0
    intCount = 0

    'Searches for a space in the word
    For intCount = 0 To intLength - 1
    intChkSpace = InStr(intStart, strWord, strSpace, CompareMethod.Text)
    If intChkSpace = 0 Then
    Exit For 'Exits loop if there is no space
    Else
    'Put space where it belongs
    Mid(lblSecretWord.Text, intChkSpace, 1) = strDash
    intStart = intChkSpace + 1
    End If
    Next intCount

    'displays - for every letter in the string
    lblSecretWord.Text = strDash

    'Clear the lblIncorrectGuesses control

    lblIncorrectGuesses.Text = ""

    'Allow the second player to guess a letter

    'The game is over when either the word has been guessed or

    'The second player makes too many incorrect guesses

    Do Until blnIsGameOver

    Loop

    'Get a letter from the second player
    strLetter = InputBox("Enter A Letter:", "Letter", "", 500, 400)

    'Convert letter to uppercase
    strLetter = strWord.ToUpper

    'Search the word for the letter
    For intIndex = 0 To strWord.Length - 1
    If strWord.Substring(intIndex, 1) = strLetter Then

    'replace appropriate dash in the lblWord control
    Mid(lblSecretWord.Text, intIndex + 1) = strLetter

    'indicate that a replacement was made
    blnIsDashReplaced = True

    End If

    Next intIndex

    'Determine whether a replacement was made

    If blnIsDashReplaced Then
    intAllowedGuesses = -+1
    If intAllowedGuesses = 0 Then
    'if the word does not contain any dashes, then
    'the user guessed the word, so the game is over
    blnIsGameOver = True
    MessageBox.Show("Great guessing!", "Hangman Game")
    Else
    'reset the blnDashReplaced variable
    blnIsDashReplaced = False
    End If
    Else
    'Proceesed when no dash was replaced
    'Diaplay the incorrect letter, then update
    'The incorrect counter variable, and show
    'The appropriate picture box
    lblIncorrectGuesses.Text = lblIncorrectGuesses.Text & " " & strLetter
    intIncorrectCounter = intIncorrectCounter + 1
    Select Case intIncorrectCounter
    Case 1
    pctPostBottom.Visible = True
    Case 2
    pctPostSide.Visible = True
    Case 3
    pctPostTop.Visible = True
    Case 4
    pctHead.Visible = True
    Case 5
    pctBody.Visible = True
    Case 6
    pctRightArm.Visible = True
    Case 7
    pctLeftArm.Visible = True
    Case 8
    pctRightLeg.Visible = True
    Case 9
    pctLeftLeg.Visible = True
    blnIsGameOver = True
    MessageBox.Show("Sorry, the word is " & strWord & ".", "Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Select
    End If
    End Sub
    End Class

  2. #2

  3. #3
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Hangman Game Help

    Even with VBCODE tags its still difficult to read, to make it easier on the eye use tabs in your nested ifs and loops such as
    VB Code:
    1. for x = 1 to 10
    2.      if something then
    3.           if something
    4.               'do something
    5.           end if
    6.      end if
    7. next x

    the easier it is to read the more people will try to help
    Chris

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: Hangman Game Help

    Ok I wil try to adjust it, Thanks, Also what are vb Tags

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Hangman Game Help

    When you want to post vb code into a thread, if you wrap your code in [ VBCODE ] tags then it will format your code so its easy to read. For example:

    [ VBCODE ]

    Private Sub....

    [/ VBCODE ]

    But don't include the spaces
    Chris

  6. #6

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: Hangman Game Help

    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class HangmanForm
    5.  
    6. Private Sub HangmanForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.  
    8. End Sub
    9.  
    10. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    11. 'Closes the program
    12. End
    13. End Sub
    14.  
    15. Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
    16.  
    17. 'declare variables
    18. Dim strWord As String 'stores the word to be guessed
    19. Dim strLetter As String 'stores the letter guesses
    20. Dim blnIsDashReplaced As Boolean 'indicates if a dash was replaced
    21. Dim blnIsGameOver As Boolean 'indicates if the game is over
    22. Dim intIncorrectCounter As Integer 'counts the number of incorrect guesses
    23. Dim intIndex As Integer 'keeps track of the indexes
    24. Dim intAllowedGuesses As Integer
    25.  
    26.  
    27. 'Hide picture boxes
    28. pctPostBottom.Visible = False
    29. pctPostSide.Visible = False
    30. pctPostTop.Visible = False
    31. pctRope.Visible = False
    32. pctHead.Visible = False
    33. pctBody.Visible = False
    34. pctRightArm.Visible = False
    35. pctLeftArm.Visible = False
    36. pctRightLeg.Visible = False
    37. pctLeftLeg.Visible = False
    38.  
    39.  
    40. 'Get a word from the first player
    41.  
    42. strWord = InputBox("Enter A Secret Word:", "Secret Word")
    43.  
    44. 'Convert word to uppercase
    45. strLetter = strWord.ToUpper()
    46.  
    47. 'Count characters in string
    48. Dim intLength As Integer
    49. intLength = strWord.Length
    50.  
    51. 'Varible for dashes to be placed
    52. Dim strDash As String
    53.  
    54. 'Display dashes for word entered
    55. Dim intCount As Integer
    56. For intCount = 0 To intLength - 1
    57. strDash = strDash & "-"
    58.  
    59. Next intCount
    60.  
    61. 'declare variable for a space
    62. Dim strSpace As String = " "
    63.  
    64. 'declare position variable to check for space
    65. Dim intChkSpace As Integer
    66.  
    67. 'Declare a starting position variable
    68. Dim intStart As Integer = 1
    69.  
    70. 'Set loop counter to 0
    71. intCount = 0
    72.  
    73. 'Searches for a space in the word
    74. For intCount = 0 To intLength - 1
    75. intChkSpace = InStr(intStart, strWord, strSpace, CompareMethod.Text)
    76.      If intChkSpace = 0 Then
    77. Exit For 'Exits loop if there is no space
    78.      Else
    79.  
    80. 'Put space where it belongs
    81. Mid(lblSecretWord.Text, intChkSpace, 1) = strDash
    82. intStart = intChkSpace + 1
    83. End If
    84.  
    85. Next intCount
    86.  
    87. 'displays "-" for every letter in the string
    88. lblSecretWord.Text = strDash
    89.  
    90. 'Clear the lblIncorrectGuesses control
    91. lblIncorrectGuesses.Text = ""
    92.  
    93. 'Allow the second player to guess a letter
    94.  
    95. 'The game is over when either the word has been guessed or
    96.  
    97. 'The second player makes too many incorrect guesses
    98.  
    99. Do Until blnIsGameOver
    100.  
    101. Loop
    102.  
    103. 'Get a letter from the second player
    104. strLetter = InputBox("Enter A Letter:", "Letter", "", 500, 400)
    105.  
    106. 'Convert letter to uppercase
    107. strLetter = strWord.ToUpper
    108.  
    109. 'Search the word for the letter
    110. For intIndex = 0 To strWord.Length - 1
    111.       If strWord.Substring(intIndex, 1) = strLetter Then
    112. 'replace appropriate dash in the lblWord control
    113. Mid(lblSecretWord.Text, intIndex + 1) = strLetter
    114.  
    115. 'indicate that a replacement was made
    116. blnIsDashReplaced = True
    117.  
    118. End If
    119.  
    120. Next intIndex
    121.  
    122. 'Determine whether a replacement was made
    123.  
    124.      If blnIsDashReplaced Then
    125. intAllowedGuesses = -+1
    126.      If intAllowedGuesses = 0 Then
    127. 'if the word does not contain any dashes, then
    128. 'the user guessed the word, so the game is over
    129. blnIsGameOver = True
    130. MessageBox.Show("Great guessing!", "Hangman Game")
    131.      Else
    132. 'reset the blnDashReplaced variable
    133. blnIsDashReplaced = False
    134. End If
    135. Else
    136. 'Proceesed when no dash was replaced
    137. 'Diaplay the incorrect letter, then update
    138. 'The incorrect counter variable, and show
    139. 'The appropriate picture box
    140. lblIncorrectGuesses.Text = lblIncorrectGuesses.Text & " " & strLetter
    141. intIncorrectCounter = intIncorrectCounter + 1
    142. Select Case intIncorrectCounter
    143. Case 1
    144. pctPostBottom.Visible = True
    145. Case 2
    146. pctPostSide.Visible = True
    147. Case 3
    148. pctPostTop.Visible = True
    149. Case 4
    150. pctHead.Visible = True
    151. Case 5
    152. pctBody.Visible = True
    153. Case 6
    154. pctRightArm.Visible = True
    155. Case 7
    156. pctLeftArm.Visible = True
    157. Case 8
    158. pctRightLeg.Visible = True
    159. Case 9
    160. pctLeftLeg.Visible = True
    161. blnIsGameOver = True
    162. MessageBox.Show("Sorry, the word is " & strWord & ".", "Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
    163. End Select
    164. End If
    165. End Sub
    166. End Class

  8. #8
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Hangman Game Help

    Yes thats right, now all you need to do is post your question in the correct forum, you will notice we're in the Classic VB forum, you want the .Net forum:

    http://vbforums.com/forumdisplay.php?f=25

    you could wait for a mod to move this thread, or just post your question in the .net forum. I'm sure someone over there can help you with the code
    Chris

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Hangman Game Help

    Quote Originally Posted by the182guy
    Yes thats right, now all you need to do is post your question in the correct forum, you will notice we're in the Classic VB forum, you want the .Net forum:

    http://vbforums.com/forumdisplay.php?f=25

    you could wait for a mod to move this thread, or just post your question in the .net forum. I'm sure someone over there can help you with the code
    Since it is game related, I think here is more appropriate.

    Moved.

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