Project 2 - The Eight Puzzle

In this project you are building a GUI application that plays a simplified version of the 15-puzzle called the 8-puzzle. Starting from a "random" arrangement of numbered tiles in a 3x3 game space, tiles are slid one at a time into the open space until they are in numeric order.

Introduction

"The 15-puzzle (also called Gem Puzzle, Boss Puzzle, Game of Fifteen, Mystic Square and many others) is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing. The puzzle also exists in other sizes, particularly the smaller 8-puzzle. If the size is 3×3 tiles, the puzzle is called the 8-puzzle or 9-puzzle, and if 4×4 tiles, the puzzle is called the 15-puzzle or 16-puzzle named, respectively, for the number of tiles and the number of spaces. The object of the puzzle is to place the tiles in order (see diagram) by making sliding moves that use the empty space."[1]

As shown in Figure 1, we will be building our puzzle as a GUI application in the WinForms or WPF formats. We will use buttons as the game tiles and a panel as the tile container. As a guide you may use the following game piece sizes and locations.

Putting buttons inside a panel makes their location relative to the upper left corner of the panel

Buttons are named btn1 through btn8 upper left corner of button # is btn#.Location which is a Point type.

Size buttons to 60 x 60 pixels. Arrange them in the panel with 4 pixels spacing between buttons and the border of the panel

Panel is 3*60 + 4*4 = 196 x 196 pixels

Valid X and Y positions in the panel are 4, 68, and 132.

In the 3x3 grid there are 9 valid positions which are all the tuples (X,Y pairs) of these three valid positions.

Implementation

Create the GUI as described with the panel, buttons and other elements in the indicated locations and with the specified names and/or labels.

Create Click events for each of the eight tile buttons. If a button is free to move into the openCellAt position, exchange the Location of the tile button with the Location of openCellAt. Rather than hard-wire code into the eight Click events, create a method called move_button( ) that, by some means performs the operation of moving the button.

A button should be moved only if it is adjacent to the openCellAt location. A goal of this project is to find an efficient means of determining if a button is adjacent to the open cell.

Submission

Place a zipped project folder for your finished project in your FTP site (see Dr. Lyle :-)

[1] 15 Puzzle,