|
-
Sep 21st, 2006, 10:28 PM
#1
Thread Starter
Junior Member
Homework Question!!!
I need to modify the Hangman game application so that it allows the first student to enter a word that contains any number of characters.
Then.
I must also modify the application so that the number of incorrect guesses the user is allowed to make should be four more than the total number of characters in the original work. For example, if the original word contains seven characters, allow the user to make 11 incorrect guesses.
Here is what I have so far:
Private Sub uiFileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiFileNewMenuItem.Click
'simulates the Hangman game
Dim word As String 'stores the word to be guessed
Dim letter As String 'stores the letter guesses
Dim dashReplaced As Boolean 'indicates whether a dash was replaced
Dim gameOver As Boolean 'indicates whether the game is over
Dim incorrectGuesses As Integer 'keeps track of the number of incorrect guesses
Dim indexNum As Integer 'controls the loop that searches the word
'get a 5-letter word form the first player
Do
word = InputBox("Enter a 5-letter word", "Hangman game")
Loop Until word.Length = 5
'convert word to uppercase
word = word.ToUpper()
'display five dashes in uiWordLabel control
Me.uiWordLabel.Text = "-----"
'clear the label controls
Me.uiIncorrectGuessLabel.Text = ""
Me.uiIncorrectLettersLabel.Text = ""
'allow player 2 to guess a letter
'the game is over when either the word is guessed or
'player 2 makes 10 incorrect guesses
Do While Not gameOver
'get a letter from player 2
letter = InputBox("Enter a letter:", "Letter", "", 500, 500)
'conver letter to uppercase
letter = letter.ToUpper()
'search the word for the letter
For indexNum = 0 To word.Length - 1
If word.Substring(indexNum, 1) = letter Then
'replace appropriate dash in the uiWordLabel control
Mid(Me.uiWordLabel.Text, indexNum + 1) = letter
'indicate that a replacement was made
dashReplaced = True
End If
Next indexNum
'determine whether a replacement was made
If dashReplaced Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
If Me.uiWordLabel.Text.IndexOf("-") = -1 Then
gameOver = True
MessageBox.Show("Great Guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else 'reset the dashreplaced variable
dashReplaced = False
End If
Else 'processed when no dash was replaced
'display incorrect letter
Me.uiIncorrectLettersLabel.Text = _
Me.uiIncorrectLettersLabel.Text & " " & letter
'update the counter variable, then display the result
incorrectGuesses = incorrectGuesses + 1
Me.uiIncorrectGuessLabel.Text = Convert.ToString(incorrectGuesses)
'determine whether player 2 made 10 incorrect guesses
If incorrectGuesses = 10 Then
'the game is over
gameOver = True
Me.uiWordLabel.Text = "Game Over"
MessageBox.Show("Sorry, the word is " & word, "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Loop
End Sub
End Class
-
Sep 22nd, 2006, 08:36 AM
#2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|