 


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


int Box, Hole, I;
[Box:= [^ ,^F,^E,^D,    \starting configuration
        ^C,^B,^A,^9,    \slide digits into ascending order
        ^8,^7,^6,^5,    \with blank in lower-right corner
        ^4,^3,^2,^1];
Hole:= 0;               \index for hole position
loop    [Clear;         \erase screen and move to start
        for I:= 0 to 15 do \show puzzle
                [ChOut(0, Box(I));  ChOut(0, ^ );
                if (I & 3) = 3 then CrLf(0)];
        case ChIn(1) of                 \get move
          $1B:  quit;                   \Esc
          $48:  if Hole < 12 then       \UpArrow scan code
                        [Box(Hole):= Box(Hole+4);
                        Hole:= Hole+4;
                        Box(Hole):= ^ ];
          $50:  if Hole > 3 then        \DnArrow
                        [Box(Hole):= Box(Hole-4);
                        Hole:= Hole-4;
                        Box(Hole):= ^ ];
          $4B:  if (Hole & 3) < 3 then  \LfArrow
                        [Box(Hole):= Box(Hole+1);
                        Hole:= Hole+1;
                        Box(Hole):= ^ ];
          $4D:  if (Hole & 3) > 0 then  \RtArrow
                        [Box(Hole):= Box(Hole-1);
                        Hole:= Hole-1;
                        Box(Hole):= ^ ]
        other   [];     \ignore 0 scan code prefix etc.
        ];
]