Author Topic: Why is this bit of code borking on the LOCATE statement?  (Read 4912 times)

0 Members and 1 Guest are viewing this topic.

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Why is this bit of code borking on the LOCATE statement?
« on: January 19, 2022, 10:14:09 pm »
G'day,

I imagine it is something simple for which I'll be kicking myself in the rear.

I felt like doing some mindless coding tonight with another BASIC implementation, and wanted to see if that code would work as-is in QB64.  The compiler is not happy with line 32 (The "LOCATE" statement in "DisplayBombs".  What silly mistake am I making here?

Code: QB64: [Select]
  1. VariableDeclarations:
  2.         type tAlphaBomb
  3.                 sChar as string
  4.                 iY as integer
  5.                 iX as integer
  6.         end type
  7.         dim AlphaBomb(1) as tAlphaBomb
  8.         DangerBomb% = 1
  9.         NewBomb% = 1
  10.         StopGame% = 0
  11. '
  12. MainProgram:   
  13.         gosub NewBomb
  14.         do
  15.                 cls
  16.                 gosub DisplayBombs
  17.                 sound 55, 3
  18.                 sleep 0.75
  19.                 gosub PrepNextBombPositions
  20.                 gosub CheckKeyboard
  21.         loop while NOT StopGame%
  22.         end
  23. '
  24. NewBomb:
  25.         AlphaBomb(NewBomb%).sChar = chr$(int(rnd * (90 - 65 + 1) ) + 65)
  26.         AlphaBomb(NewBomb%).iY = 1
  27.         AlphaBomb(NewBomb%).iX = int(rnd * (80) + 1)
  28.         return
  29. '
  30. DisplayBombs:
  31.         for i = 1 to 24
  32.                 locate AlphaBomb(i).iY, AlphaBomb(i).iX
  33.                 print AlphaBomb(i).sChar
  34.         next i
  35.         return
  36. '
  37. PrepNextBombPositions:
  38.         for i = 1 to 24
  39.                 AlphaBomb(i).iY += 1
  40.                 if AlphaBomb(i).sChar <> "" and AlphaBomb(i).iY = 25 then
  41.                         AlphaBomb(i).sChar = ""
  42.                         locate AlphaBomb(i).iY-1, AlphaBomb(i).iX
  43.                         print "*"
  44.                         sound 37,3
  45.                         sleep 9/18
  46.                         DangerBomb% = i mod 24 + 1
  47.                 end if
  48.         next i
  49.         NewBomb% = NewBomb% mod 24 + 1
  50.         Gosub NewBomb
  51.         return
  52. '
  53. CheckKeyboard:
  54.         NextKey$ = inkey$
  55.         while NextKey$ <> ""
  56.                 if NextKey$ = chr$(27) then
  57.                         StopGame% = -1
  58.                 else
  59.                         if NextKey$ = AlphaBomb(DangerBomb%).sChar then
  60.                            locate AlphaBomb(DangerBomb%).iY-1, AlphaBomb(DangerBomb%).iX
  61.                                 print "#"
  62.                                 sound 290, 1
  63.                                 sleep 3/18
  64.                                 AlphaBomb(DangerBomb%).sChar = ""
  65.                                 DangerBomb% = DangerBomb% mod 24 + 1
  66.                         end if
  67.                 end if
  68.                 NextKey$ = inkey$
  69.         wend
  70.         return

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #1 on: January 19, 2022, 10:26:47 pm »
What BASIC are you trying to compile under?  It doesn't look like QB64 code to me...


AlphaBomb(i).iY += 1


We don't have a += shortcut.  Do we??
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #2 on: January 19, 2022, 10:33:43 pm »
Arg, bad copy paste.

Code: QB64: [Select]
  1. AlphaBomb(i).iY += 1
is something that works with the other BASIC implementation, which I did change to
Code: QB64: [Select]
  1. AlphaBomb(i).iY = AlphaBomb(i).iY + 1
in QB64.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #3 on: January 19, 2022, 10:38:27 pm »
My guess would be a LOCATE 0,0 statement, which is out of bounds of the screen.

Relevant code is here:

NewBomb% = 1

GoSub NewBomb

NewBomb:
AlphaBomb(NewBomb%).sChar = Chr$(Int(Rnd * (90 - 65 + 1)) + 65)
AlphaBomb(NewBomb%).iY = 1
AlphaBomb(NewBomb%).iX = Int(Rnd * (80) + 1)
Return

Do
    Cls
    GoSub DisplayBombs

DisplayBombs:
For i = 1 To 24
    Locate AlphaBomb(i).iY, AlphaBomb(i).iX
    Print AlphaBomb(i).sChar
Next i
Return


Now, as you can see from the flow here, you set the first bomb to be at location 1, rnd * 80...   You never set a location for the rest of the bombs, so they all default to 0,0.   When you get to the DisplayBombs routine, you try and display all 24 bombs on the screen, but only the very first one has a location for it.

You're locating off the screen coordinates.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #4 on: January 19, 2022, 10:43:07 pm »
 Oh man, good eyeballs.

I was not thinking.  The other implementation is forgiving of that kind of thing.  I just need to slap a wee check in there.

Thanks !

Aside, the working version in the other implementation: https://basicanywheremachine.neocities.org/Alphabet%20Attacks.html

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #5 on: January 19, 2022, 11:10:29 pm »
That code in the OP, I had to do some tweaking and now have a version that works A-1 with QB64.

(It took me a bit to figure out that the SLEEP statement in QB64 does not like fractions of seconds, so a switch to _Delay fixed that right up.)

Code: QB64: [Select]
  1. VariableDeclarations:
  2.         type tAlphaBomb
  3.                 sChar as string
  4.                 iY as integer
  5.                 iX as integer
  6.         end type
  7.         dim AlphaBomb(1) as tAlphaBomb
  8.         DangerBomb% = 1
  9.         NewBomb% = 1
  10.         StopGame% = 0
  11. '
  12. MainProgram:   
  13.         gosub NewBomb
  14.         do
  15.                 cls
  16.                 gosub DisplayBombs
  17.                 sound 55, 3
  18.                 sleep 0.75
  19.                 gosub PrepNextBombPositions
  20.                 gosub CheckKeyboard
  21.         loop while NOT StopGame%
  22.         end
  23. '
  24. NewBomb:
  25.         AlphaBomb(NewBomb%).sChar = chr$(int(rnd * (90 - 65 + 1) ) + 65)
  26.         AlphaBomb(NewBomb%).iY = 1
  27.         AlphaBomb(NewBomb%).iX = int(rnd * (80) + 1)
  28.         return
  29. '
  30. DisplayBombs:
  31.         for i = 1 to 24
  32.                 locate AlphaBomb(i).iY, AlphaBomb(i).iX
  33.                 print AlphaBomb(i).sChar
  34.         next i
  35.         return
  36. '
  37. PrepNextBombPositions:
  38.         for i = 1 to 24
  39.                 AlphaBomb(i).iY += 1
  40.                 if AlphaBomb(i).sChar <> "" and AlphaBomb(i).iY = 25 then
  41.                         AlphaBomb(i).sChar = ""
  42.                         locate AlphaBomb(i).iY-1, AlphaBomb(i).iX
  43.                         print "*"
  44.                         sound 37,3
  45.                         sleep 9/18
  46.                         DangerBomb% = i mod 24 + 1
  47.                 end if
  48.         next i
  49.         NewBomb% = NewBomb% mod 24 + 1
  50.         Gosub NewBomb
  51.         return
  52. '
  53. CheckKeyboard:
  54.         NextKey$ = inkey$
  55.         while NextKey$ <> ""
  56.                 if NextKey$ = chr$(27) then
  57.                         StopGame% = -1
  58.                 else
  59.                         if NextKey$ = AlphaBomb(DangerBomb%).sChar then
  60.                            locate AlphaBomb(DangerBomb%).iY-1, AlphaBomb(DangerBomb%).iX
  61.                                 print "#"
  62.                                 sound 290, 1
  63.                                 sleep 3/18
  64.                                 AlphaBomb(DangerBomb%).sChar = ""
  65.                                 DangerBomb% = DangerBomb% mod 24 + 1
  66.                         end if
  67.                 end if
  68.                 NextKey$ = inkey$
  69.         wend
  70.         return

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #6 on: January 19, 2022, 11:14:42 pm »
https://basicanywheremachine.neocities.org/Alphabet%20Attacks.html

@CharlieJV does this Basic have Dynamic arrays? All I see is DIM, no REDIM.

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #7 on: January 19, 2022, 11:26:41 pm »
G'day,

Yup, dynamic arrays.

I'm working on a customized version of wwwbasic in my BASIC Anywhere Machine project.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #8 on: January 19, 2022, 11:38:33 pm »
G'day,

Yup, dynamic arrays.

I'm working on a customized version of wwwbasic in my BASIC Anywhere Machine project.

Hey, I just checked it out, won't run any of the QB64 code I tried but did ok typing in a Game of Hi Lo, Guess the number. Interesting to run programs On-Line. Where did my save of the Game go?

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #9 on: January 20, 2022, 12:01:00 am »
Yeah,  wwwbasic has been dormant for a few years.

I'm in the process of ironing out kinks that get on my nerves, then slowly figure out how to get more QB64 compatiblity in there.  (List of changes I've made to wwwbasic at the bottom of this post.)

Really, BASIC Anywhere Machine is for the BASIC hobby programming, a portable run-anywhere BASIC for learning fundamental programming skills (loops, for example) and, if I can get there, a portable side-tool to crank out and test small bits of code for export and inclusion in big projects with robust tools and goals.

So work on a little subroutine or function in BASIC Anywhere Machine, export the code, and slap that in your QB64 project.  That might make more sense than bringing full QB64 programs into BASIC Anywhere Machine.

Something like that.  My thoughts/feelings are scattered all over the place.

Let's just say I'm a huge fan of BASIC, immense fan of TiddlyWiki, and QB64 is the twinkle in my eye.

Putting it all together somehow is something the hamsters in the back of my head are still spinning away on.  To look at me, one may think the hamster's dead, yet multiple squeaky wheels are always spinning ...

Enhancements I've made to wwwBASIC so far:

🤕 Fixes to Broken Features
SWAP
✏ Edited Behaviour
SLEEP
VAL
➕ Language Implementation Additions
DATE$
NOW$
TIME$
SOUND
🔩 Architectural Enhancements
Console Focus Before Program Execution

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #10 on: January 20, 2022, 11:53:51 am »
@CharlieJV

Cool, glad you aim for QB64 compatibility, never heard of TiddlyWiki, interesting.
Love to see people connect outside interests o QB64.


Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: Why is this bit of code borking on the LOCATE statement?
« Reply #11 on: January 20, 2022, 12:20:04 pm »
Well (re QB64 compatibility), I am going to get as close as I can on this side of the cuckoo's nest, and within my abilities.

Javascript and I largely do not get along.  The work done by the wwwbasic contributors is something to behold.

My work on BASIC Anywhere Machine is a slow-going intertwingled labour o' passion, putting together the BASIC programming TiddlyWiki, a programmer's guide and web site.  Trying to figure out project management, communication, etc.

All while reminiscing via reading old BASIC Programming books on the internet archive, getting inspiration related to writing a BASIC Anywhere Machine User Guide and some kind of resource for folk who want to learn programming (BASIC is great for that kind of thing.)

Good times !