Did a search but found nothing that answers my question. Was assigned the game Hangman as a test and I haven't been able to do anything myself and it's due tomorrow. I've looked on tons of sites for help, but so far haven't found any.

The test says

You need to read a random word from a text file(included).

The user will hit the "Get Word/Phrase" button. It will then pick a random word/phrase from the text file and display that many labels at the bottom of the screen. Then enter in a letter in the textbox and hit "Check". If it is found, display the letter in a label at the bottom. If it is not found display a portion of the person. Be sure to display all letters not found in the listbox.

I used arrays to pull the random word from a text file. Used this code. Tested it by showing word in a textbox. Seems to be working, but I don't know what to do from here. How am I supposed to get it to know how many letters, and then display that letter in the correct label? Once I figure that out I'm sure I can get the rest.

This is only code I have so far...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String
        Dim myArr(64145) As Object
        Dim i As Integer : i = 0

        FileOpen(1, My.Application.Info.DirectoryPath & "\Words.txt", OpenMode.Input)

        Do While Not EOF(1)
            s = LineInput(1)
            Dim myArray(Val(CStr(i)) + 1) As Object
            myArr(i) = s
            i = i + 1
        Loop

    End Sub

Recommended Answers

All 5 Replies

Since you're dealing with textual data, use string data type, not object. This is slightly modified code of yours

Dim s As String
Dim myArr(64145) As String
Dim i As Integer : i = 0

FileOpen(1, My.Application.Info.DirectoryPath & "\Words.txt", OpenMode.Input)

Do While Not EOF(1)
  s = LineInput(1)
  myArr(i) = s
  i = i + 1
Loop

Array is the way to do it and I assume you know how to pick an array element randomly i.e. generate random numbers. Here's the rest of the code (at least the important parts you need)

' Randomize strings, pick one and assign it to ThisString string variable
Dim ThisString As String
Dim ThisStringLength As Integer

' Now you know the length of the randomly selected word
ThisStringLength = ThisString.Length

' Search char positions, assign user input to this variable (one character, set TextBox MaxLength propery to 1)
Dim UserChar As String
Dim FoundAtPosition As Integer
Dim bFoundFlag As Boolean ' A little helper, set to True if user char matches

FoundAtPosition = ThisString.IndexOf(UserChar)
bFoundFlag = False
Do While FoundAtPosition >= 0 ' Notice, indexing starts from zero!
  bFoundFlag = True ' Flag as found (no hanging)
  ' FoundAtPosition points to position in the word where the char was found
  ' so set your label here
  '
  ' Check for the next UserChar occurrence, FoundAtPosition + 1 to not to "find" the same char again
  FoundAtPosition = ThisString.IndexOf(UserChar, FoundAtPosition + 1)
Loop

If Not bFoundFlag Then
  ' No match, hang 
End If

That's basically all you need. Just add user interface and error handling.

Array is the way to do it and I assume you know how to pick an array element randomly i.e. generate random numbers.

lol nah I'm a beginner. I have some notes on how to do random numbers since we did it on a project a few weeks ago, but can't get a random word out of a text box.

Ok. Here's how you get a random item from your word array

Dim oRandomGen As New Random
Dim SelIndex As Integer

' Lower limit in your array is zero and i variable holds the upper index
' get a random number between (zero, i)
SelIndex = oRandomGen.Next(0, i + 1)
ThisString = myArr(SelIndex)

Variable i holds your actual count of words read from the file.

Now you do have all the pieces. Combine them and if you encounter some problem, post your code here.

I got it working! Thanks bro.

Hi! Nice to hear that you got answer to your problem, could you please mark the thread as solved. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.