I'll just skip to the important part:
I'm REMAKING a jeopardy type game and trying to improve my previous version. So I rewrote the code for all of it based on some of the old code.
The game asks you to select a TEXT file. It opens this file and reads all its data. It stores this data into the FILEDATA() array of a custom defined type (the defined type is called JEOPARDY). Each COUNT in this FILEDATA() array is assigned to a category it reads from the file. The name of the Category is assigned to the variable FILEDATA(x).Category. The Questions from the file are loaded to the FILEDATA(x).QUESTIONS() array. (Same thing goes for the answers!)
The thing is, the file contains extra categories and extra questions within each category. THE GAME CAN ONLY CONTAIN 5 CATEGORIES & 5 QUESTIONS IN EACH CATEGORY. Therefore, I made a GAME() array define as the JEOPARDY type to take only the important data!
I made a code to randomly pick categories & questions. It can only pick a certain category once! In each category, it must select 5 different questions.
I made it uniquely select the categories and that works fine. HOWEVER, when it selects the questions, it sometimes stops proceeding! SOMETIMES IT WORKS :S. I made it log almost every action it takes! I just can't figure out why it SOMETIMES stops loading when it hits a certain question.
I've attached the code. Please take a look at it, and tell me what's wrong with it. If you are willing to write me your own version of the code that does the same task then please do so!
Thank you for your time and effort! I appreciate all the help I receive!
I K
Last edited by INF3RN0666; Feb 11th, 2004 at 05:40 PM.
Originally posted by INF3RN0666 It seems to hang just when its going too fast.
That is ridiculous. I've never heard of code "hanging" because it was executing "too fast". If it is doing anything, it may be changing something too fast for you to see it (I haven't looked at the code yet).
Use Debug.print, put in message boxes, show variable states. All normal debugging routines should show your answer or prove that it is working correctly and that maybe you need to put some pauses in the program.
I whipped up some code to help you pick the random categories and such easier. If you can't figure out how to integrate it into your program let me know, but if I do all the work for you you'll never learn. In your case you would call the pickRandomElements function once to pick the categories, then again to pick the questions.
VB Code:
Public Function PickRandomElements(NumberElements As Integer, ArrayLimit As Long) As Long()
'this function will return any number of random index values for a given array
'it doesn't return the values, only the index values
Dim tmpreturn() As Long
Dim pickarray() As Long 'this is the array I will be using to pick from
Dim i As Long
Dim x As Long
Dim thispick As Long
ReDim pickarray(ArrayLimit)
ReDim tmpreturn(NumberElements - 1)
'fill the array with all possible index values
For i = 0 To ArrayLimit
pickarray(i) = i
Next
'let's start picking
For x = 0 To NumberElements - 1
'pick an index within the current range
thispick = CLng(Rnd * UBound(pickarray))
'add it to the return array
tmpreturn(x) = pickarray(thispick)
'remove the one you just picked, and decrease the size of the array by 1
RemoveElement thispick, pickarray
Next
PickRandomElements = tmpreturn
End Function
Public Function RemoveElement(Index As Long, FromArray() As Long)
Dim i As Long
For i = Index To UBound(FromArray) - 1
FromArray(i) = FromArray(i + 1)
Next
ReDim Preserve FromArray(UBound(FromArray) - 1)
End Function
It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.
Personally I don't think there's anythong wrong with GoTo statements and if you guys think there is, then you're strictly mistaken. They are just very slow, but hey, WE ARE USING VB (ONE OF THE EASIEST, SLOWEST, & MOST INCAPABLE LANGUAGE) LOL.
I knew my code got stuck in that section but I couldn't pinpoint why. It only happend every once in a while, so I was confused. When I redid the loading file, it works every time :S.
Anyways, thanx to dalceon, I have a new idea of how to write my new code. I'll just make a new function from scratch based on when you showed me. I need it for convenience for the entire program.