Reader Level:
ARTICLE

Puzzle Control

Posted by Busila Marian Articles | Games Programming C# October 15, 2002
In this application I had combined the power of C# and AI in order to solve the "puzzle problem".
  • 0
  • 0
  • 13463
Download Files:
 

In this application I had combined the power of C# and AI in order to solve the "puzzle problem".

I created a puzzle control which can be personalized. One can specify the number of lines and columns of the puzzle, the puzzle color, the numbers font.





The advantage of a control is that it can be hosted by a form or by InternetExplorer. The algorithm used to solve the puzzle problem is BeamSearch (the euristic function is the straight line distance).

I developed a class PuzzleControl : Control which contains a set of properties which allow to personalize the puzzle as shown in the figure above.

public int Lines
public int Columns
public Size SquareSize
public int PenWidth
public Color PenColor
public int BorderWidth
public Color BorderColor
public Font NumberFont
public Color NumberColor

As I mentioned before, I used BeamSearch algorithm to find the solution of the puzzle(the sequence of moves to get to final state). Initially, I used A* algorithm but it wasn't the best solution because of memory requirement.

A variable "Queue" is used to record unexpanded nodes. I limited the size of "Queue" (the best 20 nodes) in order to save memory. A node contains the state of puzzle ( a matrix with the numbers) and a list of moves (moves of blank) that have been done to that state.

The PuzzleControl includes operations such as: Init, Shuffle(right click on the control), Solve and StopSolve. So, the puzzle can be solved by a human player or by computer. If someone try to resolve the puzzle and need some help, he can ask computer and after a couple of help moves he can select "Computer-> Stop" from menu and continue resolve the puzzle by himself.

Note 1 : In order to move a piece of the puzzle, click the left button of the mouse on the surface of that piece. The piece will change the place with the blank. Only the pieces next to the blank can be moved.

Note 2 : Sometimes it will take more to resolve the puzzle (for the computer), because of the large number of moves that need to be done to the final solution.

Shuffled puzzle

COMMENT USING

Trending up