Posts: 660
Threads: 142
Joined: Apr 2022
Reputation:
58
playing with the idea of a "10-line" program I cane up with a "5-lines" engine that builds a game map of a cave and handles W,A,S,D navigation. I posted this to a facebook group and another person was able to fir in treasure placement and grabbing. What can you fit in there?
Code: (Select All)
Screen _NewImage(80, 45, 0): _FullScreen: _ControlChr Off: _PrintString (1, 1), String$(3280, 219): dx = 40: dy = 20: Randomize Timer: s = Int(200 + Rnd * (100 * (1 + Rnd * 4))): For d = 1 To s: m$ = m$ + Chr$(49 + Int(Rnd * 4)): Next d: px = 40: py = 20: stand = 0
Do: dx = 40: dy = 20: Color 7: _PrintString (1, 1), String$(3280, 219): For c = 1 To Len(m$): _PrintString (dx, dy), ".": Select Case Mid$(m$, c, 1): Case "1": dy = dy - 1: Case "2": dx = dx + 1: Case "3": dy = dy + 1: Case "4": dx = dx - 1: End Select: Select Case dy: Case 40, 1: dy = 20: dx = 40: End Select: Select Case dx: Case 80, 1: dy = 20: dx = 40: End Select: Next c: Color 15: _PrintString (px, py), Chr$(2)
Do: _Limit 40: kk$ = InKey$: Loop Until kk$ <> "": kk$ = UCase$(kk$)
pxm = 0: pym = 0: Select Case kk$: Case "W": pym = -1: Case "S": pym = 1: Case "D": pxm = 1: Case "A": pxm = -1: End Select: Select Case Screen(pym + py, pxm + px, 0): Case 219: pxm = 0: pym = 0: End Select: px = px + pxm: py = py + pym
Loop Until kk$ = Chr$(27)
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
I don't think that'd qualify as a 5-line program. It's a whole bunch of lines just all squeezed together into one.
May as well spread those all out onto separate lines for ease of reading, save the file as "Foo.BI", and then write a 1-line program of:
'$INCLUDE:'Foo.BI'
If all of those statements count as one line, why wouldn't my simple $INCLUDE do the same?
Posts: 660
Threads: 142
Joined: Apr 2022
Reputation:
58
Thus the use of "quotes". There are no external file calls as that would be cheating indeed. I tried to keep each line down to a theme. Initialization, drawing, input/navigation. I tried to keep it to 256 characters or less per line but I believe I failed that.
Posts: 3,974
Threads: 177
Joined: Apr 2022
Reputation:
219
Yeah putting everything under : 's is not great feat, as Steve says it makes code very unreadable so what's the point? Pete and I even played around with actual line counts after removing colons with a QB64pe coded tool.
Now do all that in 5 lines without colons! That would be worthy to be called a "5 line Proggie"! ;-))
Still not bad, 69 lines without colons is "proggie" material.
b = b + ...
Posts: 98
Threads: 1
Joined: Jun 2023
Reputation:
3
Under GWBasic this was called spaghetti code. I was told that some people had contests to write a working code like this with no line breaks (return or enter) and whoever had the largest working code won the contest. GWBasic has a bad reputation because of this. But it saved what little expensive memory there was at the time. - GareBear
Posts: 200
Threads: 5
Joined: Apr 2022
Reputation:
22
That's not spaghetti. It's just densely packed.
This sort of program is not about readability. It's about density and packing as much as you can into the smallest space.
I used to write one-liners in interpreted Basic just for grins. It can be a fun personal challenge and learning experience to see what the programming environment lets you get away with.
Thumbs up.
Posts: 135
Threads: 25
Joined: Apr 2022
Reputation:
39
I also tend to compress. I prefer to use a more complicated mathematical operation, I avoid IF, CASE if possible. I often see IFs based on the same topic being used by many people, one below the other, in many rows. why? I think that by stretching the code vertically, it will be even less transparent.
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
One liner contests have been around for a while. I entered a one liner contest for the TRS-80 Color Computer in a magazine back in the mid 80's (Hot Coco, The Rainbow, 80 Micro, I can't remember now). My entry won third place. In 255 characters I somehow managed to create an archery game where you shot 5 arrows at a circular target. You used a joystick to try and center the crosshairs on the target and the longer you waited to fire the worse the drift got. I wish I still had that code or could at least remember which magazine and year/month it was in.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 200
Threads: 5
Joined: Apr 2022
Reputation:
22
02-07-2024, 05:25 PM
(This post was last modified: 02-07-2024, 10:16 PM by JRace.)
It's a form of Code Golf, but instead of creating the smallest possible program you are packing as much functionality into the fewest lines (or one line) as possible.
@MasterGy:
It was a real eye opener when I realized that comparison operations could be used mathematically.
For instance using
y = y- (y<0) : y = y+ (y>5)
to bound y to the range of 0..5. Much faster and more compact than IF..THENs, and more readable once you know the trick. I was so proud of myself as I started finding ways to improve my existing programs.
Re LOC: I'll often combine two or more closely related operations on a single line, because I find it to be much more readable when I can take in more of the program on a single editor screen or printed page. Also, grouping the code up physically helps to clarify the program logic in my mind.
A dusty old one-liner:
Model III Basic had a line length limit of 255 characters. One trick I "discovered" was to type in as many characters as possible, hit <ENTER> and let the interpreter tokenize the line, then edit the line to add even more.
This one-line drawing program for the Model III weighs in at 220 characters, so no problem there. Note that no target line number is given to the GOTOs. 0 was the default target if none was given. Also, the THEN keyword was not strictly required:
The two POKEs make this a self-modifying program. The SET...PEEK...RESET sequence which provides a flashing cursor in "erase" mode becomes RESET...PEEK...SET in "draw" mode. (The PEEK reads the Model III's keyboard matrix to get the status of the arrow keys, spacebar (draw/erase toggle), and CLEAR key (clears screen).
(I later wrote a full drawing program in Basic, using every trick up my sleeve to get near machine-language speed wherever possible. Although I once had just about every line of that program memorized, looking at it all these years later even I cannot figure out what my own program is doing at some points. That program in dense!)
Posts: 135
Threads: 25
Joined: Apr 2022
Reputation:
39
02-07-2024, 08:27 PM
(This post was last modified: 02-07-2024, 09:58 PM by MasterGy.)
James came up with a very interesting stitch. I like the small size of the program, and I especially like the maze generation. I also like the idea or challenge. It triggers one's imagination. I'm sorry that instead of solutions, there was more negative criticism about compressed coding.
I tried to expand as James gave up the lesson. The code is sometimes not practical anymore, but due to the small number of characters, I tried to compress everything as much as I could. This is hard to understand even for me, but it works and serves the purpose. It has neither IF nor SELECT. In fact, the entire program could be arranged in a single line.
I hope sufficiently scandalous! Thanks James for the idea!
(control wasd or numeric keys. survive and collect the golds)
Code: (Select All)
en = 3: gold = 12: c0 = 219: c1 = 46: c2 = 36: c3 = 35: c4 = 2 'en=enemies gold=golds 'c0,c1,c2,c3,c4 wall,street,gold,enemi,iam - ascii code of characters in order
Randomize Timer: r(0) = 80: r(1) = 40: For t = 0 To 1: r(2 + t) = Int(r(t) / 2): p(t) = r(2 + t): d(t) = p(t): Next t: Screen _NewImage(r(0), r(1), 0): _FullScreen: _ControlChr Off: Color 6: For d = 1 To 800
m$ = m$ + Chr$(48 + Int(Rnd * 4)): Next d: _PrintString (1, 1), String$(r(0) * r(1), Chr$(c0)): For c = 1 To Len(m$): _PrintString (d(0), d(1)), Chr$(c1): t = Val(Mid$(m$, c, 1)): f = 0: For a = 0 To 1: b = a * 2: l = t = b + 1
d(a) = (t = b Or l) * (l * 2 + 1) + d(a): f = f Or d(a) = 1 Or d(a) = r(a) - 1: Next a: For t = 0 To 1: d(t) = r(t + 2) * -f + d(t) * (-f Xor 1): Next t: Next c: Color 14: For c = 1 To gold: Do: For t = 0 To 1
g(t) = Int((r(t) - 10) * Rnd) + 5: Next t: Loop While Screen(g(1), g(0), 0) <> c1 Or g(0) = r(2): _PrintString (g(0), g(1)), Chr$(c2): Next c: For c = 0 To en - 1: Do: For t = 0 To 1: en(c, t) = Int((r(t) - 10) * Rnd) + 5
Next t: Loop Until Screen(en(c, 1), en(c, 0), 0) = c1 And Screen(en(c, 1), en(c, 0) + 1, 0) = c0: Next c: Do: mygold = mygold - (Screen(p(1), p(0), 0) = c2): Color 15: _PrintString (p(0), p(1)), Chr$(c4): m = 0
k$ = LCase$(InKey$): m(0) = p(0): m(1) = p(1): For t = 0 To 3: e = ((t And 1) * 2 - 1) * (Mid$("dasw", t + 1, 1) = k$ Or Mid$("6428", t + 1, 1) = k$): q = Sgn(t And 2): m(q) = e + m(q): m = m Or e: Next t
c = Abs(m And Screen(m(1), m(0), 0) <> c0): Color 6: _PrintString (p(0), p(1)), Mid$(Chr$(c1), 1, c): For t = 0 To 1: p(t) = m(t) * c + p(t) * (c Xor 1): Next t: For c = 0 To en - 1: die = die Or Screen(en(c, 1), en(c, 0), 0) = c4
t = en(c, 2) + 2: Do: t = t - 1: For w = 0 To 1: b = (t + 16 + w) Mod 4: d(w) = en(c, w) - ((b And 1) = 0) * ((b = 0) * 2 + 1): Next w: u = Screen(d(1), d(0), 0): Loop Until u <> c0: Color 6
_PrintString (en(c, 0), en(c, 1)), Mid$(Chr$(c1), 1, en(c, 3)): en(c, 0) = d(0): en(c, 1) = d(1): en(c, 2) = t: en(c, 3) = -(u = c1): Color 2: _PrintString (d(0), d(1)), Mid$(Chr$(c3), 1, en(c, 3))
die = die Or (u = c4) Or (m(0) = en(c, 0) And m(1) = en(c, 1)): Next c: _Limit 12: Loop Until k$ = Chr$(27) Or gold = mygold Or die: Color 15: Print Left$("you dead", -8 * die); Left$("congratulation!", -15 * (gold = mygold))
|