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"