1st program in S*BASIC

Anything QL Software or Programming Related.
Post Reply
tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

1st program in S*BASIC

Post by tcat »

Hi,

Having read all Sinclair manuals and Jan Jones book on S*BASIC, I coded this my 1st program, it is nothing special, just wanted to entertain myself with the new knowledge.
I was suprised that I needed CURSOR command to put numbers evenly in the grid, as I could not have managed that with AT command.

Code: Select all

100 REMark puzzle game
110 DIM board(4,4)
120 menu
130 grid : set : layout
140 LET x=1 : y=1
150 LET x0=1 : y0=1
160 invert x,y
170 REPeat main
180 LET k = CODE( INKEY$(-1) )
190 putnum x,y
200 SELect ON k
210   = 113 : STOP
220   = 115 : shuffle : layout
230   = 192 : x=x-1 : IF x<1 : x=1
240   = 200 : x=x+1 : IF x>4 : x=4
250   = 208 : y=y-1 : IF y<1 : y=1
260   = 216 : y=y+1 : IF y>4 : y=4
270   = 32 : IF isnext(x,y) : movto x,y
280 END SELect 
290 invert x,y
300 END REPeat main
310 :
1000 DEFine PROCedure shuffle
1010 FOR n=1 TO 10
1020   LET x = RND(1 TO 4)
1030   LET xx = RND (1 TO 4)
1040   LET y = RND(1 TO 4)
1050   LET yy = RND(1 TO 4)
1060   LET tmp = board(xx,yy)
1070   LET board(xx,yy) = board(x,y)
1080   LET board(x,y) = tmp
1090 END FOR n
1100 END DEFine shuffle
1110 :
1120 DEFine PROCedure set
1130 LET n=0
1140 FOR y=1 TO 4
1150 FOR x=1 TO 4
1160   LET board(x,y) = n
1170   LET n=n+1
1180 END FOR x
1190 END FOR y
1200 END DEFine set
1210 :
1220 DEFine PROCedure grid
1230 PAPER 2: INK 7: CLS
1240 LET x=20
1250 FOR y=40 TO 80 STEP 10
1260   LINE x,y TO x+40,y
1270 END FOR y
1280 LET y=40
1290 FOR x=20 TO 60 STEP 10
1300   LINE x,y TO x,y+40
1310 END FOR x
1320 END DEFine grid
1330 :
1340 DEFine PROCedure layout
1350 FOR y=1 TO 4
1360 FOR x=1 TO 4
1370   putnum x,y
1380   IF board(x,y) = 0 : x0=x : y0=y
1390 END FOR x
1400 END FOR y
1410 END DEFine layout
1420 :
1430 DEFine PROCedure putnum(x,y)
1440 CURSOR x*27+35, y*20+25
1450 INK 7: PAPER 2
1460 LET n = board(x,y)
1470 SELect ON n
1480   = 0 : PRINT '  '
1490   = 1 TO 9 : PRINT n;' '
1500   = 10 TO 16 : PRINT n
1510 END SELect 
1520 END DEFine putnum
1530 :
1540 DEFine PROCedure invert(x,y)
1550 CURSOR x*27+35, y*20+25
1560 INK 2: PAPER 7
1570 LET n = board(x,y)
1580 SELect ON n
1590   = 0 : PRINT '  '
1600   = 1 TO 9 : PRINT n;' '
1610   = 10 TO 16 : PRINT n
1620 END SELect 
1630 END DEFine invert
1640 :
1650 DEFine FuNction isnext(x,y)
1660 LET dx = ABS(x-x0)
1670 LET dy = ABS(y-y0)
1680 IF dx=1 AND dy=0 : RETurn 1
1690 IF dx=0 AND dy=1 : RETurn 1
1700 RETurn 0
1710 END DEFine isnext
1720 :
1730 DEFine PROCedure movto(x,y)
1740 LET board(x0,y0) = board(x,y)
1750 putnum x0,y0
1760 LET board(x,y) = 0
1770 LET x0=x : y0=y
1780 END DEFine movto
1790 :
1800 DEFine PROCedure menu
1810 RESTORE 1890 : CLS #2
1820 REPeat loop
1830   IF EOF : EXIT loop
1840   READ x,y,m$
1850   AT #2,x,y : PRINT #2,m$
1860 END REPeat loop
1870 END DEFine menu
1880 :
1890 DATA 3,7,"-- PUZZLE GAME --"
1900 DATA 7,5,"� � � � - moves cursor"
1910 DATA 8,5,"space bar - move piece"
1920 DATA 9,5,"s - shuffle"
1930 DATA 10,5,"q - quit"


User avatar
Sparrowhawk
Super Gold Card
Posts: 720
Joined: Wed Dec 15, 2010 12:33 pm
Location: @131072

Re: 1st program in S*BASIC

Post by Sparrowhawk »

Not bad at all for a first SuperBASIC prog! Very nice job. :)

Interestingly, when I copied the code and loaded it into Q-emuLator for Mac, all the indented lines came up as corrupted and were marked with the fatal MISTAKE keyword. I edited them all to remove the Mistake marker and unprintable characters and the game works very nicely (apart from a few graphical glitches which I think may be due to an incorrect edit by me)


a.k.a. Jean-Yves
RWAP
RWAP Master
Posts: 2892
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: 1st program in S*BASIC

Post by RWAP »

Well line 1800 does not help. I wonder why every line starts with a TAB when you copy and paste it from the site....

To get this into q-emulator, I use TotalEdit which has the ability to set various things such as the character set, and line endings - I tried a new document with UTF-8 and <LF> as the line ending. Pasted the program into it and then highlighted all the lines, did SHIFT TAB to remove the starting tab on each line.

Saved it to a folder and ran it in q-emulator.

It worked, but line 1800 is corrupt - see image - I wonder if there is a way around this...:
TEST.png


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

Re: 1st program in S*BASIC

Post by RWAP »

Just some suggestions:

Code: Select all

1865 PAUSE 100
(otherwise you can't read the menu at the start).

Code: Select all

210   = 81,113 : STOP
220   = 83,115 : shuffle : layout
Allows you to deal with upper case letters....


tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: 1st program in S*BASIC

Post by tcat »

Hi All,

Thank you for suggestions, also I am sorry about extra tabs, I copied the code from Linux terminal, is the way I can get files accross to the web. So there also appears LF terminating each line. Also my terminal is set to UTF8 encoding. I need to learn how to upload code here as Latin-1 encoding, if that is preferable, is it?
TCAT


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

Re: 1st program in S*BASIC

Post by RWAP »

I think you will find the extra tabs at the start of each line are added by the forum software for the code tag which is a shame - maybe if showing basic code on the site, as well as using the code block, we should upload the original file as a zip attachment?


User avatar
1024MAK
Super Gold Card
Posts: 593
Joined: Sun Dec 11, 2011 1:16 am
Location: Looking forward to summer in Somerset, UK...

Re: 1st program in S*BASIC

Post by 1024MAK »

Well, I copied it, then pasted it into a Linux text editor (I'm using Ubuntu). Saved it and opened it in a hex editor.
At the start of each line there are four spaces (20h) and at the end of each line is a LF (0Ah).

Mark


:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer in Somerset later in the year :)

QL, Falcon, Atari 520STFM, Atari 1040STE, more PC's than I care to count and an assortment of 8 bit micros (Sinclair and Acorn)(nearly forgot the Psion's)
Derek_Stewart
Font of All Knowledge
Posts: 4682
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: 1st program in S*BASIC

Post by Derek_Stewart »

Hi,

I copied the code from RSS feed email and pasted it into a text file on a Ubuntu 14.04 Laptop.

Called the programme: Shuffle_bas

Loaded up QPC2 in Wine and loaded the programme Shuffle_bas. The programme loaded and ran without any errors.

I saved the programme to my RAID network server and can be loaded via another Windows QL Emulator.

Use QPC2, it runs on Windows, Linux with WINE installed.


Regards,

Derek
Derek_Stewart
Font of All Knowledge
Posts: 4682
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: 1st program in S*BASIC

Post by Derek_Stewart »

Hi,

Tried the programme code in SMSQemulator v2.01, works fine, loaded in via NFA1_ linked to my home folder in Linux.


Regards,

Derek
tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: 1st program in S*BASIC

Post by tcat »

Hi,

I wish to also share a screen capture of my first prog. As I use TV in monitor mode, I need to
adjust windows size, you can therefore see some black margins around boot screen.

This sets my windows as the best fit of TV visual area

Code: Select all

2 WINDOW #0,467,51,35,205
3 BORDER #0 : PAPER #0,0 : INK #0,4 : CLS #0
4 WINDOW #2,225,202,32,0
5 BORDER #2,1,255 : PAPER #2,7 : INK #2,2 : CLS #2
6 WINDOW #1,242,202,257,0
7 BORDER #1,1,255 : PAPER #1,2 : INK #1,7 : CLS #1
Puzzle Game
Puzzle Game
puzzle.png (3.15 KiB) Viewed 5111 times


Post Reply