Public Class MainForm
Private Sub xFileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileExitMenuItem.Click
Me.Close()
End Sub
Private Sub xFileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileNewMenuItem.Click
'simulates the hangman game
Dim word As String
Dim letter As String
Dim isDashReplaced As Boolean
Dim isGameOver As Boolean
Dim incorrectCounter As Integer
'hide the picture boxes
Me.xBottomPictureBox.Visible = False
Me.xPostPictureBox.Visible = False
Me.xTopPictureBox.Visible = False
Me.xRopePictureBox.Visible = False
Me.xHeadPictureBox.Visible = False
Me.xBodyPictureBox.Visible = False
Me.xRightArmPictureBox.Visible = False
Me.xRightLegPictureBox.Visible = False
Me.xLeftArmPictureBox.Visible = False
Me.xLeftLegPictureBox.Visible = False
'get a 5 to 10-letter word from first player
Do
word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
Loop Until word.Length >= 5 Or word.Length = 10
'convert the word to uppercase
word = word.ToUpper
'display 5-10 dashes in the xWordLabel
'clear the xIncorrectLabel
If word.Length = 5 Then
Me.xWordLabel.Text = "-----"
ElseIf word.Length = 6 Then
Me.xWordLabel.Text = "------"
ElseIf word.Length = 7 Then
Me.xWordLabel.Text = "-------"
ElseIf word.Length = 8 Then
Me.xWordLabel.Text = "--------"
ElseIf word.Length = 9 Then
Me.xWordLabel.Text = "---------"
Else
Me.xWordLabel.Text = "----------"
End If
Me.xIncorrectLabel.Text = String.Empty
'allow the second player to guess a letter
'the game is over when the second player
'either guesses the word or makes 10
'incorrect guesses
Do Until isGameOver
'get a letter from second player, then
'convert the letter to uppercase
letter = InputBox("Enter a letter:", _
"Letter", "", 450, 400)
letter = letter.ToUpper
'search the word for the letter
For indexnum As Integer = 0 To 4
'if the word contains the letter, then
'replace the dash in the xWordLabel and
'assign True to isDashRelaced to
'indicate that a replacement was made
If word.Substring(indexnum, 1) = letter Then
Mid(Me.xWordLabel.Text, indexnum + 1) _
= letter
isDashReplaced = True
End If
Next indexnum
'determine whether a dash was replaced
If isDashReplaced Then
'if the word does not contain any dashes,
'the game is over because the second
'player guessed the word; otherwise, reset
'the isDashreplaced variabls for the next
'search
If Me.xWordLabel.Text.Contains("-") = False Then
isGameOver = True
MessageBox.Show("Great guessing!", _
"Hangman Game", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Else
isDashReplaced = False
End If
Else 'processed when no dash was replaced
'display the incorrect letter, then update
'the incorrect letter, then update
'the appropriated picture box
Me.xIncorrectLabel.Text = _
Me.xIncorrectLabel.Text & " " & letter
incorrectCounter = incorrectCounter + 1
Select Case incorrectCounter
Case 1
Me.xBottomPictureBox.Visible = True
Case 2
Me.xPostPictureBox.Visible = True
Case 3
Me.xTopPictureBox.Visible = True
Case 4
Me.xRopePictureBox.Visible = True
Case 5
Me.xHeadPictureBox.Visible = True
Case 6
Me.xBodyPictureBox.Visible = True
Case 7
Me.xRightArmPictureBox.Visible = True
Case 8
Me.xLeftArmPictureBox.Visible = True
Case 9
Me.xRightLegPictureBox.Visible = True
Case 10
Me.xLeftLegPictureBox.Visible = True
isGameOver = True
MessageBox.Show("Sorry, the word is " _
& word & ".", "Hangman Game", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Select
End If
Loop
End Sub
End Class
Hangman Game Letter replace problemthe dash's change
25 Replies - 6827 Views - Last Post: 15 August 2007 - 03:35 PM
#1
Hangman Game Letter replace problem
Posted 12 August 2007 - 02:50 AM
ok i made a hangman game. you press the new game in the menu and it asked for a 5-10 letter word. it auto makes how many dash's instead of letters according to the number of letter you enter for the word. that works fine, but when guessing the letters and u get to 5 letters it takes the rest of the dash's away for some reason and wont let you get the whole word. heres my code
Replies To: Hangman Game Letter replace problem
#2
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 09:21 AM
his could be part of your problem, you're only searching through 5 characters, not the length of the word
'search the word for the letter For indexnum As Integer = 0 To 4 'if the word contains the letter, then 'replace the dash in the xWordLabel and 'assign True to isDashRelaced to 'indicate that a replacement was made f word.Substring(indexnum, 1) = letter Then Mid(Me.xWordLabel.Text, indexnum + 1) _ = letter isDashReplaced = True End If Next indexnum
#3
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 07:54 PM
so how would i write the code to correspond with how ever many letters are written for the word? would i do something like if, else ... or case, or i dunno
#4
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 08:05 PM
sorry i couldnt get back earlier i had a basketball game to attend then work following...
#5
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 08:10 PM
I would make word a Global variable
Then when you set the length when starting a new game
Now that variable is set and can be accessed in your other methods. So then, you can loop through the actual length of the word
Its ok bro, I was just having a bad day (VERY bad day)
and DIC is my escape, except there wasn't any activity so I had no way of escaping.
Private word As String
Then when you set the length when starting a new game
'get a 5 to 10-letter word from first player
Do
word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
Loop Until word.Length >= 5 Or word.Length = 10
'convert the word to uppercase
word = word.ToUpper
Now that variable is set and can be accessed in your other methods. So then, you can loop through the actual length of the word
'search the word for the letter For indexnum As Integer = 0 To word.Length 'since its a global you can reference it here 'if the word contains the letter, then 'replace the dash in the xWordLabel and 'assign True to isDashRelaced to 'indicate that a replacement was made if word.Substring(indexnum, 1) = letter Then Mid(Me.xWordLabel.Text, indexnum + 1) = letter isDashReplaced = True End If Next indexnum
TophCoder, on 12 Aug, 2007 - 08:05 PM, said:
sorry i couldnt get back earlier i had a basketball game to attend then work following...
Its ok bro, I was just having a bad day (VERY bad day)
#6
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 08:16 PM
thanks again for the help. i think your the only one who helps me 
u wouldnt have ne photoshop tutorials on siganture making would ya? hehe
u wouldnt have ne photoshop tutorials on siganture making would ya? hehe
#7
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 08:20 PM
No but Ill see if I can find one
#8
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 09:37 PM
ok so im tryin to code this game still ha. i used what u provided. i put the word im using in, and start guessing but it doesnt show any right or wrong words and comes up with an error on this part of the code...
this is the error.
Index and length must refer to a location within the string. Parameter name: length
'search the word for the letter For indexnum As Integer = 0 To word.Length 'since its a global you can reference it here 'if the word contains the letter, then 'replace the dash in the xWordLabel and 'assign True to isDashRelaced to 'indicate that a replacement was made error here-->if word.Substring(indexnum, 1) = letter Then<------------error here Mid(Me.xWordLabel.Text, indexnum + 1) = letter isDashReplaced = True End If Next indexnum
this is the error.
Index and length must refer to a location within the string. Parameter name: length
This post has been edited by TophCoder: 12 August 2007 - 09:41 PM
#9
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 09:44 PM
Sometimes I forget if strings are 0 or 1 based
try this
try this
'search the word for the letter For indexnum As Integer = 0 To word.Length - 1 'since its a global you can reference it here 'if the word contains the letter, then 'replace the dash in the xWordLabel and 'assign True to isDashRelaced to 'indicate that a replacement was made error here-->if word.Substring(indexnum, 1) = letter Then<------------error here Mid(Me.xWordLabel.Text, indexnum + 1) = letter isDashReplaced = True End If Next indexnum
#10
Re: Hangman Game Letter replace problem
Posted 12 August 2007 - 10:02 PM
well that got rid of that problem, but i have another. Its not replacing the - with the letter. even if its the right one it says wrong letter. and when its done its saying the word is ----- haha
#11
Re: Hangman Game Letter replace problem
Posted 13 August 2007 - 07:45 PM
u still with me here, did u understand what i was sayin?
#12
Re: Hangman Game Letter replace problem
Posted 13 August 2007 - 07:56 PM
Yeah I understood, been busy trying to figure something out for work (FINALLY got it working today). Post your updated code here so I can look at it
#16
Re: Hangman Game Letter replace problem
Posted 13 August 2007 - 08:12 PM
Private Sub xFileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xFileNewMenuItem.Click
'simulates the hangman game
Dim letter As String
Dim isDashReplaced As Boolean
Dim isGameOver As Boolean
Dim incorrectCounter As Integer
'hide the picture boxes
Me.xBottomPictureBox.Visible = False
Me.xPostPictureBox.Visible = False
Me.xTopPictureBox.Visible = False
Me.xRopePictureBox.Visible = False
Me.xHeadPictureBox.Visible = False
Me.xBodyPictureBox.Visible = False
Me.xRightArmPictureBox.Visible = False
Me.xRightLegPictureBox.Visible = False
Me.xLeftArmPictureBox.Visible = False
Me.xLeftLegPictureBox.Visible = False
'get a 5 to 10-letter word from first player
Do
word = InputBox("Enter a 5 to 10-letter word:", "Hangman Game")
Loop Until word.Length >= 5 Or word.Length = 10
'convert the word to uppercase
word = word.ToUpper
'display 5-10 dashes in the xWordLabel
'clear the xIncorrectLabel
If word.Length = 5 Then
Me.xWordLabel.Text = "-----"
ElseIf word.Length = 6 Then
Me.xWordLabel.Text = "------"
ElseIf word.Length = 7 Then
Me.xWordLabel.Text = "-------"
ElseIf word.Length = 8 Then
Me.xWordLabel.Text = "--------"
ElseIf word.Length = 9 Then
Me.xWordLabel.Text = "---------"
Else
Me.xWordLabel.Text = "----------"
End If
Me.xIncorrectLabel.Text = String.Empty
'allow the second player to guess a letter
'the game is over when the second player
'either guesses the word or makes 10
'incorrect guesses
Do Until isGameOver
'get a letter from second player, then
'convert the letter to uppercase
letter = InputBox("Enter a letter:", _
"Letter", " ", 450, 400)
letter = letter.ToUpper
'search the word for the letter
For indexnum As Integer = 0 To word.Length - 1
'if the word contains the letter, then
'replace the dash in the xWordLabel and
'assign True to isDashRelaced to
'indicate that a replacement was made
If word.Substring(indexnum, 1) = letter Then
Mid(Me.xWordLabel.Text, indexnum + 1) = letter
isDashReplaced = True
End If
Next indexnum
'determine whether a dash was replaced
If isDashReplaced Then
'if the word does not contain any dashes,
'the game is over because the second
'player guessed the word; otherwise, reset
'the isDashreplaced variabls for the next
'search
If Me.xWordLabel.Text.Contains("-") = False Then
isGameOver = True
MessageBox.Show("Great guessing!", _
"Hangman Game", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Else
isDashReplaced = False
OK so i got it to were i put the word in, guess the letters and they show up. but when i get to letter 4 it starts taking away to many dash's and when the word is done it still only shows 5 letters. so basically when i start guessing i see how ever many letters there should be in Dashs but when i end i see 5 letters and 0 dashs no matter how big the word is.
#17
Re: Hangman Game Letter replace problem
Posted 13 August 2007 - 08:22 PM
What comes after
Else isDashReplaced = False
#18
Re: Hangman Game Letter replace problem
Posted 13 August 2007 - 08:33 PM
Else
isDashReplaced = False
End If
Else 'processed when no dash was replaced
'display the incorrect letter, then update
'the incorrect letter, then update
'the appropriated picture box
Me.xIncorrectLabel.Text = _
Me.xIncorrectLabel.Text & " " & letter
incorrectCounter = incorrectCounter + 1
Select Case incorrectCounter
Case 1
Me.xBottomPictureBox.Visible = True
Case 2
Me.xPostPictureBox.Visible = True
Case 3
Me.xTopPictureBox.Visible = True
Case 4
Me.xRopePictureBox.Visible = True
Case 5
Me.xHeadPictureBox.Visible = True
Case 6
Me.xBodyPictureBox.Visible = True
Case 7
Me.xRightArmPictureBox.Visible = True
Case 8
Me.xLeftArmPictureBox.Visible = True
Case 9
Me.xRightLegPictureBox.Visible = True
Case 10
Me.xLeftLegPictureBox.Visible = True
isGameOver = True
MessageBox.Show("Sorry, the word is " _
& word & ".", "Hangman Game", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Select
End If
Loop
End Sub
just my hangman picture boxs for when the letter isnt in the word. and that seems to be working right.
|
|

New Topic/Question
Reply




MultiQuote



|