942,964 Members | Top Members by Rank

Ad:
Apr 2nd, 2009
0

VB5 hangman help

Expand Post »
My name is Stephanie and I have no idea what I am doing. Here is what I need help with:

Modify the code in the btnGuessW_Click event procedure. Add the code to implement the following logic after the existing code:
• Test the value in strWordGuessed against the value in strWordToGuess.
• If incorrect (different values), increment intNumWrongTries.
 When the number of incorrect answers is less than six, display the following string in a MessageBox control:
" That is not correct. You have guessed wrong y times."

Replace y with the number of wrong guesses. Use the following syntax for the MessageBox control:
MessageBox.Show("Text to display")
 When the number of wrong tries equals six, display the message "You lose." and display strWordtoGuess in lblWord. Use the following syntax for the MessageBox control:
MessageBox.Show("Text to display")
• If correct, display strWordtoGuess in lblWord, and display the following message:
"You guessed the word in x tries."
Replace x with the number of guesses.

The code I have so far:
Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click

Dim strWordGuessed = "BASSETT HOUND"
intNumTries += 1
strWordGuessed = txtWord.Text.ToUpper
strWordGuessed(0) = strWordToGuess "B"
strWordGuessed(1) = strWordToGuess "A"
strWordGuessed(2) = strWordToGuess "S"
strWordGuessed(3) = strWordToGuess "S"
strWordGuessed(4) = strWordToGuess "E"
strWordGuessed(5) = strWordToGuess "T"
strWordGuessed(6) = strWordToGuess "T"
strWordGuessed(7) = strWordToGuess "H"
strWordGuessed(8) = strWordToGuess "O"
strWordGuessed(9) = strWordToGuess "U"
strWordGuessed(10) = strWordToGuess "N"
strWordGuessed(11) = strWordToGuess "D"
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stephiesue02 is offline Offline
2 posts
since Apr 2009
Apr 2nd, 2009
0

Re: VB5 hangman help

I have been working on this more and here is the code I have now but still have errors I can't fix. Any suggestions?
Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click

strWordGuessed = txtWord.Text
strWordGuessed = strWordGuessed.ToUpper
intNumTries += 1
If Not strWordToGuess.Contains(strWordGuessed) Then
intNumWrongTries += 1
If intNumWrongTries < 6 Then
MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
Else
MessageBox.Show("You lose.")
lblWord.Text = strWordToGuess
End If
Else
lblWord.Text = strWordToGuess
MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
End If
End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stephiesue02 is offline Offline
2 posts
since Apr 2009
Apr 2nd, 2009
0

Re: VB5 hangman help

Hangman. I always thought you guessed letters and not words. Have they come up with a new way of playing the game?

I would say that you need to set up a string array that matches the word that you are guessing.

And then I would create a string array for the correct word.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim strArrayWord(11) as string
  2. Dim strArrayGuessed(11) as string

Every Time a correct letter (letter not word--little bit of a difference) is guessed, you fill the strArrayGuessed(x) with the proper value.

So, if the word is BassettHound and the user chose 's,' then strArrayGuessed(2) = "s": strArrayGuessed(3) = "s"

Your next step would be to check each array to see that each member matches. You could use a for loop:
VB Syntax (Toggle Plain Text)
  1. dim bCorrect as Boolean
  2. bCorrect = True
  3. for i = 0 to 11
  4. if strArrayGuessed(i) = strArrayWord(i) then
  5.  
  6. else
  7. bCorrect = False
  8. exit for
  9. End if
  10. next i
  11.  
  12. if bCorrect = True then
  13. ' Success
  14. else
  15. ' Failure
  16. End if

The Boolean variable bCorrect will not be left as True unless all letters of the word are correct. That's a start.
Last edited by hkdani; Apr 2nd, 2009 at 11:28 pm.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Please urgent.Help me with database coding on how to sort out into report.
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Corrupted Install Disk for VB 4.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC