100 REM                 The 15 Puzzle
101 REM                         by Dale Dewey
102 REM                            7284 High View Trail
103 REM                            Victor, New York  14564
104 REM                 Copyright, 1982
105 REM
110 ON ERROR GOTO 130
120 PLAY "mf": ON ERROR GOTO 0: GOTO 200
130 IF ERR<>73 THEN RESUME 200
140 WIDTH 80:CLS:LOCATE 3,1
150 PRINT "You're NOT using BASICA!"
160 PRINT "This program uses advanced features."
170 PRINT "PRESS any key to continue."
180 IF INKEY$="" THEN 180
190 RESUME 650
200 DEF SEG=0
210 IF (PEEK(&H410) AND &H30)<>&H30 THEN GOTO 290
220 WIDTH 80:CLS:LOCATE 3,1
230 PRINT "You're NOT using the COLOR/GRAPHICS Monitor Adapter!"
240 PRINT "This program uses graphics and requires that adapter."
250 PRINT "PRESS any key to continue."
260 DEF SEG
270 IF INKEY$="" THEN 270
280 GOTO 650
290 DEFINT I-N
300 DIM ST(16), S(5,5)
310 KEY OFF: CLS: SCREEN 1,0: COLOR 0,1
320 LOCATE 1,14: PRINT "The 15 Puzzle"
330 GOSUB 680
340 MOVE=0
345 LOCATE 23,13: PRINT "Move =>> "CHR$(27)CHR$(24)CHR$(25)CHR$(26);
350 ANS$=INKEY$:IF ANS$="" THEN 350
355 WHILE INKEY$<>"":WEND
360 IF LEN(ANS$)=1 THEN IF ANS$="Q" OR ANS$="q" THEN 630 ELSE 430
370 Q=ASC(RIGHT$(ANS$,1))
390 IF Q=72 AND YZ>1 THEN X0=XZ: Y0=YZ-1: GOTO 440
400 IF Q=75 AND XZ>1 THEN X0=XZ-1: Y0=YZ: GOTO 440
410 IF Q=77 AND XZ<4 THEN X0=XZ+1: Y0=YZ: GOTO 440
420 IF Q=80 AND YZ<4 THEN X0=XZ: Y0=YZ+1: GOTO 440
430 GOSUB 610: GOTO 350
440 MOVE=MOVE+1
450 SWAP S(Y0,X0),S(YZ,XZ)
460 N0=S(Y0,X0): C0=0
470 GOSUB 1150
480 SWAP Y0,YZ: SWAP X0,XZ
490 N0=S(Y0,X0): C0=2
500 GOSUB 1150
510 LOCATE 5,14: PRINT USING "Move ####";MOVE;
520 GOSUB 560
530 IF WIN <>1 THEN 350
540 LOCATE 23,12: PRINT "You have WON!"
550 GOTO 630
560 FOR I=1 TO 4
570 FOR J=1 TO 4
580 IF (I=4) AND (J=4) THEN WIN=1: RETURN
590 IF S(I,J)<>J+(I-1)*4 THEN WIN=0: RETURN
600 NEXT J: NEXT I
610 LOCATE 24,13: PRINT "Illegal Move!!";: FOR I=1 TO 2000: NEXT: LOCATE 24,13: PRINT "              ";
620 RETURN
630 LOCATE 25,1: PRINT "Would you like another puzzle? ";:ANS$=INPUT$(1)
640 IF ANS$="Y" OR ANS$="y" THEN 310
650 ON ERROR GOTO 670
660 END 'RUN "MENU"
670 SCREEN 0,0,0: COLOR 7,0: WIDTH 40: ON ERROR GOTO 0: END
680 REM        Build Screen
690 LINE (0,25)-(50,44),1,BF
700 LINE (0,162)-(50,181),1,BF
710 LINE (224,24)-(319,167),,B
720 LINE (66,50)-(205,157),,B
730 LINE (71,55)-(200,152),,B
740 PAINT (70,54),3,3
750 FOR I=1 TO 3: LINE (71+32*I,55)-(71+32*I,152): LINE (71,55+24*I)-(200,55+24*I): NEXT I
760 FOR I=0 TO 55 STEP 5
770 CIRCLE (25,103),I,1,,,2.5
780 NEXT I
790 DEF SEG: POKE &H4E,1
800 LOCATE 5,30: PRINT "This  is a"
810 LOCATE 6,30: PRINT "computer  "
820 LOCATE 7,30: PRINT "version of"
830 LOCATE 8,30: PRINT "   the    "
840 LOCATE 9,30: PRINT "15-PUZZLE."
850 LOCATE 11,30: PRINT "The object"
860 LOCATE 12,30: PRINT "is to move"
870 LOCATE 13,30: PRINT "the blocks"
880 LOCATE 14,30: PRINT "to  form a"
890 LOCATE 15,30: PRINT "pattern of"
900 LOCATE 16,30: PRINT "1-4,  5-8,"
910 LOCATE 17,30: PRINT "9-12,  and"
920 LOCATE 18,30: PRINT "13-15 with"
930 LOCATE 19,30: PRINT "one  empty"
940 LOCATE 20,30: PRINT "space!!   "
950 LOCATE 25,12: PRINT "ENTER `Q' to quit";
960 DEF SEG: POKE &H4E,3
970 PLAY "mbt160"
980 RANDOMIZE VAL(RIGHT$(TIME$,2))
990 FOR I=1 TO 16
1000  ST(I)=INT(RND*16)+1
1010  IF I=1 THEN 1060
1020  FOR J=1 TO I-1
1030   IF ST(I)=ST(J) THEN 1000
1040  NEXT J
1050  SOUND ST(I)*100,.75
1060 NEXT I
1070 FOR I=1 TO 4
1080  FOR J=0 TO 3
1090   X0=J+1: Y0=I: S(Y0,X0)=ST(I+J*4)
1100   IF S(Y0,X0)=16 THEN S(Y0,X0)=0: C0=0: YZ=Y0: XZ=X0 ELSE C0=2
1110   N0=S(Y0,X0)
1120 GOSUB 1150
1130 NEXT J: NEXT I
1140 RETURN
1150 REM       Set a square (SQ) to color (C0) & number (N0)
1160 PAINT (45+32*X0,37+24*Y0),0,3
1170 LOCATE 6+Y0*3,7+X0*4
1180 PRINT "  ";CHR$(29);CHR$(29);: IF N0<>0 THEN PRINT USING "##";N0;: PLAY "L16ac"
1190 PAINT (45+32*X0,37+24*Y0),C0,3
1200 RETURN