help with Guess Word Program

jdp972

New member
Joined
Oct 11, 2013
Messages
2
Programming Experience
Beginner
Option Strict On
Option Explicit On


Module Guesswork
    Const charLimit As Integer = 12
    Dim sWord(charLimit) As Char
    Dim uGuess(charLimit) As Char
    Dim wordInput As String
    Const _space As String = "  "
    Const blank As String = "__"
    Dim letrInput As String
    Dim correctLetr(sWord.Length) As Char
    Dim wrongLetr(totAttempts) As Char
    Dim totAttempts As Integer = 0
    Dim attemptsEof As Integer = 0
    Dim spcCounter As Integer = 0

    Sub gameModule()


        Console.WriteLine("Enter the secret word:  ")
        wordInput = Console.ReadLine
        sWord = wordInput.ToCharArray
        Do Until correctLetr = sWord
            Do While sWord.Length > 12
                Console.WriteLine("Enter word with 12 or less characters:  ")
                wordInput = Console.ReadLine
                sWord = wordInput.ToCharArray
            Loop

            sWord = wordInput.ToCharArray

            Console.WriteLine()

            

            Console.WriteLine()
            Console.WriteLine()



            If correctLetr.Length < sWord.Length Then
                Console.WriteLine("Enter a letter: ")
                letrInput = Console.ReadLine

                Do While letrInput.Length > 1
                    Console.WriteLine("Enter one letter at a time:  ")
                    letrInput = Console.ReadLine
                Loop
                uGuess = letrInput.ToCharArray
            End If

            For count4 = 0 To sWord.Length - 1
                For count6 = 0 To uGuess.Length - 1
                    If uGuess(count6) = sWord(count4) Then
                        correctLetr = letrInput.ToCharArray
                    End If

                    'Can you give me some insight as to why this isn't working? It is
                    ' putting the letrInput into both correctletr array
                    '  and wrongletr array. I've tried it several ways, looked on
                    ' msdn forums, and it seems like it should work, but its not

                    If uGuess(count6) <> sWord(count4) Then
                        wrongLetr = letrInput.ToCharArray
                    End If
                Next
            Next

            Console.Clear()

            'I have these two writing in the program so I could see what values are being assigned
            ' to them and it confirms they are being assigned the same character regardless
            'of the loop I set up

            Console.WriteLine(correctLetr)
            Console.WriteLine(wrongLetr)

            Console.WriteLine()
            Console.WriteLine("Incorrect letters: " & wrongLetr)
            Console.WriteLine()

            totAttempts = CInt((sWord.Length / 2) + 1)
            Console.WriteLine("Total Attempts: " & totAttempts)
            Console.WriteLine("Missed Attempts: " & wrongLetr.Length)

            attemptsEof = totAttempts - wrongLetr.Length
            Console.WriteLine("Remaining attempts: " & attemptsEof)

            Console.WriteLine()



            For count1 = 0 To sWord.Length - 1
                For count2 = 0 To correctLetr.Length - 1
                    If correctLetr(count2) = sWord(count1) Then
                        Console.Write(correctLetr(count2) & _space)
                    End If
                    If correctLetr(count2) <> sWord(count1) Then
                        Console.Write(blank & _space)
                    End If
                Next
            Next

            Console.WriteLine()

        Loop
    End Sub


    Sub Main()
        Dim newGame As String = "yes"
        Dim quitGame As String = "no"
        Dim gameInput As String

        Console.WriteLine("Welcome to GuEsS WoRd!")
        Console.WriteLine()

        Do Until correctLetr = sWord
            gameModule()
            If correctLetr = sWord Then
                Console.WriteLine("You Win.")
                Console.WriteLine("Would you like to play again?")
                gameInput = Console.ReadLine
            ElseIf newGame = gameInput Then
                gameModule()
            ElseIf quitGame = gameInput Then
            End If
            If wrongLetr.Length = totAttempts Then
                Console.WriteLine("You Lose.")
                Console.WriteLine("Would you like to play again?")
                gameInput = Console.ReadLine
                If newGame = gameInput Then
                    gameModule()
                ElseIf quitGame = gameInput Then
                End If
            End If
        Loop
        Console.Read()

    End Sub
End Module
 
Last edited by a moderator:
If you would like us to help you then you might try asking a question. We're not just going to read your code on the off chance that we'll see something wrong. If you're having an issue then tell us what that issue is. If you can't be bothered to make the effort to explain the issue then we're unlikely to be bothered to make the effort to help you with it.

EDIT: Right after posting that I noticed that you had a couple of comments in your code that indicated what the issue is. That's a really bad idea because we won't see them if we don't read the code. Of course, I only noticed the comments because they were highlighted green because because JohnH added xcode tags for you. Without that I still wouldn't have seen them. You might take some more time over your posts in future and make them clear, thus helping us and yourself. The easier you make it for us to help you, the more chance that we will and, if we don't have to waste our valuable time doing so, by reading unformatted code and searching for the issue because you haven't explained it, the more chance we'll do so in future too.
 
Firstly, this code is bad:
                    If uGuess(count6) = sWord(count4) Then
                        correctLetr = letrInput.ToCharArray
                    End If
 
                    'Can you give me some insight as to why this isn't working? It is
                    ' putting the letrInput into both correctletr array
                    '  and wrongletr array. I've tried it several ways, looked on
                    ' msdn forums, and it seems like it should work, but its not
 
                    If uGuess(count6) <> sWord(count4) Then
                        wrongLetr = letrInput.ToCharArray
                    End If
There's no way that both those conditions can be True at the same time so you should not be testing both. If you want to do one thing if a condition is True and another if it's False then you test the condition once only and use an If...Else statement:
                    If uGuess(count6) = sWord(count4) Then
                        correctLetr = letrInput.ToCharArray
                    Else
                        wrongLetr = letrInput.ToCharArray
                    End If
As for why the code isn't working the way you expect, you should debug it to work that out. Just reading your code is often not enough to determine where there's an issue, especially for us when we didn't write it. Debugging allows you to watch the code as it executes, which is much more illuminating. Place a break point at the top of the code (F9) and then step through it one line at a time (F8 or F10 depending on your settings). Before each step, use the Autos, Locals and Watch windows, etc, to determine the state of the app and what you expect to happen. After the step, evaluate the state of the app again and compare reality with expectation. If they differ then you have found the exact location of an issue and you know exactly what should have happened, what did happen and what data was in use at the time. That should be enough to fix it and, if it's not, you can at least pass on far more relevant information to us.
 
First off, thanks to JohnH.

My bad, when I posted the thread I didn't know I had to have an xcode and I thought the green messages would come up clear enough. Anyways, I have been debugging the program. I constantly do that. The problem is that I am taking an online class, with a professor who is part time. So, I have no one to ask questions to and our textbook only explains theory, and not any specifics about language. Everything I learned so far is from msdn help menu or forums like these.

1) Ok, I see your point about the using the if...then...else. But, I tried that already and I tried it again. The code still didn't work, it is still entering the input letter into both arrays.

2) Also, the arrays are only holding one value at a time and I have no clue why or how to fix it.
 
The first thing I see that doesn't look right is this section of code:

VB.NET:
        Console.WriteLine("Enter the secret word:  ")
        wordInput = Console.ReadLine
        sWord = wordInput.ToCharArray
        Do Until correctLetr = sWord
            Do While sWord.Length > 12
                Console.WriteLine("Enter word with 12 or less characters:  ")
                wordInput = Console.ReadLine
                sWord = wordInput.ToCharArray
            Loop

What you need to do first is make sure the word is the correct length before proceeding with the rest of the code. Do something like this before continuing with the rest of the code, which includes saving to a CharArray:

VB.NET:
 Do      
 	Console.Write("Enter the secret word with 12 or fewer characters:  ")
        wordInput = Console.ReadLine
	If wordinput.Length > 12 Then
		Console.WriteLine("Word is too long.")
        End If
 Loop While wordinput.Length > 12

In addition, it looks like you are addressing two different users; the first one to enter a secret word, and the second to guess it. I would include instructions to clear the console after the secret word is entered, before continuing with the rest of the code. For example:

VB.NET:
Console.WriteLine("Press Enter to clear the screen before next person tries to guess the secret word.")
Console.ReadLine()
Console.Clear()
sWord = wordInput.ToCharArray

Now you can work on fixing the main section of your code.
 
Last edited:

Latest posts

Back
Top