Example Code

The Sam Loyd (n² – 1)–Puzzle

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Operating System

  • Windows

Code and Documents

Attachment

Description

Implementation of the famous (n² – 1) Puzzle by Sam Loyd

 

The game consists of a square grid consisting of n² - 1 tiles and an empty position that allows adjacent tiles to be moved. The goal is to arrange the tiles in ascending order from left to right and top to bottom, starting from the top-left corner.

mceclip0.png

By clicking the left mouse button on a tile adjacent (horizontally or vertically) to the empty one, the tile slides into the vacant position.

The first level of the puzzle (n = 3) presents a 3×3 grid with 8 tiles and one empty cell. The game includes 5 levels, each with a number of tiles that grows quadratically.

Right-clicking on a non-empty tile provides free hints displayed in the “Tips” row located above the game board. Clicking the “Help” button generates a strategic move suggested by the computer, at the cost of a 5-second penalty.

Right-clicking on the “Help” button opens a shortcut menu that allows you to speed up or slow down the automatic execution of moves by the computer.

The “About” menu item pauses the game (trick!).

To start, restart or stop a game, use the menu bar, from which you can also consult the ranking of the 10 best players.

 


References

H. Richard, "The Sam Loyd 15-Puzzle," Dublin, Trinity College Dublin, Department of Computer Science, TCD-CS-2001-24, 2001, pp. 28. 

I. Parberry, "A Real-Time Algorithm for the (n^2-1)-Puzzle," 5 February 1997, Preprint submitted to Elsevier Science.

How to Use

To start the game, open the file "sliding puzzle.vi".

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors