20 Line Programming Challenge

Anything QL Software or Programming Related.
User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

20 Line Programming Challenge

Post by Mr_Navigator »

Welcome to the 20 Line Programming Challenge (LPC) :!:

The purpose of this post is fairly straightforward, for QL'ers it is to challenge, have a play with or just to explore ways in which to achieve the best possible program you can write in S*Basic in 20 lines of code or less. I got the idea after seeing similar sections in other forums, so why not the QL and its derivatives.

Here are the rules

1) A line is defined as a line of text, terminated with a linefeed (Ta DJ)
2) You can stack multiple commands per line up to a maximum of 20 commands per line
3) Any external media is not allowed
4) State in the subject or body of the post the type of S*Basic used to help others
5) Lines beginning with 'REMark' do not count towards your 20 line allocation, however they will help others understand your code so please use them
6) Lines containing only ':' do not count towards your 20 line allocation but do allow clarity

The subject of your 20 LPC can be anything from a utility, a game, a demo or just an exploration of unusual, interesting or clever coding

Make sure that you use the Code button when posting and include any screen dumps if relevant

Occasionally, interesting entries could be considered for inclusion in the QUANTA magazine :geek:

That's it and thanks for your support

Just to get things rolling, how about a simple cursor controlled Snake game?
Last edited by Mr_Navigator on Sun Jul 10, 2011 8:35 pm, edited 1 time in total.


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: 20 Line Programming Challenge

Post by dilwyn »

Mr_Navigator wrote:Welcome to the 20 Line Programming Challenge (LPC) :!:

The purpose of this post is fairly straightforward, for QL'ers it is to challenge, have a play with or just to explore ways in which to achieve the best possible program you can write in S*Basic in 20 lines of code or less. I got the idea after seeing similar sections in other forums, so why not the QL and its derivatives.

Here are the rules

1) A line is defined as a line of text, terminated with a carriage return

Just to get things rolling, how about a simple cursor controlled Snake game?
Hmm, this would be quite difficult. QL lines do not end with a carriage return but rather with a linefeed.
That said, as long as you allow me that liberty, I might try to take you up on this challenge after I've finished my articles for Quanta.

Dilwyn


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: 20 Line Programming Challenge

Post by dilwyn »

Mr_Navigator wrote:Welcome to the 20 Line Programming Challenge (LPC) :!:

Just to get things rolling, how about a simple cursor controlled Snake game?
OK, here's a first little effort to get this going - a simple snake game in 14 lines, written in about 15 minutes. Plenty of lines (6) left to improve it!

Code: Select all

100 REMark QL Snake in less than 20 lines
110 MODE 8 : WINDOW #0,484,252,18,2 : papcol% = RND(0 TO 7) : PAPER #0,papcol% : INK #0,7*(papcol%<4) : BORDER #0,1,255 : CLS #0
120 CSIZE #0,2,0 : DIM screen$(24,40) : FOR y = 0 TO 24 :screen$(y) = FILL$(' ',40)
130 sx% = RND(10 TO 30) : sy% = RND(5 TO 20) : screen$(sy%,sx%) = 'O' : AT #0,sy%,sx% : PRINT #0,'O';
140 speed% = RND(0 TO 10) : direc% = RND(1 TO 4) : snakelen% = 0
150 REPeat program
160   key = CODE(INKEY$(speed%)) : IF key = 27 THEN EXIT loop : REMark Esc
170   SELect ON key=192,200,208,216 : direc% = (key = 208)+(2*(key=200))+(3*(key=216))+(4*(key=192))
180   sx% = sx%-(direc%=4)+(direc%=2) : sy% = sy%-(direc%=1)+(direc%=3)
190   IF sx% < 0 OR sx% > 39 OR sy% < 0 OR sy% > 24 THEN EXIT program : REMark oops, off-screen
200   IF screen$(sy%,sx%+1) <> ' ' THEN EXIT program : REMark oops, run into snake body
210   screen$(sy%,sx%+1) = 'O' : AT #0,sy%,sx% : PRINT #0,'O'; : snakelen% = snakelen%+1
220 END REPeat program
230 BEEP 5000,50 : AT #0,0,0 : PRINT #0,'Oops, game over. Snake length was ';snakelen%
Dilwyn


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: 20 Line Programming Challenge

Post by dilwyn »

dilwyn wrote:
Mr_Navigator wrote:Welcome to the 20 Line Programming Challenge (LPC) :!:

Just to get things rolling, how about a simple cursor controlled Snake game?
OK, here's a first little effort to get this going - a simple snake game in 14 lines, written in about 15 minutes. Plenty of lines (6) left to improve it!

130 sx% = RND(10 TO 30) : sy% = RND(5 TO 20) : screen$(sy%,sx%) = 'O' : AT #0,sy%,sx% : PRINT #0,'O';
Oops, bug in line 130 :oops: , where "screen$(sy%,sx%)="O" should be screen$(sy%,sx%+1)="O"

Code: Select all

100 REMark QL Snake in less than 20 lines
110 MODE 8 : WINDOW #0,484,252,18,2 : papcol% = RND(0 TO 7) : PAPER #0,papcol% : INK #0,7*(papcol%<4) : BORDER #0,1,255 : CLS #0
120 CSIZE #0,2,0 : DIM screen$(24,40) : FOR y = 0 TO 24 :screen$(y) = FILL$(' ',40)
130 sx% = RND(10 TO 30) : sy% = RND(5 TO 20) : screen$(sy%,sx%+1) = 'O' : AT #0,sy%,sx% : PRINT #0,'O';
140 speed% = RND(5 TO 10) : direc% = RND(1 TO 4) : snakelen% = 0
150 REPeat program
160   key = CODE(INKEY$(speed%)) : IF key = 27 THEN EXIT loop : REMark Esc
170   SELect ON key=192,200,208,216 : direc% = (key = 208)+(2*(key=200))+(3*(key=216))+(4*(key=192))
180   sx% = sx%-(direc%=4)+(direc%=2) : sy% = sy%-(direc%=1)+(direc%=3)
190   IF sx% < 0 OR sx% > 39 OR sy% < 0 OR sy% > 24 THEN EXIT program : REMark oops, off-screen
200   IF screen$(sy%,sx%+1) <> ' ' THEN EXIT program : REMark oops, run into snake body
210   screen$(sy%,sx%+1) = 'O' : AT #0,sy%,sx% : PRINT #0,'O'; : snakelen% = snakelen%+1
220 END REPeat program
230 BEEP 5000,50 : AT #0,0,0 : PRINT #0,'Oops, game over. Snake length was ';snakelen%


RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: 20 Line Programming Challenge

Post by RWAP »

Good work - though any chance you could upload a file (maybe Snake_bas) which could be loaded directly into QPC2 or Q-emuLator with all the correct line endings to make it easy for people please :)


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: 20 Line Programming Challenge

Post by dilwyn »

RWAP wrote:Good work - though any chance you could upload a file (maybe Snake_bas) which could be loaded directly into QPC2 or Q-emuLator with all the correct line endings to make it easy for people please :)
snake_bas.zip
OK, that proved to be a little bit harder than I thought as the Forum rejects most meaningful extensions, e.g. it disallowed snake_bas, snake.bas, snake_bas.dat, snake.txt and so on. So I gave up and zipped it, which it seems to like. If anyone has problems with it, email me privately and I'll send it by email to you.
Mr Navigator did suggest the use fo the CODE command and I suppose that should copy and paste OK.
Would be a good idea to define exactly what we can and can't attach in postings here for future reference, I guess.

Dilwyn


RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: 20 Line Programming Challenge

Post by RWAP »

The code block is to be used for easy copy and pasting.

The problem is how you get that into a QL emulator (or woe-betide, a real QL!).

If all a user has is Notepad, then that program adds the wrong code (CR,LF) to end each line of text (and you also get spacing before each line).

A proper editor, such as TotalEdit (which is free in its basic version), can however handle the proper line endings if set for Unix code endings (LF).

Actually is there not a command to grab everything on the windows clipboard into QPC2?


User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

Re: 20 Line Programming Challenge

Post by Mr_Navigator »

Well as I started it I should really put my two pence worth, here is my 20 line submission

It is a fun mini application for generating 'Epic Movie' titles by combining two words randomly and adding a sub tagline depending on the users choice. Obviously the more data statements the more interesting and funny it can become. The listing is below, and a _BAS file would be attached as suggested by RWAP, however every combination I tried resulted in it not being allowed except if saved with the .zip extension, even though it is not a compressed file, it is a text file. So that is what is attached, a text file.

Code: Select all

100 MODE 4:WINDOW 512,256,0,0:PAPER 2:INK 0:CLS:d=DATE:RANDOMISE d
110 PAPER 4:CSIZE 2,1:CURSOR 1,11:PRINT " E P I C   M o v i e   T i t l e r ":INK 7
120 OVER 1:CURSOR 0,10:PRINT " E P I C   M o v i e   T i t l e r ":CSIZE 1,0:PRINT
130 PRINT "  Original(O), Prequel(P), Sequel(Q)"
140 REPeat loop:I$=INKEY$:IF I$>="o" AND I$<="q" THEN EXIT loop:END FOR loop
150 :
160 IF I$="o":RESTORE 310
170 IF I$="p":RESTORE 370
180 IF I$="q":RESTORE 340
190  a=1+INT(RND*10):             FOR f=1 TO a:READ aa$:END FOR f
200  b=1+INT(RND*10):RESTORE 270: FOR f=1 TO b:READ bb$:END FOR f
210  c=1+INT(RND*10):RESTORE 280: FOR f=1 TO c:READ cc$:END FOR f
220 :
230 CSIZE 2,0:CURSOR 11,101:PAPER 7:INK 0:OVER 0
240 PRINT " ";bb$;" ";cc$;" :";:CSIZE 1,0:PRINT aa$;" ":INK 4:OVER 1:CURSOR 10,100:CSIZE 2,0
250 PRINT " ";bb$;" ";cc$;" :":INK 0:CSIZE 0,0:PRINT:PRINT "       Coming Soon..."
260 :
270 DATA "Sonic","Transporting","Red","Happy","Great","Wet","Family","Great","Super","Sky"
280 DATA "Armada","Weekend","Saturdays","Engagement","Tears","Warrior","Walker","Wars","Shepherd","Diver"
290 :
300 REMark Original
310 DATA "Insurgence","Episode 1","Episode 4","The Awakening","The Movie","","The Motion Picture","An Original Story","Conflict at Moore's Rock","First Blood"
320 :
330 REMark Sequel
340 DATA "the Sequel","II","the Return","Revisited","Second Coming","Second Encounter","Next Generation","Returns","Again","is Back"
350 :
360 REMark Prequel
370 DATA "First Born","The Early Years","The Beginning","Young Guns","Starting Point","1st Generation","The Rise","Dawn","Initial Steps","The Calling"
Attachments
Screen Shot
Screen Shot
Untitled-1.png (6.72 KiB) Viewed 8575 times
EMT_BAS.zip
It's not a zip file
(1.56 KiB) Downloaded 322 times


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: 20 Line Programming Challenge

Post by dilwyn »

Nice one, Mr Navigator :D

Rich mentioned grabbing the Windows clipboard into QPC...use the QPC_SYNCSCRAP command so that anything on Windows clipboard is copied to the Menu Extension Scrap thing, from where you can "paste" it into anything which understands how to handle the Scrap system, such as QD and save it as a basic program from there. Don't worry too much if QD adds a left margin of a few spaces, SBASIC should handle that.

I suppose I should have included a screen of my snake program, so here it is if I can figure out how to get the IMG command to work...

Oh, just realised that points to an image somewhere on the web, so I'll try it as an attachment instead
snake_scr.png


User avatar
vanpeebles
Commissario Pebbli
Posts: 2821
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: 20 Line Programming Challenge

Post by vanpeebles »

Impressive stuff guys and there is me still struggling with my zip adventure front end :oops:


Post Reply