Author Topic: BASIC programming tips  (Read 49826 times)

0 Members and 1 Guest are viewing this topic.

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #100 on: 02:38, 07 April 18 »
I'm not really sure if it's necessary to use REMAIN with AFTER, though I don't know the circumstances for which what you're using AFTER in, I suppose if AFTER was in a Loop you may need REMAIN in that situation. What I found with AFTER, is a single jump to a subroutine. I used one in my short game Find Red to clear the mosaic grid, which seemed to work fine in that situation, I don't recall having any REMAIN statements in that program, though all that is, is a single jump to clear the mosaic AFTER that time had elapsed.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Online skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #101 on: 14:43, 07 April 18 »
Yes, generally REMAIN is not necessary to be used with AFTER. It is necessary when, depending on player choices in the game, you want to stop the execution of AFTER subroutine.
Imagine for example that (for your game) you give 30 seconds to the player to make his choice. For this, you can use AFTER, and after 30 seconds print <you lose!>. In this case, AFTER has to be disabled by REMAIN in most cases, in which the player has made the choice before 30 seconds. If it is not, then <You lose> will appear in the next turn

Another problem i had has to do with where the program goes after calling the AFTER subroutine, as it doesn't seem to go in the same line every time (as i wanted to send the execution back to the next turn). Using GOTO in the subroutine wasn't the best choice, because the end of the subroutine by RETURN will not be recognized. What i did is setting a variable to 1 in the AFTER subroutine, and then using IF/THEN commands in 2-3 lines in different places in the list i send the program back to the next turn. Hopefully, this works

Nice game in only 10 lines! Well done! Needing careful observing!

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #102 on: 07:07, 08 April 18 »
Yes, generally REMAIN is not necessary to be used with AFTER. It is necessary when, depending on player choices in the game, you want to stop the execution of AFTER subroutine.
Imagine for example that (for your game) you give 30 seconds to the player to make his choice. For this, you can use AFTER, and after 30 seconds print <you lose!>. In this case, AFTER has to be disabled by REMAIN in most cases, in which the player has made the choice before 30 seconds. If it is not, then <You lose> will appear in the next turn


I see, so in your situation you need to cancel the countdown and reinitialize for the next choice. That will need a REMAIN.

Quote
Another problem i had has to do with where the program goes after calling the AFTER subroutine, as it doesn't seem to go in the same line every time (as i wanted to send the execution back to the next turn). Using GOTO in the subroutine wasn't the best choice, because the end of the subroutine by RETURN will not be recognized. What i did is setting a variable to 1 in the AFTER subroutine, and then using IF/THEN commands in 2-3 lines in different places in the list i send the program back to the next turn. Hopefully, this works


Not quite sure I follow, the AFTER statement is a condition where by when it's set, that will initiate after the time has elapsed from within anywhere in the program, it's possible that if your program is doing something else with variables which might interfere with variables within your programs AFTER subroutine, that may cause problems. Normally what happens in that case is if you have areas of code you need protected from the Event Jump from AFTER, you can use DI to Disable Interrupts & EI to Enable Interrupts.

Quote
Nice game in only 10 lines! Well done! Needing careful observing!


Thanks! :)
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Online skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #103 on: 16:54, 08 April 18 »

Not quite sure I follow, the AFTER statement is a condition where by when it's set, that will initiate after the time has elapsed from within anywhere in the program, it's possible that if your program is doing something else with variables which might interfere with variables within your programs AFTER subroutine, that may cause problems. Normally what happens in that case is if you have areas of code you need protected from the Event Jump from AFTER, you can use DI to Disable Interrupts & EI to Enable Interrupts.
I looked again to what i had done, as it couldn't remember exactly. We are talking about the situation when player loses, because he has not made the choice and we want to go to the next turn.
So, in that case, AFTER subroutine has been runned. I create a new unused variable in the subroutine, and define A=1. So, in case the subroutine has been runned, A=1.
Then, in the area of inputing, just after X$=inkey$ and before the input choices i entered a line <if A=1 then GOTO> (go the next turn). Of course, A is set to 0 in the start of each turn.
In that way, the game does not wait for inputing of the player choice, and goes to next turn. If not done, after displaying <you lose>, the game will continue the same turn, not the next turn

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 320
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 344
  • Likes Given: 146
Re: BASIC programming tips
« Reply #104 on: 06:23, 09 April 18 »
Short Basic Screensaver with frames. :)

10 MODE 1:INK 0,0:BORDER 0:DEFINT a-z:a$=CHR$(150):b$=CHR$(156):c$=CHR$(147):d$=CHR$(153):e$=CHR$(154):f$=CHR$(149):g$=CHR$(9)
20 INK RND*2+1,RND*26:PEN RND*15 MOD 3+1:x=RND*29+1:y=RND*22+1:s=RND*10+2:w=RND*6+2:LOCATE x,y:PRINT a$STRING$(s,e$)b$:y=y+1:FOR a=1 TO w:LOCATE x,y:PRINT f$STRING$(s,g$)f$:y=y+1:NEXT:LOCATE x,y:PRINT c$STRING$(s,e$)d$;:GOTO 20
« Last Edit: 20:21, 10 April 18 by ZbyniuR »
In STARS, TREK is better than WARS.

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #105 on: 11:45, 09 April 18 »
I looked again to what i had done, as it couldn't remember exactly. We are talking about the situation when player loses, because he has not made the choice and we want to go to the next turn.
So, in that case, AFTER subroutine has been runned. I create a new unused variable in the subroutine, and define A=1. So, in case the subroutine has been runned, A=1.
Then, in the area of inputing, just after X$=inkey$ and before the input choices i entered a line <if A=1 then GOTO> (go the next turn). Of course, A is set to 0 in the start of each turn.
In that way, the game does not wait for inputing of the player choice, and goes to next turn. If not done, after displaying <you lose>, the game will continue the same turn, not the next turn


I'm not quite sure I follow, but it sounds like the A=1 needs to be in the <You Win> subroutine to take it to the next turn, with the <You Lose> routine to clear variables and restart (RUN) the program.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Online skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #106 on: 19:51, 09 April 18 »
No, maybe my english is not so good and i could't make it clear.
imagine that we expect for an answer from the player.
10 A=0:REM DEFINING VARIABLES, GIVING CHOICES-TURN STARTS
20 AFTER 250 GOSUB 100
30 A$=inkeys
35 if A=1 then goto 10
40 if a$=""A""  then..... etc
50 goto 30
100 PRINT '' YOU LOSE'': A=1:RETURN

In the case of a later answer, LINE 100 IS CALLED. Player loses, but program is still between 30 and 50, and waiting for a player choice.
So, we added line 35 to send the programme to the next turn

Many thx for your interest AMSDOS!

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.051
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 677
  • Likes Given: 294
Re: BASIC programming tips
« Reply #107 on: 20:07, 09 April 18 »
Wouldn't it be better to have a WHILE A=0 on line 10, and WEND on line 35, instead of the GOTO?
EgoTrip&#39;s Stuff
EgoTrip's Stuff

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #108 on: 03:10, 10 April 18 »
No, maybe my english is not so good and i could't make it clear.


No I think I'm misreading. Thought you had a problem, when in fact you've found a solution.



* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Online skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #109 on: 04:58, 10 April 18 »
Maybe WHILE can work too. Due to my very limited knowledge, i use mostly very few commands
Yes, AMSDOS, hopefully is working!

Online skylas

  • CPC664
  • ***
  • Posts: 84
  • Country: gr
    • AMSTRAD SAKIS
  • Liked: 114
  • Likes Given: 106
Re: BASIC programming tips
« Reply #110 on: 04:36, 16 April 18 »
One more simple question!
When we have an INPUT question of an a$ variable, is there a way to specify that characters must not be over a specific number? (for example i want a$ variable to be not more than 15 characters).
I found LEN, but i wonder if there is a quicker way

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #111 on: 10:07, 16 April 18 »
One more simple question!
When we have an INPUT question of an a$ variable, is there a way to specify that characters must not be over a specific number? (for example i want a$ variable to be not more than 15 characters).
I found LEN, but i wonder if there is a quicker way


As far as I know, INPUT only accepts up to the 255 characters, you could have in your program (15 characters max) and then do some MID$, with something like this (I hope):


Code: [Select]
1000 if LEN(a$)>15 THEN r=15 ELSE r=LEN(a$)
1010 w$=MID$(a$,1,r)


A couple of years ago, I wrote this silly little Condensed Text Name selector using Cursor keys to force the amount of areas to let the user select. It works, but it's a bit cumbersome to use.


EDIT: LEFT$ could also be used, which may have a speed advantage to MID$, so:


Code: [Select]
1010 w$=LEFT$(a$,r)

will do the same job as MID$ in this case.
« Last Edit: 12:32, 16 April 18 by AMSDOS »
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3


Offline Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1306
Re: BASIC programming tips
« Reply #113 on: 20:54, 29 April 18 »
*** Prime numbers from 2 to 997 in ~5 seconds in one Basic line ***

Code: [Select]
10 MODE 2:ZONE 6:DEFINT a-y:z=TIME:p=3:m=1000:c=SQR(m+1):DIM s(m+1):PRINT 2,:WHILE p<c:PRINT p,:FOR i=p+p*2 TO m STEP p*2:s(i)=1:NEXT:p=p+2:WHILE s(p):p=p+2:WEND:WEND:WHILE p<m:PRINT p,:p=p+2:WHILE s(p):p=p+2:WEND:WEND:PRINT:PRINT(TIME-z)/300;"seconds"
The table is reusable. For example, adding this line:

Code: [Select]
20 CALL &BB18:MODE 2:z=TIME:p=3:PRINT 2,:WHILE p<m:PRINT p,:p=p+2:WHILE s(p):p=p+2:WEND:WEND:PRINT:PRINT(TIME-z)/300;"seconds"
it takes only 4 seconds to show the same prime numbers.
« Last Edit: 21:00, 29 April 18 by Urusergi »

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: BASIC programming tips
« Reply #114 on: 09:28, 13 October 18 »
I think this question has been asked before, but:

Is there anyway to (from BASIC) fetch the character or character-code currently on the screen on a certain position?

This would require a character-memory-map of some kind, which I don't think the CPC has?

Something like CHARCODE(x,y)

Or what about using PEEKS?

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.318
  • Country: au
    • index.php?action=treasury
  • Liked: 978
  • Likes Given: 1127
Re: BASIC programming tips
« Reply #115 on: 10:19, 13 October 18 »
I think this question has been asked before, but:

Is there anyway to (from BASIC) fetch the character or character-code currently on the screen on a certain position?

This would require a character-memory-map of some kind, which I don't think the CPC has?

Something like CHARCODE(x,y)

Or what about using PEEKS?

Not sure if it's what you're after, but COPYCHR$ might be worth a look:
http://www.cpcwiki.eu/index.php/Locomotive_BASIC#COPYCHR.24_.28st.29

I don't think it works on a 464, but I seem to remember a discussion on cpcwiki a few years ago about a 464 equivalent.
(I could be remembering incorrectly though).
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: BASIC programming tips
« Reply #116 on: 10:31, 13 October 18 »
Not sure if it's what you're after, but COPYCHR$ might be worth a look:
http://www.cpcwiki.eu/index.php/Locomotive_BASIC#COPYCHR.24_.28st.29

I don't think it works on a 464, but I seem to remember a discussion on cpcwiki a few years ago about a 464 equivalent.
(I could be remembering incorrectly though).
Interesting. Will take a closer look. But it would be nice with something for the 464, since that's the one I'm using for this. (Small enough to be on the same desk as my PC).
EDIT: found this in another thread:
10 FOR a=1 TO 17:POKE 39999+a,VAL("&"+MID$("DD7E00CDB4BBF5CD60BB320000F1C3B4BB",a*2-1,2)):NEXT
20 LOCATE x,y:CALL 40000,0:PRINT CHR$(PEEK(0)) '  this works as COPYCHR$ in any CPC

I used to found this in some magazine. :)

And remember about TEST command, could be usefull to detect pixel color. :)
http://www.cpcwiki.eu/forum/programming/basic-efficient-collision-detection-routine/
« Last Edit: 11:00, 13 October 18 by mr_lou »

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #117 on: 14:19, 13 October 18 »
I think this question has been asked before, but:

Is there anyway to (from BASIC) fetch the character or character-code currently on the screen on a certain position?

This would require a character-memory-map of some kind, which I don't think the CPC has?

Something like CHARCODE(x,y)

Or what about using PEEKS?


Would have to know more about how that character onscreen got there. If it's a program placing and removing characters onscreen, using an array is faster than copychr$(#0) or it's TXT RD CHAR (#BB60) counterpart.
If you were gathering information from a Disk Catalog (#BC9B) through, the firmware can be told where to place that information with the DE register being the address to where the buffer is to go.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline mr_lou

  • 6128 Plus
  • ******
  • Posts: 3.036
  • Country: dk
    • index.php?action=treasury
    • 8-bit Memoirs - a Blu-ray diskmag-like eBook about the 8-bit era
  • Liked: 1201
  • Likes Given: 2423
Re: BASIC programming tips
« Reply #118 on: 15:30, 13 October 18 »
Would have to know more about how that character onscreen got there. If it's a program placing and removing characters onscreen, using an array is faster than copychr$(#0) or it's TXT RD CHAR (#BB60) counterpart.
If you were gathering information from a Disk Catalog (#BC9B) through, the firmware can be told where to place that information with the DE register being the address to where the buffer is to go.

Well the idea is to draw levels for a game, using normal PRINT commands. But these levels only contain very little data - as in most of it would be empty spaces. Therefor I figure an array (or map) would only take up unnecessary much space. Hence why I thought it would be better to be able to simply check what's on the screen.

Of course, speed matters too, so..... dunno.

EDIT:Did a short test. First 1000 checks using the PEEK method above. Took 26,44 seconds. Then 1000 outputs from an array. Took 23,92 seconds. So it's not like a huge difference. 2½ seconds for 1000 cycles. So that's 2½ ms slower per cycle to use PEEK rather than an array. I can live with that.
« Last Edit: 15:52, 13 October 18 by mr_lou »

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #119 on: 23:20, 13 October 18 »
Sorry I got the impression that it was 2.5 seconds (not microseconds), which sounds like a lot if the game is running through a cycle of checks.

I converted this game earlier this year, which had a lot of copychr$(#0) in it. The game is also MODE 1, so I setup a Memory array with a Function pointing to it, which gave me the advantage of having a byte sized array from BASIC, even on a MODE 1 screen I was able to have some redefined characters and the Array situated at &A000. I'm unsure what happened to the original program, though may have somewhere, actually I think the program maybe within this attached file with this message.
« Last Edit: 23:23, 13 October 18 by AMSDOS »
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #120 on: 08:49, 09 July 19 »
I made this following programme to test the various timings between storing values to an array or poking to memory, along with the Loops they should be using (Single or Nested). I guess my finds won't come as much of a Surprise to other BASIC coders, though I just thought it would be worthwhile including it in this thread anyway.


The results I found was:
* Test 1 which stores a value to a 2D array through a Nested Loop is the Slowest, taking a little over 2 seconds to complete.
* Test 2 which Pokes to value to memory through a Nested Loop took just under 2 seconds.
* Test 3 was the Fastest, which Pokes values to Memory using a Single Loop takes a little over 1 Second.
* Test 4 which stores a value to a single array through a Single Loop took 1 and a half Seconds.


From Lines 460-530 I've setup a Nested Loop and used a calculation similar to the POKE from Nested Loop demo to calculate the correct position within the Single Array in Test 4 just to show how to correctly calculate a position based from 2 variables (x & y), y is multiplied by 20 which represents the Length of the array.


Code: [Select]

100 MODE 2:CALL &BC02
110 DIM g$(20,25)
120 DEFINT a-z
130 ' Test 1 - Store value into 2D Array through Nested Loop
140 t!=TIME
150 FOR y=1 TO 25
160  FOR x=1 TO 20
170    g$(x,y)=CHR$(32)
180  NEXT x
190 NEXT y
200 t1!=TIME:PRINT"Time 1 (2D Array in Nested Loop) =";(t1!-t!)/300
210 CLEAR:DEFINT a-z
220 ' Test 2 - Poke from Nested Loop
230 MEMORY &9FFF
240 t!=TIME
250 FOR y=1 TO 25
260   FOR x=1 TO 20
270     POKE &9FEB+x+(y*20),32
280   NEXT x
290 NEXT y
300 t1!=TIME:PRINT"Time 2 (Memory Poke in Nested Loop) =";(t1!-t!)/300
310 ' Test 3 - Single Loop
320 DEFINT a-z
330 t!=TIME
340 FOR a=1 TO 500
350   POKE &A1FF+a,32
360 NEXT a
370 t1!=TIME:PRINT"Time 3 (Memory Poke in Single Loop) =";(t1!-t!)/300
380 CLEAR:DEFINT a-z:DIM g$(500)
390 ' Test 4 - Store value into Single Array through Single Loop
400 t!=TIME
410 FOR a=1 TO 500
420   g$(a)=CHR$(225)
430 NEXT a
440 t1!=TIME:PRINT"Time 4 (Singular Array in Single Loop) =";(t1!-t!)/300
450 CALL &BB18
460 MODE 0:b=-20
470 FOR y=1 TO 25
480   FOR x=1 TO 20
490     PEN 15:LOCATE x,y:PRINT g$(b+x+(y*20));
500   NEXT x
510 NEXT y
520 CALL &BB18
530 PEN 1:PRINT CHR$(224):CLEAR:END
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #121 on: 08:04, 10 July 19 »
With my programme yesterday producing some fast results from a Single Loop, I wanted to have a look at the problem of calculating the correct X & Y positions from a Single Loop. I'm unsure how performance compares, but I was able to eventually work it out based on the 20x25 Text Screen and Screenshot as Proof.


Code: [Select]

100 MODE 0:BORDER 0:INK 14,15:INK 15,9
110 DEFINT a-z
120 w=20
130 FOR a=1 TO 500
140   y=(a+19)\w
150   x=(a+w)-(y*w)
160   PEN INT(RND*15)+1:LOCATE x,y:PRINT CHR$(248);
170 NEXT a
180 CALL &BB18:PEN 1:END


To make it work I had to calculate 'y' first which adds 19 (width-1) to the position of the loop and then divide by 'w' which is the width. Once I know where 'y' is positioned, I can add the position of the loop with the width and y is multiplied by the width and subtracted to get the value of x, so for example if a=21, 20 is added to get 41, y at that point equals 2, so 2*20 = 40 and 41-40 = 1.
The formulas will slow things down, I'm unsure if it's past the point where Nested Loops would have the edge over this approach, though at least it's solved now.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.318
  • Country: au
    • index.php?action=treasury
  • Liked: 978
  • Likes Given: 1127
Re: BASIC programming tips
« Reply #122 on: 08:54, 10 July 19 »
That's a very interesting test.
I couldn't help but try it with nested loops, and then compare the running times.

Code: [Select]
130 for y=1 to 25
140 for x=1 to 20
150 PEN INT(RND*15)+1:LOCATE x,y:PRINT CHR$(248);
160 next x
170 next y

The following times are SCIENTIFICALLY CALCULATED with a stopwatch app on my phone.  :laugh:

single loop: 10.6 seconds
nested loops: 7.8 seconds
« Last Edit: 08:58, 10 July 19 by ervin »
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.796
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 1033
  • Likes Given: 1747
Re: BASIC programming tips
« Reply #123 on: 12:05, 10 July 19 »
That's a very interesting test.
I couldn't help but try it with nested loops, and then compare the running times.

The following times are SCIENTIFICALLY CALCULATED with a stopwatch app on my phone.  :laugh:

single loop: 10.6 seconds
nested loops: 7.8 seconds


Cool, I wondered if those calculations were going to a bearing on the timings. As it turns out that test I performed is faster compared to if I had made x & y functions, which overall is nearly a whole second slower again.



* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Urusergi

  • CPC6128
  • ****
  • Posts: 189
  • Country: es
  • Liked: 276
  • Likes Given: 1306
Re: BASIC programming tips
« Reply #124 on: 00:47, 11 July 19 »
Another approach, with an improvement of ~1 second:
Code: [Select]
...
130 FOR a=0 TO 499
140 y=(a+20)\20
150 x=1+a MOD 20
...