Hangman game

cor_dog2

Member
Joined
Feb 15, 2005
Messages
7
Programming Experience
Beginner
I'm trying to make a hangman game and i cannot get it to work right. What i want it to do is: prompt for a word to guess. Display # of dashes for the word entered. And have how ever many letters the word is that was entered multiply it by 2 and that is how many guesses you will get. If all guesses are used up, then it will say game over and tell you what the word was. If guessed right it will say good guessing. My code is as follows so far:


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

'simulates the hangman game

'declares variables

Dim strWord As String 'stores the word to be guessed

Dim strLetter As String 'stores the letter guesses

Dim blnDashReplaced As Boolean 'indicates if a dash was replaced

Dim blnGameOver As Boolean 'indicates if the game is over

Dim intIncorrectGuesses As Integer 'counts the number of incorrect guesses

Dim intIndex As Integer 'keeps track of the indexes

'get a 5-letter word from the first player

strWord = InputBox("Enter a word:", "Word")

Do While strWord.Length > 1

strWord = InputBox("Enter a word:", "Word")

Loop

'convert word to uppercase

strWord = strWord.ToUpper()

'display dashes for word entered ??

Me.lblWord.Text = "-----"

'clear the lblIncorrectGuesses control

Me.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 10 incorrect guesses

Do While Not blnGameOver

'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(Me.lblWord.Text, intIndex + 1) = strLetter

'indicate that a replacement was made

blnDashReplaced = True

End If

Next intIndex

'determine whether a replacement was made

If blnDashReplaced Then

'if the word does not contain any dashes, then

'the user guessed the word, so the game is over

If Me.lblWord.Text.IndexOf("-") = -1 Then

blnGameOver = True

MessageBox.Show("Great guessing!", "Hangman Game", _

MessageBoxButtons.OK, MessageBoxIcon.Information, _

MessageBoxDefaultButton.Button1)

Else 'reset the blnDashReplaced variable

blnDashReplaced = False

End If

Else 'processed when the word contains at least 1 dash

'update the counter variable, then display the result

intIncorrectGuesses = intIncorrectGuesses + 1

Me.lblIncorrectGuesses.Text = Convert.ToString(intIncorrectGuesses)

'determine whether the user has made 10 incorrect guesses, the game is over

blnGameOver = True

Me.lblWord.Text = "Game Over"

MessageBox.Show("Sorry, the word is " & strWord, "Hangman Game", _

MessageBoxButtons.OK, MessageBoxIcon.Information, _

MessageBoxDefaultButton.Button1)

End If

Loop

End Sub

End
Class


Any help/code that anyone can provide would be greatly appreciated. Thanks, Correy.
 
Last edited:

cor_dog2

Member
Joined
Feb 15, 2005
Messages
7
Programming Experience
Beginner
Well I can get the dialog box to prompt you to type a initial word to guess. But i can only get it to let me select a word of so many characters that i specify. I want to be able to type in any word length in the prompt and then have it show that many "dashes" the word contains. Thats one part i'm having issues with.

The second part is the guessing. I guess once and it says game over. I want it to let me have twice the amount of characters the intial word had multiplied by two for the amount of guesses.

Keep in mind I am a beginner so I hope this helps. let me know if you need any other info. Thanks.
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
This'll give you the correct number of -'s
VB.NET:
Dim i as integer = 0
while i < strWord.lengh[size=2][color=#008000]
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].lblWord.Text += "-"[/size]
i+=1
end while

You'll want to change this:
strLetter = strWord.ToUpper()

Dim a number of allowed guesses set it using the strWord.length*2

then use
If NOT blnDashReplaced Then
number of guesses -+1
end if
if number of guesses =0 then

gameover....
end if
 

cor_dog2

Member
Joined
Feb 15, 2005
Messages
7
Programming Experience
Beginner
Ok thank you for that info....I got the dashes part figured out. But now when i go to guess a letter for the first time it says wrong game over and tells me what the word was. It is not letting me guess as many times as i should be able to and its not replacing the dashes with the letter i choose either. I tried playing around with the code you gave me without luck. This is the new code i have now:

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

'simulates the hangman game

'declares variables

Dim strWord AsString'stores the word to be guessed

Dim strLetter AsString'stores the letter guesses

Dim blnDashReplaced AsBoolean'indicates if a dash was replaced

Dim blnGameOver AsBoolean'indicates if the game is over

Dim intIncorrectGuesses AsInteger'counts the number of incorrect guesses

Dim intIndex AsInteger'keeps track of the indexes

Dim i AsInteger = 0

Dim intAllowedGuesses AsInteger



'get a word from the first player

strWord = InputBox("Enter a word:", "Word")

'convert word to uppercase

strLetter = strWord.ToUpper()

'display dashes for word entered

While i < strWord.Length

Me.lblWord.Text += "-"

i += 1

EndWhile





'clear the lblIncorrectGuesses control

Me.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

DoWhileNot blnGameOver

'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(Me.lblWord.Text, intIndex + 1) = strLetter

'indicate that a replacement was made

blnDashReplaced = True

EndIf

Next intIndex

'determine whether a replacement was made

If Not blnDashReplaced Then

intAllowedGuesses = -+1

EndIf

If intAllowedGuesses = 0 Then

'if the word does not contain any dashes, then

'the user guessed the word, so the game is over

blnGameOver = True

MessageBox.Show("Great guessing!", "Hangman Game", _

MessageBoxButtons.OK, MessageBoxIcon.Information, _

MessageBoxDefaultButton.Button1)

Else
'reset the blnDashReplaced variable

blnDashReplaced = False

EndIf

'processed when the word contains at least 1 dash

'update the counter variable, then display the result

intIncorrectGuesses = intIncorrectGuesses + 1

intAllowedGuesses = intAllowedGuesses * strWord.Length * 2

Me.lblIncorrectGuesses.Text = Convert.ToString(intIncorrectGuesses)

'determine whether the user has made too many incorrect guesses, the game is over

blnGameOver = True

Me.lblWord.Text = "Game Over"

MessageBox.Show("Sorry, the word is " & strWord, "Hangman Game", _

MessageBoxButtons.OK, MessageBoxIcon.Information, _

MessageBoxDefaultButton.Button1)

'End If

Loop

'clear game over message to start over again

Me.lblWord.Text = ""

EndSub

End
Class


Can you please let me know what i'm doing wrong? Thanks you for your time..I appreciate it and I'm actually learning quite a bit from this!
 
Last edited:

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
You cahnged the wrong one, you now have 2: strLetter = strWord.ToUpper() instead of strWord = strWord.ToUpper()
strLetter = strLetter.ToUpper()
Sorry I should have explained what I ment by change this.

My last post also had a typo, it should be intAllowedGuesses -=1 so that every incorrect guess subtracts 1 from the guesses.

You'll also want to move where you work out the incorrect guesses up to before the loop otherwise it'll reset everyloop. I think you want intAllowedGuesses = strWord.length * 2

You need to remove :
VB.NET:
[color=#008000]'determine whether the user has made too many incorrect guesses, the game is over

[/color][size=2]blnGameOver = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].lblWord.Text = "Game Over"

MessageBox.Show("Sorry, the word is " & strWord, "Hangman Game", _

MessageBoxButtons.OK, MessageBoxIcon.Information, _

MessageBoxDefaultButton.Button1)

[/size][size=2][color=#008000]'End If
[/color][/size]
It's just before the last loop.
 
Top Bottom