 


Task

Implement the Fifteen Puzzle Game.



The   15-puzzle   is also known as:

   Fifteen Puzzle
   Gem Puzzle
   Boss Puzzle
   Game of Fifteen
   Mystic Square
   14-15 Puzzle
   and some others.


Related Tasks

   15 Puzzle Solver
   16 Puzzle Game


 
Module Puzzle15 {
 
      00 BASE 1 : DEF RND(X)=RND
      10 REM 15-PUZZLE GAME
      20 REM COMMODORE BASIC 2.0
      30 REM ********************************
      40 GOSUB 400 : REM INTRO AND LEVEL
      50 GOSUB 510 : REM SETUP BOARD
      60 GOSUB 210 : REM PRINT PUZZLE
      70 PRINT "TO MOVE A PIECE, ENTER ITS NUMBER:"
      80 INPUT X
      90 GOSUB 730 : REM CHECK IF MOVE IS VALID
      100 IF MV=0 THEN PRINT "WRONG MOVE" : GOSUB 1050 : GOTO 60
      110 D(Z)=X : D(Y)=0
      120 GOSUB 210 : REM PRINT PUZZLE
      130 GOSUB 950 : REM CHECK IF PUZZLE COMPLETE
      140 IF PC THEN 160
      150 GOTO 70
      160 PRINT"YOU WON!"
      170 END
      180 REM
      190 REM *******************************
      200 REM PRINT/DRAW THE PUZZLE
      210 FOR P=1 TO 16
      220   IF D(P)=0 THEN D$(P)="     " : GOTO 260
      230   S$=STR$(D(P))
      240   N=LEN(S$)
      250   D$(P) = LEFT$("   ",3-N)+S$+"  "
      260 NEXT P
      270 PRINT "+-----+-----+-----+-----+"
      280 PRINT "!";D$(1);"!";D$(2);"!";D$(3);"!";D$(4);"!"
      290 PRINT "+-----+-----+-----+-----+"
      300 PRINT "!";D$(5);"!";D$(6);"!";D$(7);"!";D$(8);"!"
      310 PRINT "+-----+-----+-----+-----+"
      320 PRINT "!";D$(9);"!";D$(10);"!";D$(11);"!";D$(12);"!"
      330 PRINT "+-----+-----+-----+-----+"
      340 PRINT "!";D$(13);"!";D$(14);"!";D$(15);"!";D$(16);"!"
      350 PRINT "+-----+-----+-----+-----+"
      360 RETURN
      370 REM
      380 REM *******************************
      390 REM INTRO AND LEVEL OF DIFFICULTY
      400 PRINT CHR$(147)
      410 DIM SH(3) : SH(1)=10 : SH(2)=50 : SH(3)=100
      420 PRINT "15 PUZZLE GAME FOR COMMODORE BASIC 2.0" : PRINT : PRINT
      430 PRINT "PLEASE ENTER LEVEL OF DIFFICULTY,"
      440 PRINT "1(EASY), 2(MEDIUM) OR 3(HARD):";
      450 INPUT V
      460 IF V<1 OR V>3 THEN 440
      470 RETURN
      480 REM
      490 REM *******************************
      500 REM BUILD THE BOARD
      510 DIM D(16) : DIM D$(16) : REM BOARD PIECES
      520 REM SET PIECES IN CORRECT ORDER FIRST
      530 FOR P=1 TO 15
      540   D(P) = P
      550 NEXT P
      560 D(16) = 0 : REM 0 = EMPTY PIECE/SLOT
      570 Z=16      : REM Z = EMPTY POSITION
      580 PRINT: PRINT "SHUFFLING PIECES";
      590 FOR N=1 TO SH(V)
      600   PRINT".";
      610   X = INT(RND(0)*4)+1
      620   R = Z+(X=1)*4-(X=2)*4+(X=3)-(X=4)
      630   IF R<1 OR R>16 THEN 610
      640   D(Z)=D(R)
      650   Z=R
      660   D(Z)=0
      670 NEXT N
      680 PRINT CHR$(147)
      690 RETURN
      700 REM
      710 REM *******************************
      720 REM CHECK IF MOVE IS VALID
      730 MV = 0
      740 IF X<1 OR X>15 THEN RETURN
      750 REM FIND POSITION OF PIECE X
      760 P=1
      770 IF D(P)=X THEN Y=P : GOTO 810
      780 P=P+1 : IF P>16 THEN PRINT "UH OH!" : STOP
      790 GOTO 770
      800 REM FIND POSITION OF EMPTY PIECE
      810 P=1
      820 IF D(P)=0 THEN Z=P : GOTO 860
      830 P=P+1 : IF P>16 THEN PRINT "UH OH!" : STOP
      840 GOTO 820
      850 PRINT Y;Z
      860 REM CHECK IF EMPTY PIECE IS ABOVE, BELOW, LEFT OR RIGHT TO PIECE X
      870 IF Y-4=Z THEN MV=1 : RETURN
      880 IF Y+4=Z THEN MV=1 : RETURN
      890 IF Y-1=Z THEN MV=1 : RETURN
      900 IF Y+1=Z THEN MV=1 : RETURN
      910 RETURN
      920 REM
      930 REM *******************************
      940 REM CHECK IF PUZZLE IS COMPLETE / GAME OVER
      950 PC = 0
      960 P=1
      970 IF D(P)<>P THEN RETURN
      980 P=P+1
      990 IF P<16 THEN 970
      1000 PC = 1
      1010 RETURN
      1020 REM
      1030 REM ******************************
      1040 REM A SMALL DELAY
      1050 FOR T=0 TO 400
      1060 NEXT T
      1070 RETURN
}
Puzzle15
 