Specify a string function for Hangman Game
Hi I am in a basic programming class right now and I am starting to understand it all, but I am stumped on one of my instructions. I am using VB2008 Express and my instruction is:
In guessing a letter, specify the string function you will need to use to determine which values of "?" in lblWord need to be replaced with the correct letter. I have no idea what I should be using here, any help is greatly appreciated :) Thank you
P.S. - In case it helps below is the code I already have:
Public Class frmMain
Public strLetterGuessed As String
Public strWordGuessed As String
Public strWordToGuess As String
Public strLettersGuessed As String
Public intNumTries As Integer = 0
'Copy code from SampleCode.txt here during Part 3
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
lblWord.Text = "??????? ?????"
btnGuessL.Enabled = True
btnGuessW.Enabled = True
btnSolve.Enabled = True
strWordToGuess = "BASSETT HOUND"
intNumTries = 0
End Sub
Private Sub btnGuessL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessL.Click
txtLetter.Text = strLetterGuessed.ToUpper + ","
lblGuessed.Text = strLettersGuessed
intNumTries += 1
End Sub
Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click
txtWord.Text.ToUpper()
txtWord.Text = strWordGuessed
intNumTries += 1
End Sub
Private Sub btnSolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolve.Click
lblWord.Text = "BASSETT HOUND"
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Re: Specify a string function for Hangman Game
welcome to VBF
As in the game for real:
for each guess you need to check if the actual guess is in strWordToGuess, if yes you need to switch each ? that stands for the guessed letter with this letter.
Re: Specify a string function for Hangman Game
To find the letters, you need to use String.IndexOf:
Code:
Dim intPosition As Integer = strWordToGuess.IndexOf(strLetterGuessed, 0, StringComparison.CurrentCultureIgnoreCase)
You'll need to loop it though to find ALL the letters in the word though, since IndexOf will only tell you the position of the first one after the startIndex you specify (in the example above, this would be 0)
If it doesn't find any letters, it'll report -1
To replace the letters, you'll need to use String.Remove and String.Insert
Code:
strWordGuessed = strWordGuessed.Remove(intPosition, 1).Insert(intPosition, strLetterGuessed)
Now, using both these concepts, your logic should be similar to this:
- Check to see if the letter guessed is in the word
- If -1, then exit routine (return it was a bad guess)
- If not, then start a loop (Do Until intPosition = -1)
- Inside the loop, replace the first position the letter was found at.
- Re-check intPosition starting at intPosition + 1... if there are further letters to be replaced, it will loop, otherwise, it'll exit the loop.
- Exit routine (we replaced all the letters needed, return that it was a good guess)