1 DEF FN r(v)=INT (RND*v)+1: REM we'll be using this a lot here for setup, although in real chess it should barely be used at all... 10 GO SUB 9400: REM modularity is pretty much vital in making Sinclair BASIC legible 20 GO SUB 9100 30 GO SUB 9200 40 GO SUB 8800 50 GO TO 9500 8799 REM Material assignment 8800 LET b=FN r(16)-1: REM give black and white a random number of additional pieces each 8810 LET w=FN r(16)-1 8820 FOR x=1 TO b 8830 LET p=LEN b$ 8840 LET k=FN r(p) 8850 LET r=FN r(8): REM defined functions can have the same name as variables 8860 LET f=FN r(8) 8870 IF d$(r,f)<>" " THEN GO TO 8840: REM piece already there 8880 IF b$(k)="p" AND (r=1 OR r=8) THEN GO TO 8840: REM pawn on 1st or 8th rank trapping 8890 LET d$(r,f)=b$(k) 8900 IF k=p THEN LET b$=b$(1 TO k-1): GO TO 8920 8910 LET b$=b$( TO k-1)+b$(k+1 TO ) 8920 NEXT x 8930 FOR x=1 TO w: REM now doing for white what we just did for black, could be tidied 8940 LET p=LEN w$ 8950 LET k=FN r(p) 8960 LET r=FN r(8) 8970 LET f=FN r(8) 8980 IF d$(r,f)<>" " THEN GO TO 8950 8990 IF w$(k)="P" AND (r=1 OR r=8) THEN GO TO 8950 9000 LET d$(r,f)=w$(k) 9010 IF k=p THEN LET w$=w$(1 TO k-1): GO TO 9030 9020 LET w$=w$( TO k-1)+w$(k+1 TO ) 9030 NEXT x 9040 RETURN 9099 REM Kings 9100 LET rw=FN r(8) 9110 LET fw=FN r(8) 9120 LET d$(rw,fw)="K" 9130 LET rb=FN r(8) 9140 LET fb=FN r(8) 9150 LET rd=ABS (rw-rb): REM find distance between kings 9160 LET fd=ABS (fd-fb) 9170 IF rd<2 AND fd<2 THEN GO TO 9130 9180 LET d$(rb,fb)="k" 9190 RETURN 9199 REM Promotions 9200 FOR b=1 TO 8 9210 LET v=FN r(30): REM 30% chance of promoting each pawn 9220 IF v=1 THEN LET b$(7+b)="q": GO TO 9260 9230 IF v<3 THEN LET b$(7+b)="r": GO TO 9260 9240 IF v<7 THEN LET b$(7+b)="n": GO TO 9260 9250 IF v<10 THEN LET b$(7+b)="b" 9260 NEXT b 9270 FOR w=1 TO 8 9280 LET v=FN r(30) 9290 IF v=1 THEN LET w$(7+w)="Q": GO TO 9330 9300 IF v<3 THEN LET w$(7+w)="R": GO TO 9330 9310 IF v<7 THEN LET w$(7+w)="N": GO TO 9330 9320 IF v<10 THEN LET w$(7+w)="B" 9330 NEXT b 9340 RETURN 9399 REM Setup 9400 LET b$="qrrnnbbpppppppp": REM we'll set the kings specifically later 9410 LET w$="QRRNNBBPPPPPPPP" 9420 DIM d$(8,8): REM string arrays are autofilled with spaces; this defines the board area 9430 LET f$="" 9440 LET g$="" 9450 RETURN 9499 REM Board 9500 FOR x=1 TO 8 9510 PRINT 9-x;: REM rank number 9520 FOR y=1 TO 8 9530 PRINT INVERSE (((x+y)/2)<>INT ((x+y)/2));d$(x,y);: REM evaluates to 1, a black square, if rank number + file number, read from the top left, is odd; the bottom right square is 8 + 8 = 16 = even = white, conforming to "white to the right" standards 9540 LET f$=f$+d$(x,y): REM building the FEN 9550 NEXT y 9560 PRINT ': REM newline 9570 LET f$=f$+"/" 9580 NEXT x 9590 PRINT " abcdefgh"'' 9600 LET k=LEN f$: REM f$ now contains the full string of the board, but we need to count the spaces into numbers 9610 LET t=0: REM counter variable 9620 FOR x=1 TO k 9630 LET t$=f$(x) 9640 IF t$<>" " AND t=0 THEN GO TO 9670: REM not empty and not currently running through blank spaces 9650 IF t$=" " THEN LET t=t+1: GO TO 9670: REM number of blanks in this batch so far 9660 LET f$(x-1)=STR$ t: LET t=0: REM not empty, so a blank space sequence must have ended; insert the counter into the string (STR$ turns the numeric value into its string representation) 9670 NEXT x 9680 FOR x=1 TO k: REM now strip the spaces out 9690 IF f$(x)<>" " THEN LET g$=g$+f$(x) 9700 NEXT x 9710 LET g$=g$( TO LEN g$-1)+" w - - 0 1": REM knock off the final / 9720 PRINT g$