10 REM***THE FIFTEEN PUZZLE 20 REM***W. L. COLSHER 25 DIM A(16) 30 INPUT"DO YOU NEED INSTRUCTIONS (Y=1, N=2)";A:IF A=1 THEN GOSUB 10000 31 ' CLS:INPUT"DO YOU NEED INSTRUCTIONS (Y=1, N=2)";A:IF A=1 THEN GOSUB 10000 40 M=0:PRINT"GENERATING THE PUZZLE TAKES A WHILE. PLEASE WAIT." 41 ' CLS:M=0:PRINT"GENERATING THE PUZZLE TAKES A WHILE. PLEASE WAIT." 50 FOR I=1 TO 16:A(I)=0:NEXT I 60 FOR I=1 TO 16 70 R=RND(16) 80 IF A(R)<>0 THEN 70 90 A(R)=I 100 NEXT I 110 GOSUB 5000 120 IF F=1 THEN 50 130 GOSUB 6000 140 PRINT" ":INPUT"YOUR MOVE";X 145 GOSUB 4000 150 GOSUB 7000 160 IF F<>0 THEN 180 170 PRINT"ILLEGAL MOVE, RE-ENTER":FOR I=1 TO 500:NEXT I:GOTO 130 180 A(X+F)=A(X):A(X)=16 190 GOTO 8000 200 M=M+1:GOTO 130 999 END 4000 REM***C0NVERT NUMBER TO LOCATION IN ARRAY 4010 FOR I=1 TO 16 4020 IF A(I)=X THEN 4040 4030 NEXT I 4040 X=I 4050 RETURN 5000 REM***VERIFY SOLUTION POSSIBLE 5005 F=1 5010 S=0 5020 FOR I=1 TO 15 5030 FOR J=I+1 TO 16 5040 IF A(I)>A(J) THEN S=S+1 5050 NEXT J:NEXT I 5060 FOR I=1 TO 8 5070 READ X 5080 IF A(X)=0 THEN S=S+1 5090 NEXT I 5095 RESTORE 5100 A=INT(S/2) 5110 IF A*2=S THEN F=0 5120 RETURN 5130 DATA 2,4,5,7,10,12,13,15 6000 REM***DISPLAY GAME BOARD 6004 ' CLS:L=339:P.A.217,"MOVE ";M : ' P.A.pos, pos=0..63->line 1, 64..127->line 2 etc. 6005 PRINT "MOVE ";M 6010 FOR I=1 TO 4 6014 ' P.A.L," "; 6015 PRINT" "; 6020 FOR J=1 TO 4 6025 N=A((I-1)*4+J) 6028 IF N=16 THEN N=0 6030 IF N<10 THEN PRINT" ";N; 6038 IF (N=10) * (N>16) THEN PRINT N; 6040 NEXT J 6050 L=L+64 6060 NEXT I 6070 RETURN 7000 REM***CHECK FOR LEGAL MOVE 7010 F=0 7015 IF X+1>16 THEN 7025 7020 IF A(X+1)*16 THEN F=1 7025 IF X-1>=0 THEN 7035 7030 IF A(X-1)=16 THEN F=-1 7035 IF X+4>16 THEN 7045 7040 IF A(X+4)=16 THEN F=4 7045 IF X-4<=0 THEN 7060 7050 IF A(X-4)=16 THEN F=-4 7060 RETURN 8000 REM***CHECK FOR A WIN 8010 FOR I=1 TO 16 8020 IF A(I)<>I THEN 200 8030 NEXT I 8040 GOSUB 6000 8050 PRINT" ":PRINT" " 8060 PRINT"CONGRATULATIONS!!! YOU DID IT IN ONLY ";M;" MOVES!!" 8070 PRINT" ":INPUT"TO PLAY AGAIN, HIT ENTER. ";A$ 8090 GOTO 10 10000 REM***INSTRUCTIONS 10010 PRINT"F I F T E E N P U Z Z L E" 10011 ' CLS:P.A.18,"F I F T E E N P U Z Z L E" 10020 PRINT"THE OBJECT OF THE 'FIFTEEN PUZZLE IS TO MOVE THE" 10021 ' P.A.128,"THE OBJECT OF THE 'FIFTEEN PUZZLE IS TO MOVE THE" 10030 PRINT"NUMBERS AROUND SO THAT THEY ARE IN ORDER FROM 1 TO 15. A MOVE" 10040 PRINT"IS MADE BY TYPING IN THE NUMBER (WHICH MUST BE ADJACENT TO" 10050 PRINT"THE ZERO) YOU WISH TO MOVE. THAT NUMBER IS THEN EXCHANGED" 10060 PRINT"WITH THE ZERO. YOU WIN WHEN THE BOARD LOOKS LIKE THIS:" 10070 PRINT" ":PRINT" ":PRINT" 1 2 3 4":PRINT" 5 6 7 8":PRINT" 9 10 11 12" 10080 PRINT"13 14 15 0" 10090 PRINT" ":INPUT"HIT ENTER TO PLAY";A$ 10100 RETURN