--- title: "Shifting Squares" id: 77518 type: "computer_media" slug: "shifting-squares" url: "http://localhost/computer_media/shifting-squares/" markdown_url: "http://localhost/computer_media/shifting-squares.md" published_at: "2026-11-15T18:38:14+00:00" modified_at: "2026-11-15T18:38:14+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "A 3x3 tile puzzle that the QL shuffles and then solves by random moves, counting them." post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" genre: - name: "Demo" slug: "demo" taxonomy: "genre" url: "http://localhost/type/demo/" media_type: "Diskette" download_url: "https://archive.org/download/timex-sinclair-software-archive/Shifting%20Squares%20%281987%29%28-%29%28Sinclair%20QL%29%28Program%29.zip" mediadate: "1987" article_media: - id: 42111 title: "QL Shorts" type: "article" url: "http://localhost/article/ql-shorts/" media_type_tags: "Demo" license: false --- # Shifting Squares A 3×3 sliding-tile puzzle that the QL plays by itself. The board of tiles 1 to 9 (9 is the space) is shuffled with 25 to 34 random moves, then the computer keeps making random legal moves, printing the board after each one, until the tiles are back in order, and reports how many moves it took. The legal moves for each position of the space are held in DATA. With purely random moves a solution can take a very long time. No author or source is printed; it appeared in the QL Shorts column with short programs by Rob Miles and D Duncan. Published in Sinc-Link July-August 1987 ([QL Shorts](https://timexsinclair.com/article/ql-shorts/)). ## Source Code ``` 12 WINDOW 452,236,60,20 14 MODE 8 20 init 30 print_board 40 set_shuffle 50 find_space 60 REPeat game 70 win_pos 80 IF S%=0 THEN EXIT game 90 rnd_mve 100 do_move 110 MOVE%=MOVE%+1 120 print_board 130 END REPeat game 140 PRINT "SOLVED IN ";MOVE% 1000 DEFine PROCedure init 1010 DIM B%(9) 1020 DIM M$(9,4) 1030 DIM M%(9) 1040 Q%=0 1050 DATA "24 ","135 ","26 ","157 ","2468" 1060 DATA "359 ","48 ","579 ","68 " 1070 DATA 2,3,2,3,4,3,2,3,2 1080 RESTORE 1050 1090 FOR I=1 TO 9 1100 READ M$(I) 1110 END FOR I 1120 FOR I=1 TO 9 1130 READ M%(I) 1140 END FOR I 1150 MOVE%=0 1499 END DEFine init 1500 DEFine PROCedure print_board 1510 FOR I=1 TO 9 1520 PRINT B%(I);" "; 1530 IF I=INT(I/3)*3 THEN PRINT 1540 END FOR I 1550 PRINT 1560 pause_1 1999 END DEFine print_board 2000 DEFine PROCedure set_shuffle 2010 FOR I=1 TO 9 2020 B%(I)=I 2030 END FOR I 2040 P%=9 2050 N%=RND(25 TO 34) 2060 FOR Z=1 TO N% 2070 rnd_mve 2080 do_move 2090 END FOR Z 2100 Q%=0 2999 END DEFine set_shuffle 3000 DEFine PROCedure find_space 3010 FOR I=1 TO 9 3020 IF B%(I)=9 THEN P%=I 3030 END FOR I 3999 END DEFine find_space 4000 DEFine PROCedure win_pos 4010 S%=0 4020 FOR I=1 TO 9 4030 IF I<>B%(I) THEN S%=1 4040 END FOR I 4999 END DEFine win_pos 5000 DEFine PROCedure pause_1 5010 FOR I=1 TO 500 5020 END FOR I 5999 END DEFine pause_1 6000 DEFine PROCedure rnd_mve 6010 I%=RND(1 TO M%(P%)) 6020 J%=M$(P%,I%) 6999 END DEFine rnd_mve 7000 DEFine PROCedure do_move 7010 T%=B%(P%) 7020 B%(P%)=B%(J%) 7030 B%(J%)=T% 7040 Q%=P% 7050 P%=J% 7999 END DEFine do_move ```