Results 1 to 3 of 3

Thread: Specify a string function for Hangman Game

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    1

    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

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width