Author Topic: QB64 10-Liner programs or games  (Read 13654 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #15 on: March 21, 2022, 04:55:43 am »
@Pete Wow nice 3 liner!

@STxAxTIC now I know why money doesn't grow on trees, too busy riding down that river. Nice spontaneous game!

@bplus 
I know that you have a lot of imagination maybe you can show us some complete program less than 10 lines or 2 or 3 lines

happy to see you always active
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #16 on: March 21, 2022, 05:02:55 am »
Cool... "10 Liners"...  Back in the mid 80's I ran an Amstrad CPC464... 4mhz cpu with only 64K of ram...  Learning to program efficiently was the thing back then. In the magazines that I purchased were "10 liners". It was amazing, back then, that so much could be done with so little coding. Mind you, the CPC, was capable of having long lines of code... But still... amazing...

magazines came out in Italy  with the name commodore compiuter club
which urged readers to post language code basic with just one line of code

which are small complete programs
obviously they can't be beautiful programs or games but what you can do with just one line
it takes creativity
then many people compete as listed above in the post there is a special site of people who spidano with the most beautiful program and those who use less line of code with lots of rules
the site in the post mentioned above I think they are German programmers
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #17 on: March 21, 2022, 10:00:43 am »

When I was playing around making a small plasma code to add to programs, a year or so ago, I tried to make a small version in 10 lines or less, just for fun, and had saved this one.

Any old timers here remember Rels' 9 liner challenge at qbasicnews about 20 years ago?  Some really cool snippets were posted there...

- Dav

Code: QB64: [Select]
  1. 1 t = TIMER
  2. FOR x = 0 TO _WIDTH STEP 2
  3.     FOR y = 0 TO _HEIGHT STEP 3
  4.         r = SIN(1.1 * t) * (_WIDTH / 2) - y + (_HEIGHT / 2)
  5.         LINE (x, y)-(x, y), _RGB(r, r - y, -r), BF
  6.     NEXT
  7.     IF x = _WIDTH THEN _DISPLAY ELSE t = t + .005
  8.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #18 on: March 21, 2022, 10:03:26 am »
I don't like lines extended over to China horizontally because of use of colons but do like these little ditties, with single statements per line. Neither do I like too many single letter variables unless they are obvious like x, y, mx, my, s$ for temp string of text$. I like nice readable short code.

I have an idea for a game library to make writing 10 liners, more or less, easier.
Why waste code on the usual sh... stuff?

For instance the game library could have:
Code: QB64: [Select]
  1. Const Sh = 800, Sw = 600
  2. DIM SHARED AS LONG Mx, My, k$, done, Lim
  3. Screen _NewImage(Sw, Sh, 32)
  4.    Cls
  5.    Mx = _MouseX: My = _MouseY
  6.    k$ = Inkey$
  7.    PlayGameCodeHere  ' read and execute your 10 liner here
  8.    _Limit Lim ' let coder set Lim
  9. Loop Until done

Actually that would be the main code and then $Include the 10 liner game.

« Last Edit: March 21, 2022, 10:09:05 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #19 on: March 21, 2022, 11:36:24 am »
Not sure how to fix this error bplus.

  [ You are not allowed to view this attachment ]  

Offline gaslouk

  • Newbie
  • Posts: 29
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #20 on: March 21, 2022, 11:54:41 am »
Code: QB64: [Select]
  1. CONST sh = 800, sw = 600
  2. DIM SHARED AS LONG mx, my, done, lim
  3. SCREEN _NEWIMAGE(sw, sh, 32)
  4.     CLS
  5.     mx = _MOUSEX: my = _MOUSEY
  6.     k$ = INKEY$
  7.     _DISPLAY
  8.     '    _LIMIT lim
  9. LOOP UNTIL done
  10.  
  11.  
  12.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #21 on: March 21, 2022, 12:03:42 pm »
I don't like lines extended over to China horizontally because of use of colons but do like these little ditties, with single statements per line. Neither do I like too many single letter variables unless they are obvious like x, y, mx, my, s$ for temp string of text$. I like nice readable short code.

I have an idea for a game library to make writing 10 liners, more or less, easier.
Why waste code on the usual sh... stuff?

For instance the game library could have:
Code: QB64: [Select]
  1. Const Sh = 800, Sw = 600
  2. DIM SHARED AS LONG Mx, My, k$, done, Lim
  3. Screen _NewImage(Sw, Sh, 32)
  4.    Cls
  5.    Mx = _MouseX: My = _MouseY
  6.    k$ = Inkey$
  7.    PlayGameCodeHere  ' read and execute your 10 liner here
  8.    _Limit Lim ' let coder set Lim
  9. Loop Until done

Actually that would be the main code and then $Include the 10 liner game.

@bplus You have a lot of tricks up your sleeve, but I'll bet DIMming k$ as "LONG" is not one of them. :D

I completely agree about the line challenge and non-use of colons. Every colon is another line, in my opinion. Oh course, characters could be counted, but that's not entirely fair either, as certain keywords are longer than others.

I use the same approach to most of my larger programs. Make a central hub, and run sub-routines from it. This method allowed me to always know the program flow would continuously be cycled through that hub. Back in the old days, it provided a way to time the program, and evoke a screen saver. Other advantages existed, too.

@Dav Speaking of screen savers, in the paragraph above, I would have loved to have the plasma one you posted! Very cool!

@Kiara87 I get it, more of a discussion concerning optimization of BASIC code, rather than the use of BASIC libraries to develop OOP. Most of what I see of a practical aspect of this is the use of creative algorithms, mostly math formulas, like Dav and Bill posted in their 10-liners. The downside of "optimization" when it comes to limiting lines of code, ironically, results in obfuscation, much the same too many lines of convoluted code produce. Use of GOTO statements, for example, to cut down on a loop line code. I consider that bad coding practice, especially to introduce it, in a positive way, to a beginner. I consider BASIC best for coders who like to build things from the foundation up. All I ever heard from C communities is "You need a library for that." That was their response to me trying to write a keyboard function in C. I eventually accomplished it, because it was possible with C/C++ statements, but I was not too happy that such a large community completely lost track of how to build a program from the foundation up.

Pete

PS @Dimster That was just a typo on Mark's part. He was just giving a skeleton, almost pseudo-code, example of how to set up a 10-line hub to run a program. Sorry for the late reply, you posted while I was writing.
« Last Edit: March 21, 2022, 12:09:58 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #22 on: March 21, 2022, 12:12:25 pm »
Sorry all, I posted in code box without testing, should of left it out until I tested. I had errands that needed attending to this AM. It was just a sketch of an idea. But if you guys couldn't figure that out, I don't know...

Turns out the insert part can just be a sub (that might call more subs and functions).
Don't need library or what ever, just a sub. The sucky / decision part is screen 0 or graphics?

Hope to post a proper example this afternoon.

@Dimster as Pete caught error was K$ in Long Dim
@gaslouk lim was to be set by coder of the Sub you left out, oh you caught the k$ first! :)
@Pete yeah you got the idea of which I was speculating I think.

PS I crashed my computer trying STx's game because it was meant for screen 0.
« Last Edit: March 21, 2022, 12:39:26 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #23 on: March 21, 2022, 01:03:14 pm »
Some more thoughts, I see a need for a setup before the main game in loop.

Screen 0 does eliminate a need for a number of lines.

How to handle (with skeleton as Pete called it) something like Daves screen saver / graphics?

Need more practice with short programs before coming up with good generic skeleton. Unfortunately I need them in English not Italian. Can't blame Kiara for wanting Italian!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #24 on: March 21, 2022, 01:23:50 pm »
Yeah what I am finding is these things are really 100 lines or less:
https://www.vitoco.cl/atari/10liner/MNPUZZLE/

  [ You are not allowed to view this attachment ]  

Quote

The full and expanded BASIC listing is:

GRAPHICS 18
? #6;"     M*N PUZZLE"
Initializes the screen and prints the game title. Uses standard graphics mode 2 (big text) without normal text window at the bottom, and default colours.
R=120
DIM A$(R),B$(R),D(3),J(15)
S=DPEEK(88)+40
M=4
N=M
A$="."
A$(R)=A$
B$=A$
Initializes constants and variables:
R is the the length of the screen area where the maze will be placed.
S is the memory location of the playfield.
M is the width and N is the height, starting at 4x4.
A$ has a copy of the playfield containing the solved puzzle, and B$ has a copy of the current playfield during the game. Both string variables need to be of the same maximum size R for the string comparison to work.
D() and J() arrays store movement information as follows...
FOR I=0 TO 3
  READ D,J
  D(I)=D
  J(J)=D
NEXT I
DATA -20,13,1,11
DATA -1,7
DATA 20,14
Set up screen deltas for random and joystick movements for all four directions. Graphics mode 2 uses 20 bytes per line, then you have to substract 20 bytes to find the screen position of the piece in the upper side of the current screen position, and add 1 to find the one at the right.
D(0-3) array stores the four deltas for random shufle
J(0-15) store the same four deltas for UP, RIGHT, LEFT and DOWN joystick positions and zero ("dont' move") for the other positions (default values in TurboBASIC XL).
Data instructions were placed at the end of other lines to save space.
WHILE 1
End-less loop...
  REPEAT
Begin of the routine to select the size of the puzzle, from 2x2 up to 6x6.
    MOVE S, S+1, 199
Clears playfield area.
    P = S + 70 - 20*(N DIV 2) - M DIV 2
Finds the upper left position in screen area of the puzzle for the current size of it.
    FOR I=0 TO N*M-2
      X=P+I+(I DIV M)*(20-M)
      POKE X, (33-42*(I>25)+I*65) MOD 256
    NEXT I
Draws the puzzle, except the last piece.
X is the screen position of the current piece.
The pieces starts alphabetically from A to Z. In sizes that require more than 26 pieces, numbers 1 to 9 are included, giving 35 pieces for the largest 6x6 mode (including one empty space).
At the same time, it asigns a colour for every piece, obtaining a different pattern based on current puzzle size.
    PAUSE 9
Just a pause before accepting a change of the size. If there is no pause, it's possible to change the size two times in just one move.
    REPEAT
      J=15-STICK(0)
      K=1-STRIG(0)
    UNTIL J+K
Waits until joystick is moved or button is pressed. Bits are being negated to be more useful: 0=released, 1=pressed for button and one bit per direction for joystick.
    IF K=0
      M=M+(J&8>0)*(M<6)-(J&4>0)*(M>2)
      N=N+(J&2>0)*(N<6)-(J&1>0)*(N>2)
    ENDIF
Based on the joystick movement, changes the width and/or height of the puzzle. Diagonal movement is allowed!
M increases by one only if the right bit of the joystick is set and the current size is less than the maximum allowed size, and decreased only if the left bit is set and current size is more than the minimum allowed size. The same for N in the other axis... BTW, all this happens only if the button was not pressed.
  UNTIL K
Quits the game setup routine when the button is pressed.
  MOVE S,ADR(A$),R
Saves the screen data for the solved puzzle in A$.
  X=X+1
  Z=X+1
  C=N*M*8
Sets the current position as the empty cell in X, the previous position in Z (fake for the first time), and the number of moves required to shuffle the puzzle in C, based on the selected size of the puzzle.
  WHILE C
Begin of the shuffling routine.
    REPEAT
      D=RAND(4)
      Y=X+D(D)
      Q=PEEK(Y)
    UNTIL Q AND Y<>Z
Randomly select a direction to find a piece to be moved to the empty space. Q is the piece, Y is it's position on screen. It shouldn't be the same piece from the previous move.
    SOUND 0,20+D*3,8,8
Plays a sound. The tone is based on the selected direction.
    POKE Y,0
    POKE X,Q
Move the selected piece to the empty position.
    Z=X
    X=Y
    C=C-1
Sets the position of the last moved piece, the new new position of the empty space and decreases the pending move's counter.
  WEND
  SOUND
End of the shufling routine. Needs to turn off shuffling sound.
  REPEAT
Begin of the gameplay routine.
    REPEAT
      J=STICK(0)
      Y=X+J(J)
      Q=PEEK(Y)
    UNTIL Q
Select the piece to move based on the joystick position. Note that joystick movement is inverted: UP moves the empty space down, i.e. moves up the piece under it. There are no pieces neither in the empty position (joystick in released position) nor outside the puzzle area.
    POKE 77,0
Clears the attract mode register. This prevents the change of screen colours while playing, or restore the screen colours after a long pause while playing.
    SOUND 0,20+J,8,8
Plays a sound, the tone is based on the joystick position.
    POKE Y,0
    POKE X,Q
    X=Y
Move the selected piece to the empty space and set in X the new position of the empty space.
    PAUSE 8
    SOUND
Stop the sound after a small pause.
    C=C+1
    POSITION 9+(C<10),9
    ? #6;C
Increase and print the count of movements.
    WHILE STICK(0)<15
    WEND
Waits until joystick is released. No continuous moves in this game.
    MOVE S,ADR(B$),R
Copies the current playfield to B$.
  UNTIL B$=A$
Compares the current payfield against the solved one. If they match, the game ends.
  POSITION 8,11
  ? #6; "DONE!";
  FOR I=0 TO 9
    SOUND 0,60-5*I,12,8
    PAUSE 4
  NEXT I
  SOUND
Congratulations! You solved the puzzle. Bells and whistles...
  WHILE STRIG(0)
  WEND
Wait for the button to be pressed to start again.
WEND

I say make it 100 lines with no colons and get something as enjoyable to read as to play!

That would be way more educational and inspiring for all levels of hobbiests.
« Last Edit: March 21, 2022, 01:30:35 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #25 on: March 21, 2022, 01:54:56 pm »
@bplus 
I know that you have a lot of imagination maybe you can show us some complete program less than 10 lines or 2 or 3 lines

happy to see you always active

Sure but I am just double parking with colons this program:
https://qb64forum.alephc.xyz/index.php?topic=4733.msg141466#msg141466

Code: QB64: [Select]
  1. _Title "May Flowers 8-Liner": Screen _NewImage(800, 600, 32): sq = (600 - 100) ^ 4: For i = 0 To 100: Line (0, i)-(800, i), _RGB32(0, 0, 155 + i): Next
  2. For i = 100 To 600: Line (0, i)-(800, i), _RGB32(0, 210 - .25 * i, 0): Next
  3. For i = 1 To 125: x = Rnd * 800: s = Rnd * 8: y = 100 - s: cc = Rnd * 75: col~& = _RGB32(cc, cc, cc): Line (x, y)-Step(s, s), col~&, BF: Next
  4. Do: cc = Int(Rnd * 12) * 5 + 200: col~& = _RGB32(cc, cc, 0): petals = (20 * Rnd) + 5: xo = Rnd * 800: yo = 600 - (Rnd * sq) ^ .25: z = (yo / 100) ^ 2
  5.     For theta = 0 To 2 * 3.14159 Step 0.001: r = Cos(petals * theta)
  6.         For fil = 0 To z: x = r * Cos(theta) * fil + xo: y = r * Sin(theta) * fil + yo: PSet (x, y), col~&: Next fil
  7.     Next theta: _Limit 20
  8.  

BTW the code lines just fit across my IDE screen with max 152 characters.

  [ You are not allowed to view this attachment ]  

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #26 on: March 21, 2022, 02:05:02 pm »
I think we need a good working definition of what a "line of code" is
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #27 on: March 21, 2022, 02:24:33 pm »
This is one liner in another Basic without colons!!!
SmallBASIC Code:
Code: [Select]
for x=0 to 262144 do pset x%512,x\512,rgb(x%256,(x%512 xor x\512)%256,x\512%256)

Code: QB64: [Select]
  1. _Title "XOR Magic translated from SmallBASIC 'one-liner' by bplus"
  2. Screen _NewImage(512, 512, 32): For x = 0 To 262144: PSet (x Mod 512, x \ 512), _RGB(x Mod 256, ((x Mod 512) Xor (x \ 512)) Mod 256, x \ 512 Mod 256): Next: Sleep
  3.  

@STxAxTIC a line of code does not have colons in it.

Like this:
Code: QB64: [Select]
  1. _Title "XOR Magic translated from SmallBASIC 'one-liner' by bplus"
  2. Screen _NewImage(512, 512, 32)
  3. For x = 0 To 262144
  4.     PSet (x Mod 512, x \ 512), _RGB(x Mod 256, ((x Mod 512) Xor (x \ 512)) Mod 256, x \ 512 Mod 256)
  5.  

Here is something closer but using 2 dang colons again:
Code: QB64: [Select]
  1. _Title "XOR Magic translated from SmallBASIC 'one-liner' by bplus"
  2. Screen _NewImage(512, 512, 32)
  3. 1 PSet (x Mod 512, x \ 512), _RGB(x Mod 256, ((x Mod 512) Xor (x \ 512)) Mod 256, x \ 512 Mod 256): x = x + 1: If x < 262144 Then GoTo 1 Else Sleep
  4.  
« Last Edit: March 21, 2022, 02:41:34 pm by bplus »

Marked as best answer by Kiara87 on March 21, 2022, 01:52:46 pm

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #28 on: March 21, 2022, 02:47:47 pm »
(I'm sure equivalent thing has been done before)

Catch The aei Vowels!

Literally 10 lines, but not 10 statements.  Well, could knock out a few lines by getting rid of instructions ...

BASIC Anywhere Machine code that works A-1 as is with QB64:


Code: QB64: [Select]
  1. another_one:
  2. cls : locate 1,1 : print "Catch the aei vowels!" : print "Press the vowel's key when you see it." : print "Don't make a mistake !"
  3. i = int(rnd * 9) + 65
  4. locate 5,1 : print "Score: " + str$(score%) : locate 7,1 : print "..."
  5. _delay 1 : while inkey$ <> "" : wend : key_pressed$ = ""
  6. locate 7,1 : print chr$(i) + "    " : _delay 1
  7. key_pressed$ = ucase$(inkey$)
  8. if (instr("AEI",chr$(i)) AND key_pressed$ = chr$(i)) then score% = score% + 1 : sound 2000,1 : goto another_one
  9. if (instr("AEI",chr$(i)) = 0) AND key_pressed$ = "" then goto another_one
  10. sound 40, 1 : score% = score% - 2 : goto another_one
  11.  

« Last Edit: March 21, 2022, 03:27:07 pm by CharlieJV »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QB64 10-Liner programs or games
« Reply #29 on: March 21, 2022, 03:44:36 pm »
Yeah so I test Skeleton idea with STxAxTICS's 10 liner game and saved a whole 3 lines by making it 28 lines long total with Skeleton code.

You only need write the lines between
Sub MainSetupCode ' try STxAxTIC's code
   and
End Sub

and between

Sub MainLoopCode ' try STxAxTIC's code
  and
End Sub

Code: QB64: [Select]
  1. _Title "Test Skeleton Screen 0" ' b+ 2022-03-21
  2. ' screen 0 is (8 * 80 = 640) X (16 * 25 = 400) dont need constants for that
  3. Dim Shared As Long Mx, My, ClsTF, done, Lim
  4.  
  5. MainSetupCode ' call setup
  6.     If ClsTF Then Cls
  7.     Mx = _MouseX: My = _MouseY
  8.     k$ = InKey$
  9.     MainLoopCode ' read and execute your 10 liner here
  10.     _Display
  11.     If Lim Then _Limit Lim ' let coder set Lim
  12. Loop Until done
  13.  
  14. Sub MainSetupCode ' try STxAxTIC's code
  15.     ClsTF = 0 ' don't clear screen
  16.     Lim = 30 ' set _Limit 30
  17.  
  18. Sub MainLoopCode ' try STxAxTIC's code
  19.     Static i, t
  20.     i = i + 1
  21.     Print Space$(Int(80 / 2 - 10) * (1 + .25 * Cos(i / 20) + .25 * Sin(i / 30))); "|"; String$(7 * (1 + Abs(Cos(i / 10))), "$"); "|"
  22.     If (Screen(1 + My, 1 + Mx, 0) = 36) Then t = t + 1 Else t = t - 5 'b+ gets mean!
  23.     _Title "$:" + Str$(t)
  24.  

Should we try another one? Of course, this is b+ ;-))
« Last Edit: March 21, 2022, 04:32:53 pm by bplus »