One Line Wonder

BlitzMax Forums/BlitzMax Programming/One Line Wonder

dw817(Posted 2016) [#1]
I know there was a conversation earlier about, I think it was Casaber, who wanted to be able to write a routine in a single line of code.

The problem was the IF statement. If you used a ; (semicolon) then it would also include that code if the prior IF's statement was true. So it required you to enter a carriage-return if you wanted to have code after the IF statement.

Years ago I wrote silly little 1-liners for Call A.P.P.L.E., an Apple Computer magazine - and I ran into the same problem. So what I used (and am using here) is a LOGICAL statement to make an effect on a variable - a sneaky way of getting around the IF statement.

There are 3 of them in here actually.

If you go out of bounds on the bouncing ball, the acceleration reverses itself. Both on X & and Y.

The third, if you hit the [ESCAPE] key it will exit.

And no, I'm not using the GOTO statement or even a REPEAT or WEND, but a FOR/NEXT statement where the index is reset to zero until you hit the ESCAPE key, then it is one, and the target is reached, and the program ends.
ax=5;ay=4;Graphics 640,480;For i=0 To 1;Cls;DrawOval x,y,40,40;x:+ax;y:+ay;ax:*1-2*(x<0 Or x>600);ay:*1-2*(y<0 Or y>440);Flip;i=KeyDown(27);Next
Hope This Helps !


Dan(Posted 2016) [#2]
Heh, nice tricks , should be written down ^^


TomToad(Posted 2016) [#3]
Replaced the For Next loop with a While Wend loop and saved 5 characters. Did a few more changes and saved a total of 13 characters
u=5;v=4;Graphics 640,480;While Not KeyDown(27);Cls;DrawOval x,y,40,40;x:+u;y:+v;u:*1-2*(x<0Or x>600);v:*1-2*(y<0Or y>440);Flip;Wend



Cocopino(Posted 2016) [#4]
"should be written down ^^"

... or forgotten ASAP ;)


dw817(Posted 2016) [#5]
Good compression, Tom ! I didn't have WHILE/WEND on the apple is why I used a FOR/NEXT. And yes, WHILE/WEND does save a bit of space (and perhaps confusion) :)

Cocopino, could be written down. In any case you see how it is possible to have any number of programs, even full games, in a single line of code.


Dan(Posted 2016) [#6]
@Cocopino:
iv just opened gimp, drawn a circle, grabbed it as brush, and moved the circle arround, with 0 lines of code !!!

i guess we are done with programming ;p

anyway, its always nice to find bits and bytes which can be done, or done differently, than what is normaly used.
Back in the days, there were challenges in the magazines, which asked people to write programs,for example, as 10 liners.

And there are very nice examples of games, which can be done with it.


and to add even better compression, you can leave the Graphic statement out, so blitz will open its default 400x300 window.

ok, this is blitzmax not blitz3d


Cocopino(Posted 2016) [#7]
Well... I was asked once to maintain/make changes to a program "structured" similar like that. Because of the limitations of the language, it was VBDOS I believe, having a limit of 64000 characters per program, the first lines started with something like:

dim aa,ab,ac ... mx,my,mz 'all integers
dim na$,nb$,nc$ ...... nx$,ny$,nz$ 'all strings


Then the fun really started: look up the meaning of every variable in the "documentation", e.g. "aa = product article ID".
Later I found out I could use the "chain" command to circumvent the character limit somewhat but it still was absolutely horrible.

So when I saw this code I was afraid of getting nightmares again ;)