0

I'm having a hard time trying to display an error message once someone has already entered in that letter. I'm super new to all of this so I have no idea what I'm doing but I can't get it to work. Please help, I've been trying to fix this for a week and need help! I've done this ->

I have LettersNotGuessedYet as ""

'Displays an error message if a letter is used twice or more
        GuessThere = InStr(LettersNotGuessedYet, GuessString)
        If GuessThere = 0 Then
            LettersNotGuessedYet += GuessString
        Else
            Guess1There = InStr(LettersNotGuessedYet, GuessString)
            If Guess1There = 1 Then
                MessageBox.Show("blah"blahh ")
            End If
        End If

It works for the first letter I put in (which is GuessString) but then anything else I type in after doesnt display any message?

Edited by Arsenal0202

2
Contributors
1
Reply
28
Views
1 Year
Discussion Span
Last Post by invisal
1

Welcome to Daniweb, let me completely guide you to creating the most basic HangMan game using VB.NET in Console Application. Then, you can apply the basic principle to your Hangman project in WinForm.

First of all, we started with the empty VB.NET console project.

Sub Main()
End Sub

GameLoop

You need to create a game loop, setting up all the possible words, and determine the maximum number of attempt that player can guess. The game loop will simply keep asking if the player want to continue playing our game.

Const MaxNumberOfAttempt As Integer = 10
Dim Words As String() = {"LOVE", "DANIWEB", "PROGRAMMING"}

Sub Main()
    Do
        Game()
        Console.WriteLine("Press 'y' and enter to continue")
    Loop Until Console.ReadLine() <> "y"
End Sub

Sub Game()
End Sub

Game

In this section, we will focus on what inside our Game() function.

Sub Game()
End Sub

First of all, we need to pick one word out of our word list.

Sub Game()
    '' Picking one word from our possible word list
    Dim HiddenWord As String = Words(New Random().Next(0, Words.Length - 1))
End Sub

Then, we have create another variable to hold the correct letters that player has guess in our hidden word. At first, our player hasn't guess any letter. Therefore, all the letter is hidden. For example, if our hidden word is "DANIWEB", the initial correct guess is "-------".

 Sub Game()
    '' Picking one word from our possible word list
    Dim HiddenWord As String = Words(New Random().Next(0, Words.Length - 1))

    '' Hidden all the letter of the hidden word with letter -
    Dim GuessWord As String = StrDup(HiddenWord.Length, "-")
End Sub

There are two more important variables that we need to create. The first one is storing the number of attempt that our player has guessed so far. The second variable is to store all the letter that our player has guessed including the correct and incorrect letter.

Sub Game()
    '' Picking one word from our possible word list
    Dim HiddenWord As String = Words(New Random().Next(0, Words.Length - 1))

    '' Hidden all the letter of the hidden word with letter -
    Dim GuessWord As String = StrDup(HiddenWord.Length, "-")

    '' Number of attempt that our player has tried
    Dim NumberOfAttempt = 0

    '' Letter that our player has guessed
    Dim GuessedLetters As String = ""

    Console.WriteLine("Hidden Word: " + GuessWord)
End Sub

Now we are ready to get input from our player and doing the guess loop.

Guess Loop

This section will focus primiary on the guess loop in our Game() function.

Sub Game()
    '' Picking one word from our possible word list
    Dim HiddenWord As String = Words(New Random().Next(0, Words.Length - 1))

    '' Hidden all the letter of the hidden word with letter -
    Dim GuessWord As String = StrDup(HiddenWord.Length, "-")

    '' Number of attempt that our player has tried
    Dim NumberOfAttempt = 0

    '' Letter that our player has guessed
    Dim GuessedLetters As String = ""

    Console.WriteLine("Hidden Word: " + GuessWord)

    While (NumberOfAttempt < MaxNumberOfAttempt)
         '' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         '' << WE WILL FOCUS HERE
         '' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    End While

    Console.WriteLine("You run out of guess, you lose. The hidden word is " + HiddenWord)
End Sub

To avoid repeating a lot of code in this post. We will zoom into the guess loop and ignore the rest of the code in the Game() function.

While (NumberOfAttempt < MaxNumberOfAttempt)
        Console.Write("Please type letter you want to guess: ")
        Dim Letter As String = Console.ReadLine()(0).ToString().ToUpper

        GuessedLetters += Letter
        NumberOfAttempt = NumberOfAttempt + 1
End While

The above code is obvious. We simply let the player guess at most MaxNumberOfAttempt number of time. Each time, we will record there guessed letter. Right now, we haven't prevented the duplicated letter guess and whether we have guessd the correct letter.

Adding whether we have input the duplicated letter from previous guess is easy.

While (NumberOfAttempt < MaxNumberOfAttempt)
    Console.Write("Please type letter you want to guess: ")
    Dim Letter As String = Console.ReadLine()(0).ToString().ToUpper

    If (GuessedLetters.Contains(Letter)) Then
        '' Player has already guess this letter before
        Console.WriteLine("You have already guessed this letter. Please choose another letter")
    Else
        GuessedLetters += Letter
        NumberOfAttempt = NumberOfAttempt + 1
    End If
End While

Noticed that, when player input the duplicated letter, we don't increase the number of attempt. Now, it is time to add some games logic. Let handle when player input the wrong letter.

While (NumberOfAttempt < MaxNumberOfAttempt)
    Console.Write("Please type letter you want to guess: ")
    Dim Letter As String = Console.ReadLine()(0).ToString().ToUpper

    If (GuessedLetters.Contains(Letter)) Then
        '' Player has already guess this letter before
        Console.WriteLine("You have already guessed this letter. Please choose another letter")
    Else
        If HiddenWord.Contains(Letter) Then
             '' Right letter
        Else
             '' Wrong letter
             Console.WriteLine("Wrong guess")
        End If

        GuessedLetters += Letter
        NumberOfAttempt = NumberOfAttempt + 1
    End If
End While

We only show the message that they guess it wrong when they input the wrong letter. Now, it is time to handle when they got the right letter.

While (NumberOfAttempt < MaxNumberOfAttempt)
    Console.Write("Please type letter you want to guess: ")
    Dim Letter As String = Console.ReadLine()(0).ToString().ToUpper

    If (GuessedLetters.Contains(Letter)) Then
        '' Player has already guess this letter before
        Console.WriteLine("You have already guessed this letter. Please choose another letter")
    Else
        If HiddenWord.Contains(Letter) Then
            '' Right letter
            '' Identified the location of the correct letter in the hidden word
            '' and replace the - letter with the correct letter.
            For i As Integer = 0 To HiddenWord.Length - 1
                If Letter = HiddenWord(i) Then
                    GuessWord = GuessWord.Substring(0, i) + Letter + GuessWord.Substring(i + 1)
                End If
            Next

            '' You have fully guess the correct word, you win
            If (GuessWord = HiddenWord) Then
                Console.WriteLine("You win. the hidden word is " + HiddenWord)
                Return
            Else
                Console.WriteLine("Right guess. Here is your guess word " + GuessWord)
            End If
        Else
             '' Wrong letter
             Console.WriteLine("Wrong guess")
        End If

        GuessedLetters += Letter
        NumberOfAttempt = NumberOfAttempt + 1
    End If
End While

Finally, we have the full basic functional Hangman game in VB.NET using the console. Here is the full code:

Const MaxNumberOfAttempt As Integer = 10
Dim Words As String() = {"LOVE", "DANIWEB", "PROGRAMMING"}

Sub Main()
    Do
        Game()
        Console.WriteLine("Press 'y' and enter to continue")
    Loop Until Console.ReadLine() <> "y"
End Sub

Sub Game()
    '' Picking one word from our possible word list
    Dim HiddenWord As String = Words(New Random().Next(0, Words.Length - 1))

    '' Hidden all the letter of the hidden word with letter -
    Dim GuessWord As String = StrDup(HiddenWord.Length, "-")

    '' Number of attempt that our player has tried
    Dim NumberOfAttempt = 0

    '' Letter that our player has guessed
    Dim GuessedLetters As String = ""

    Console.WriteLine("Hidden Word: " + GuessWord)

    While (NumberOfAttempt < MaxNumberOfAttempt)
        Console.Write("Please type letter you want to guess: ")
        Dim Letter As String = Console.ReadLine()(0).ToString().ToUpper

        If (GuessedLetters.Contains(Letter)) Then
            '' Player has already guess this letter before
            Console.WriteLine("You have already guessed this letter. Please choose another letter")
        Else
            '' Check if we got the right letter
            If HiddenWord.Contains(Letter) Then
                For i As Integer = 0 To HiddenWord.Length - 1
                    If Letter = HiddenWord(i) Then
                        GuessWord = GuessWord.Substring(0, i) + Letter + GuessWord.Substring(i + 1)
                    End If
                Next

                If (GuessWord = HiddenWord) Then
                    Console.WriteLine("You win. the hidden word is " + HiddenWord)
                    Return
                Else
                    Console.WriteLine("Right guess. Here is your guess word " + GuessWord)
                End If

            Else
                Console.WriteLine("Wrong guess")
            End If

            GuessedLetters += Letter
            NumberOfAttempt = NumberOfAttempt + 1
        End If
    End While

    Console.WriteLine("You run out of guess, you lose. The hidden word is " + HiddenWord)
End Sub

Now, you can apply some techique that I have used in this guide to do in your project. Have fun.

Edited by invisal

Votes + Comments
Good one!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.