Results 1 to 10 of 10

Thread: I'm STUMPED - This makes no sense!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    I'm STUMPED - This makes no sense!

    Hey guys,

    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
    Attached Files Attached Files
    Last edited by INF3RN0666; Feb 11th, 2004 at 05:40 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Anyway you can just keep running it in debug mode to try and catch where it's hanging?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    It seems to hang just when its going too fast.

    Can someone look at the code please..
    Thank you

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    Anyone??

    Can someone atleast propose a coding idea for such a situation?

  5. #5
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    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.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    Your program seems to run into an infinite loop when running this code. I want even begin to tell you how bad using Goto Statements is.


    VB Code:
    1. frmLoadStatus.labProgress.Caption = "Picking Question " & Y & " in Category " & X & "..."
    2. PickQuestion:
    3.             Quest(Y) = Int(Rnd * UBound(FileData(Cat(X)).Questions()))
    4.            
    5.             Sleep 25
    6.            
    7.             For Z = 0 To Y
    8.                 If Z <> Y And Quest(Y) = Quest(Z) Then GoTo PickQuestion
    9.                 DoEvents
    10.             Next
    11.                        
    12.             Game(X).Questions(Y) = FileData(Cat(X)).Questions(Quest(Y))
    13.                        
    14.             DoEvents
    15.         Next
    16.        
    17.         DoEvents
    18.     Next

    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:
    1. Public Function PickRandomElements(NumberElements As Integer, ArrayLimit As Long) As Long()
    2. 'this function will return any number of random index values for a given array
    3. 'it doesn't return the values, only the index values
    4. Dim tmpreturn() As Long
    5. Dim pickarray() As Long 'this is the array I will be using to pick from
    6. Dim i As Long
    7. Dim x As Long
    8. Dim thispick As Long
    9.  
    10.  
    11. ReDim pickarray(ArrayLimit)
    12. ReDim tmpreturn(NumberElements - 1)
    13. 'fill the array with all possible index values
    14. For i = 0 To ArrayLimit
    15.    pickarray(i) = i
    16. Next
    17. 'let's start picking
    18. For x = 0 To NumberElements - 1
    19.    'pick an index within the current range
    20.    thispick = CLng(Rnd * UBound(pickarray))
    21.    'add it to the return array
    22.    tmpreturn(x) = pickarray(thispick)
    23.    'remove the one you just picked, and decrease the size of the array by 1
    24.    RemoveElement thispick, pickarray
    25. Next
    26. PickRandomElements = tmpreturn
    27. End Function
    28.  
    29. Public Function RemoveElement(Index As Long, FromArray() As Long)
    30. Dim i As Long
    31.  
    32. For i = Index To UBound(FromArray) - 1
    33.    FromArray(i) = FromArray(i + 1)
    34. Next
    35. ReDim Preserve FromArray(UBound(FromArray) - 1)
    36.  
    37. End Function
    It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.

  7. #7

  8. #8
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Originally posted by Wokawidget
    Ouch!!!

    Goto statements!

    Ooooowwwwwwch!



    That is some VERY bad coding techneques you are using there.

    Woka
    Whats wrong with goto's ? If you code them right there is no problem.

  9. #9
    Fanatic Member
    Join Date
    Sep 2000
    Location
    Over There
    Posts
    522
    yep they're great for on error goto but that's about it.

    People use goto's when they can't come up with a correct
    loop to do something. I can't think of a situation to ever use a goto.

    Plus goto statement's make tracing code almost impossible.
    It Never Fails. Everytime I try to make a program idiot proof, the world makes a better idiot.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    Hmmm...

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width