MaN1cPuzzle

MaN1CPuzzle is an implementation of the 15-Puzzle (also known as Magic 15, Game of Fifteen, Mystic Square, …), written in 20 lines of BASIC (max 80 chars per line) for the Sinclair ZX Spectrum homecomputer. You can change the size of the frame to M lines x N columns, where 3<M<4 and 3<N<6.

MaN1cPuzzle is my entry to the Puzzle Challenge, organized by RetroProgramming Italia 8 bit e oltre – Associazione Culturale in June 2019.

15-Puzzle solved!

You can either download it for use with a real ZX Spectrum or an emulator, or play it in your browser (keyboard is required) from the project page on itch.io.

Full program source code

1 LET M=4:LET N=4:DIM F(M,N):BORDER 0:INK 0:PAPER 0:DEF FN e(x)=INT(x/2)=x/2
2 CLS:FOR I=0 TO M-1:FOR J=0 TO N-1:LET T=FN e((I+J)):FOR X=I*5 TO I*5+4
3 FOR Y=J*5 TO J*5+4:POKE 22528+X*32+Y,32+T*8:NEXT Y:NEXT X:NEXT J:NEXT I
10 FOR I=1 TO M:FOR J=1 TO N:LET F(I,J)=(I-1)*N+J:NEXT J:NEXT I:LET F(M,N)=0
11 LET S=100:LET X=M:LET Y=N:LET V=0:GO SUB 30:PRINT #1;AT 0,0;"Shuffling% "
12 LET K=8+INT(RND*4):GO SUB 40:PRINT #1;AT 0,11;INT(V*100/S): GO TO 13-(V<S)
13 LET V=0:GO SUB 30:PRINT #1;AT 1,0;"MaN1cPuzzle by Marco's Retrobits"
14 LET K=CODE(INKEY$):RANDOMIZE:IF K<8 OR K>11 THEN GO TO 14
15 PRINT #1;AT 0,0;"Moving...";TAB 31:GO SUB 40:GO SUB 30: LET T=0
16 FOR I=1 TO M:FOR J=1 TO N:LET T=T+(F(I,J)=(I-1)*N+J):NEXT J:NEXT I
17 IF F(M,N)=0 AND T=M*N-1 THEN PRINT #1;AT 0,0;"SOLVED :) ":PAUSE 0:GO TO 10
18 GO TO 14:REM ** Tweak frame size: 2<M<4 2<N<6; shuffle moves: LET S=XXX **
30 FOR I=1 TO M:FOR J=1 TO N:PAPER 4+FN e((I+J)):PRINT AT (I-1)*5+2,(J-1)*5+2;
31 PRINT "  ";AT (I-1)*5+2,(J-1)*5+2;:IF F(I,J) THEN PRINT F(I,J)
32 NEXT J:NEXT I:PRINT #1;AT 0,0;"Moves:    ";V;TAB 31:BEEP .02,23:RETURN
40 IF K=8 AND Y<N THEN LET F(X,Y)=F(X,Y+1):LET F(X,Y+1)=0:LET Y=Y+1:LET V=V+1
41 IF K=9 AND Y>1 THEN LET F(X,Y)=F(X,Y-1):LET F(X,Y-1)=0:LET Y=Y-1:LET V=V+1
42 IF K=10 AND X>1 THEN LET F(X,Y)=F(X-1,Y):LET F(X-1,Y)=0:LET X=X-1:LET V=V+1
43 IF K=11 AND X<M THEN LET F(X,Y)=F(X+1,Y):LET F(X+1,Y)=0:LET X=X+1:LET V=V+1
44 RETURN:REM **** 20-Liner BASIC M*N-1 Puzzle game by Marco Varesio 2019 ****
3×3 frame: shuffling…

Tweaking

The game behavior can be modified by changing some variables assignments:

  • Frame lines number: LET M = m (m being a number between 3 and 4) at line 1
  • Frame columns number: LET N = n (n being a number between 3 and 6) at line 1
  • Number of random tiles movements when shuffling: LET S = s at line 11
4×6 frame size
Leggi in Italiano

Leave a comment

Your email address will not be published. Required fields are marked *