Author Topic: My First InForm Program - Trackword  (Read 6314 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #15 on: August 07, 2018, 06:35:45 AM »
Crikey, Fellippe!  Is it not 06.30 in Brazil?  Richard

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2825
  • Let it go, this too shall pass.
    • QB64.org
Re: My First InForm Program - Trackword
« Reply #16 on: August 07, 2018, 06:41:55 AM »
A bit later, 7:40 now. Code time before the baby's up ♥️.

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #17 on: August 07, 2018, 06:51:00 AM »
So, you're holding down two jobs, you do all this work on QB64, you answer everybody's questions, AND you've a baby?

Incredible!

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2825
  • Let it go, this too shall pass.
    • QB64.org
Re: My First InForm Program - Trackword
« Reply #18 on: August 07, 2018, 07:00:21 AM »

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #19 on: August 07, 2018, 07:08:16 AM »
So now some technical questions.  In the Trackword program, when the solve routine is happening, we've just pressed the "Solve" button, so the program is in the SUB __UI_Click (id AS LONG) subroutine, and stays there until the solve routine is complete.  During that time, the computer doesn't "know about" the Inform part - it's just doing the calculations.  So, are the  TIMER(__UI_EventsTimer) OFF &  TIMER(__UI_RefreshTimer) OFF statements going to do anything?

When I was considering putting in a Progress Bar, I assumed that the solve routine would have to be placed in the SUB __UI_BeforeUpdateDisplay subroutine with a 30 times / second (default) _DISPLAY update.  But as this program hasn't left the SUB __UI_Click (id AS LONG) subroutine, it shouldn't be affected by any InForm stuff?  You can tell that I haven't got a clue as to how machine-level code (or any other level come to that) works.

Richard

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2825
  • Let it go, this too shall pass.
    • QB64.org
Re: My First InForm Program - Trackword
« Reply #20 on: August 07, 2018, 07:28:25 AM »
TIMERs are QB64's way of multithreading if you will, which means that even while the calculations are going on in SUB __UI_Click, which was fired by the __UI_EventsTimer, the interface keeps on being updated because __UI_RefreshTimer keeps being fired at ~30fps (or as otherwise set by the SetFrameRate method).

What I'd do if I wanted a ProgressBar control to be updated while the calculations take place would be something like this (pseudo code):

    DIM SHARED progressValue AS LONG
    DIM SHARED isCalculating AS _BYTE

    'in sub Click:
    if id = solveButton then
        isCalculating = True
        maxStep = whateverRichardSaysItIs
        control(ProgressBar).max = maxStep
        for progressValue = 1 to maxStep
             'do your thing
        next
        isCalculating = False
    end if

    'in sub BeforeUpdateDisplay:
    if isCalculating then
        control(ProgressBar).Hidden = False
        control(ProgressBar).value = progressValue
    else
        control(ProgressBar).Hidden = True
    end if

Notice how BeforeUpdateDisplay sub will deal with whatever is going on in sub Click.

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #21 on: August 07, 2018, 10:57:48 AM »
bplus, I've looked into your work.  Thanks very much.  Your algorithm to build the 10,256 combinations is amazing.  I never understood recursion.  In my code, every possible cell-to-cell connection is tried one after the other.

When your algorithm 2 is used, the time taken to do the search is 0.66seconds, compared to my values above (it's slightly dependent upon which word is searched).  So values are similar.  However, that time includes reading the dictionary file into the array, which you have to do.

The time taken to do the seach (after the dictionary is loaded) in your method is 0.05seconds.  That is quite a change indeed.

In my program, I do the dictionary search directly from hard disc file.  I had assumed that the computation time would be taken up in doing the text manipulations to create the 10,256 words.  But it does now seem as if it is the dictionary search which is taking the time.  I'll try your method in my code.

Thanks a lot, Richard

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: My First InForm Program - Trackword
« Reply #22 on: August 07, 2018, 11:16:02 AM »
Hi Richard,

You are most welcome! Thank you for such an interesting post I seemed ready to help.

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #23 on: August 08, 2018, 05:30:42 AM »
bplus, I quickly tried changing my code to using an array (like you do) instead of searching the dictionary.rnd file and then the time taken increased to an extraordinary 13.5 seconds!!  I've also noticed that sometimes my code can be unstable, sometimes not quite getting all the words and/or not getting all the nine-letter words: very odd and I must have checked the code many times.  So, I'll completely overwrite my searching code with yours, and then I'll add your name to the credits for which your actual name as well would be useful.

Richard

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: My First InForm Program - Trackword
« Reply #24 on: August 08, 2018, 08:55:12 AM »
Hi Richard,

Make sure you are loading files once at the beginning of program, a one time only event and you definitely want event checking off while doing that along with using Fellippe's ideas for turning off event checking while doing the calculations.

As far as using my actual name, my first name is Mark, my initials are MGA, and that is as far as I am comfortable revealing personal info at moment.

On 2nd thought, the file / array loading code could be done in main section of bas file, before any event? Again check with Fellippe.

PPS Again I remind you, Steve's methods / example used for his spell checker program are probably way better optimized than what I wrote in a night just to test the concepts. Before the binary search method was employed (the found sub), my word find (brute force) was really slow too!

PS ^2 use "QB64 Team" as my last name!
« Last Edit: August 08, 2018, 09:40:22 AM by bplus »

Offline Qwerkey

  • Forum Resident
  • Posts: 736
Re: My First InForm Program - Trackword
« Reply #25 on: August 08, 2018, 12:03:19 PM »
bplus, I didn't wish to intrude into your privacy.  "QB64 Team" will do quite well for credits.

I'll definitely do all the loading, array addressing as a one-off at program start.  I'm happy to copy exactly what you've done: it works so well.  I'd be grateful if you'd just give your search function another check over.

Code: QB64: [Select]
  1. FUNCTION found% (w$)
  2.     lo& = 0: hi& = UBOUND(words$)
  3.     WHILE hi& - lo& > 1
  4.         test& = (hi& - lo&) / 2 + lo&
  5.         IF w$ = words$(test&) THEN
  6.             found% = 1: EXIT FUNCTION
  7.         ELSEIF w$ > words$(test&) THEN
  8.             lo& = test&
  9.         ELSE
  10.             hi& = test&
  11.         END IF
  12.     WEND
  13.     'if still here then
  14.     found% = 0

My equivalent of this would use  test& = (hi& - lo&) \ 2 + lo& instead of  test& = (hi& - lo&) / 2 + lo&.  And when the RHS is odd, yours gives one integer higher than mine (which rounds down - your rounds up).

I ought to be able to work out which is the correct one, but the (simple) algorithm scrambles my brain.  If you're happy that yours always finds any match as it clearly seems to, I'll go with that.

Thanks a lot, Richard

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: My First InForm Program - Trackword
« Reply #26 on: August 08, 2018, 01:00:35 PM »
Ha! That's funny, I thought x/2 always returns the lower if x is integer type or long type...

Without researching carefully, I would strongly guess it doesn't matter whether it is rounded up or down.

Maybe change
WHILE hi& - lo& > 1

to
WHILE hi& - lo& >= 1

to cover our butts?  Whatever way, the conservative answer is to keep test& rounded down for low side and rounded up for hi side, so that test& never jumps past our target word.

OK I will test these ideas... stay tuned, unless you've done your own tests already.

I am glad you like QB64 Team, they should get credit! :)

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2825
  • Let it go, this too shall pass.
    • QB64.org
Re: My First InForm Program - Trackword
« Reply #27 on: August 08, 2018, 01:03:05 PM »
What are we getting credit for?

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: My First InForm Program - Trackword
« Reply #28 on: August 08, 2018, 01:11:15 PM »
Being helpful, creating a great PL, keeping it updated, fixing bugs adding features and documented and answering questions, the people at this forum helping each other with ideas and moral support...

When I say QB64 Team, I mean all who participate in this process.

Does QB64 Team have another meaning?

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: My First InForm Program - Trackword
« Reply #29 on: August 08, 2018, 02:06:12 PM »
The Binary search method works the same as playing the Hi Lo Game with AI;
Code: QB64: [Select]
  1. _TITLE "Hi Lo Game AI"
  2. ' Hi Lo AI.bas SmallBASIC 0.12.9 (B+=MGA) 2017-08-14
  3.  
  4. DEFLNG A-Z
  5.  
  6. secret = 0
  7. secret = 11 'another limit test
  8.  
  9. 'test intelligence of this program with boner!  comment next code line
  10.  
  11. '  for limit tests comment next line or uncomment for normal range test
  12. secret = INT(RND * 10) + 1 ' < 1 to 10
  13.  
  14.  
  15. Hi = 11: Lo = 0 '<<<<  make these higher and lower than what the secret number can be (just tested secret = 10, oops!)
  16. guessNumber = 0
  17. WHILE Hi - Lo > 1 '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< test both > 1 AND >= 1  Result: Does not work with >= 1
  18.     guess = (Hi - Lo) \ 2 + Lo '<<<<<<<<<<<<<<<<<<<<<<<< test both / AND \       Result: Both work OK
  19.     guessNumber = guessNumber + 1
  20.  
  21.     PRINT guessNumber; " Guess is "; guess; ", ";
  22.     IF guess = secret THEN
  23.         PRINT "Yes! The secret number was "; guess
  24.         PRINT: PRINT "Here comes another round! "
  25.         INPUT "OK, press enter"; dummy$
  26.         PRINT
  27.         Hi = 11: Lo = 0: guessNumber = 0: secret = INT(RND * 10) + 1
  28.     ELSEIF guess > secret THEN
  29.         PRINT "Too high!"
  30.         Hi = guess
  31.     ELSE
  32.         PRINT "Too Low!"
  33.         Lo = guess
  34.     END IF
  35. PRINT "Yikes! Programming error, did not find secret number."
  36. PRINT "WTH? did someone mess with secret number?"
  37.  

What could go wrong is NOT setting Hi, high enough at start or Lo, low enough at start. (And I don't think I did make Hi high enough AND Lo low enough with my test code. Fortunately a word wasn't first or last.)

While Hi  - Lo > 1   keep testing,     NOT While Hi - Lo >= 1, it will never finish!

It did NOT make a difference if / or \ was used.

Sorry, code needed further edits.
« Last Edit: August 08, 2018, 02:11:32 PM by bplus »