Length of word in file

XeReX
05-16-2003, 07:48 AM
I am new with visual basic. I am trying to make a game.

From a file with random words (txt) i want to pick first 3 letters words, then 4 letter words etc.

If the pc picked the first random 3 letter word, then jumble it and you have three guesses. if you are correct then go to the 4 letter word etc.

This is what i have started with:

Private Sub Command3_Click()
Dim FileNum As Integer
FileNum = FreeFile
Open App.Path & ("\words.txt") For Input As FileNum
Do While Not (EOF(FileNum))
i = i + 1
ReDim Preserve wordsArr(i)
Input #FileNum, wordsArr(i)
Loop
For i = 1 To UBound(wordsArr)
List1.AddItem wordsArr(i)
Next i
End Sub

so i can display all the words. As you see, i am very new to VB and my english is not the best yet.
But thanks for you help!

SpaceFrog
05-16-2003, 08:02 AM
If it is not english what would it be ?

Why don't you associate the word with it's length in an array

Type WordsArr
Word as string * 30
Length as integer
end type

While scanning the textfile :

Do While Not (EOF(FileNum))
i = i + 1
ReDim Preserve wordsArr(i)
Input #FileNum, wordsArr(i).Word
Wordsarr(i).lenght = Len(wordsarr(i).word)
Loop

The sort the array on the .length basis have a look in the tutors corner, there's a very interesting thread on the subject

Robse
05-16-2003, 12:15 PM
I like the idea of using a type to store the words in. This way you could
also have a boolean variable in it and check that to see whether the
word has already been asked before. But you shouldn't need to store
the extra information about the word's length in the type, you can just
get that with the Len() funcition. Also if I understand you correctly,
there is no need to sort the array.

The type:

Private Type WordsArr
Word As String
Used As Boolean
End Type

Private Words() As WordsArr


To input the words from the textfile:

Open "c:\words.txt" For Input As FileNum

Do While Not (EOF(FileNum))
i = i + 1
ReDim Preserve Words(i)
Input #FileNum, Words(i).Word
Loop

Close #FileNum 'Don't forget to close the file


To retrieve a word of length iCurrLen,
that has not been asked before;

Private Function GetWord(iCurrLen As Integer) As String
Dim i As Integer
For i = 1 To UBound(Words)
If Len(Words(i).Word) = iCurrLen And Not Words(i).Used Then
Words(i).Used = True '"Mark" word as used
GetWord = Words(i).Word
Exit Function
End If
Next i
End Function

(If there are no more words, it will return an empty string)

Now all you need is a function that scrambles your words,
and to tie it all together into a little game. Any questions
ask again.

SpaceFrog
05-19-2003, 02:02 AM
Well I'm not quite sure that calculating separately the length of the word would gain time if the operation is repeated number of times ...
Nevertheless instead using a bool to check if word has already been asked, you could use an integer to count the occurences ...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum