
If the grid width is odd (e.g., 3×3), then the number of inversions in a solvable situation is even. If not, it randomizes the puzzle again, and repeats this process until a solvable arrangement is found. Borrowing from what others have done, my code chooses a totally random arrangement, then checks if it is solvable (see the “solvable” function in the code). If, instead, one simply places each tile at random, it turns out that only half of the possible arrangements can be slid back to the solution. If one scrambles the puzzle by making random allowed moves, then the puzzle can always be solved, however it takes many, many such movements to randomize the puzzle. It then waits indefinitely until the player either presses START to go back to the “intro” state and play again or turns the badge off. The software is open source and published on GitHub. Once in the “solved” state, the program displays a “You Won” message, then the full image. If so, the state transitions to “solved”. After each move, the puzzle is checked to see if it is in the solved position. Then the state transitions to “play.” In “play”, the program monitors the up, down, left, and right buttons and moves the tiles accordingly. Once a selection is made, the state transitions to “setup.” In this state, the puzzle is scrambled and the scrambled puzzle is displayed.
“intro” displays the puzzle image and then asks the player to select either a 3×3 or 4×4 puzzle. It roughly follows a state machine pattern with the the states being “intro”, “setup”, “play”, and “solved”. The tiled graphics approach used in the Adafruit displayio library is ideally suited for a sliding puzzle game.Īfter initial setup and bookkeeping, the program has an infinite while loop. It is configurable so that different images can be used, and it supports both 3×3 (8 tile) and 4×4 (15 tile) puzzles. It uses pictures for the puzzle, with numbers superimposed to make the puzzles easier to solve. This is a sliding puzzle game written in #CircuitPython for the #PyBadge and PyBadge LC. In addition to physical puzzles, video versions can be found on computers and the web.
Many such puzzles have been made, in various sizes. Basically, the tiles are randomized, and then must be returned, by sliding adjacent tiles into the open space one at a time, until the puzzle is returned to the original arrangement. The puzzles may have letters forming words or have pictures instead of, or in addition to, numbers. The first one, a 4×4, 15 tile puzzle, was made in 1890. A plastic sliding 15 puzzle using numbers