kdawn2475
02-27-04, 07:56 PM
Can anybody help me code this hangman game?
I am making a two person hangman game. The first person enters a phrase up to 30 characters in length and the second guesses the phrase.
It does not contain graphics, but I need to figure out how to get rid of the dashes "-" when a phrase is entered that contains spaces.
I am totally new to this so please don't laugh the amateaurish quality of the code :)
Here is what I have so far:
'create form level variable
Private mstrPhrase As String
Private Sub ProcessLetterLabels(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles GLable.Click, ALabel.Click, BLabel.Click, CLabel.Click, DLabel.Click, ELabel.Click, _
FLabel.Click, HLabel.Click, ILabel.Click, JLabel.Click, _
KLabel.Click, LLabel.Click, MLabel.Click, NLabel.Click, OLabel.Click, _
PLabel.Click, QLabel.Click, RLabel.Click, SLabel.Click, TLabel.Click, _
ULabel.Click, VLabel.Click, WLabel.Click, XLabel.Click, YLabel.Click, ZLabel.Click
Dim objLetterLabel As Label, intIndex As Integer, blnReplaced As Boolean
Dim strPartialphrase, strCharacter As String
'hide letter
objLetterLabel = sender
objLetterLabel.Visible = False
'assign status of phrase to strPartialPhrase
strPartialphrase = Me.PhraseLabel.Text
'search for letter
For intIndex = 0 To mstrPhrase.Length - 1
'assign chosen character to strCharacter
strCharacter = mstrPhrase.Substring(intIndex, 1)
'find match
If strCharacter = objLetterLabel.Text Then
'replace dash in strPartialPhrase
Mid(strPartialphrase, intIndex + 1) = objLetterLabel.Text
'indicate replacement
blnReplaced = True
End If
Next
'determine whether a replacement was made in strPartialPhrase
If blnReplaced = True Then
'determine if variable has a dash
If strPartialphrase.IndexOf("-") > -1 Then
Me.PhraseLabel.Text = strPartialphrase
Else 'correct guess
Me.PhraseLabel.Text = mstrPhrase
MessageBox.Show("Nice job!", "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
Me.GuessLabel.Text = Val(Me.GuessLabel.Text) + 1
If Val(Me.GuessLabel.Text) = 7 Then
Me.PhraseLabel.Text = "Game Over"
MessageBox.Show("OOPS, the phrase is " & mstrPhrase, "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
Private Sub FileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileNewMenuItem.Click
'get phrase
mstrPhrase = UCase(InputBox("Enter A Phrase up to 30 Characters", "Hangman"))
'determine if phrase is 30 characters
If mstrPhrase.Length <= 30 Then
'display label controls
Dim objcontrol As Control
For Each objcontrol In Controls
objcontrol.Visible = True
Next objcontrol
'display dashes
Me.PhraseLabel.Text = "------------------------------"
'replace dashes
ElseIf mstrPhrase.IndexOf(" ") Then
Dim strPhrase = Val(Me.PhraseLabel.Text)
strPhrase = strPhrase.replace("-", " ")
'set guess label to zero
Me.GuessLabel.Text = 0
Else
'display message
MessageBox.Show("Please enter a phrase up to 30 characers", "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub FileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileExitMenuItem.Click
Me.Close()
End Sub
I am making a two person hangman game. The first person enters a phrase up to 30 characters in length and the second guesses the phrase.
It does not contain graphics, but I need to figure out how to get rid of the dashes "-" when a phrase is entered that contains spaces.
I am totally new to this so please don't laugh the amateaurish quality of the code :)
Here is what I have so far:
'create form level variable
Private mstrPhrase As String
Private Sub ProcessLetterLabels(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles GLable.Click, ALabel.Click, BLabel.Click, CLabel.Click, DLabel.Click, ELabel.Click, _
FLabel.Click, HLabel.Click, ILabel.Click, JLabel.Click, _
KLabel.Click, LLabel.Click, MLabel.Click, NLabel.Click, OLabel.Click, _
PLabel.Click, QLabel.Click, RLabel.Click, SLabel.Click, TLabel.Click, _
ULabel.Click, VLabel.Click, WLabel.Click, XLabel.Click, YLabel.Click, ZLabel.Click
Dim objLetterLabel As Label, intIndex As Integer, blnReplaced As Boolean
Dim strPartialphrase, strCharacter As String
'hide letter
objLetterLabel = sender
objLetterLabel.Visible = False
'assign status of phrase to strPartialPhrase
strPartialphrase = Me.PhraseLabel.Text
'search for letter
For intIndex = 0 To mstrPhrase.Length - 1
'assign chosen character to strCharacter
strCharacter = mstrPhrase.Substring(intIndex, 1)
'find match
If strCharacter = objLetterLabel.Text Then
'replace dash in strPartialPhrase
Mid(strPartialphrase, intIndex + 1) = objLetterLabel.Text
'indicate replacement
blnReplaced = True
End If
Next
'determine whether a replacement was made in strPartialPhrase
If blnReplaced = True Then
'determine if variable has a dash
If strPartialphrase.IndexOf("-") > -1 Then
Me.PhraseLabel.Text = strPartialphrase
Else 'correct guess
Me.PhraseLabel.Text = mstrPhrase
MessageBox.Show("Nice job!", "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
Me.GuessLabel.Text = Val(Me.GuessLabel.Text) + 1
If Val(Me.GuessLabel.Text) = 7 Then
Me.PhraseLabel.Text = "Game Over"
MessageBox.Show("OOPS, the phrase is " & mstrPhrase, "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
Private Sub FileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileNewMenuItem.Click
'get phrase
mstrPhrase = UCase(InputBox("Enter A Phrase up to 30 Characters", "Hangman"))
'determine if phrase is 30 characters
If mstrPhrase.Length <= 30 Then
'display label controls
Dim objcontrol As Control
For Each objcontrol In Controls
objcontrol.Visible = True
Next objcontrol
'display dashes
Me.PhraseLabel.Text = "------------------------------"
'replace dashes
ElseIf mstrPhrase.IndexOf(" ") Then
Dim strPhrase = Val(Me.PhraseLabel.Text)
strPhrase = strPhrase.replace("-", " ")
'set guess label to zero
Me.GuessLabel.Text = 0
Else
'display message
MessageBox.Show("Please enter a phrase up to 30 characers", "Hangman", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub FileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileExitMenuItem.Click
Me.Close()
End Sub