Implementing A star algorithm to sliding tile puzzle game using C# forms

86 Views Asked by At

last week we learned the A* algorithm in the artificial intelligence class. Our teacher showed us step-by-step how to use this algorithm to arrive at a destination on the board. He drew it by hand and he used the maze example which is a bunch of squares came together. And there was an obstacle. He mentioned open and closed lists but he didn't use them.

Then he gave us an assignment that implements this algorithm in an 8-puzzle game. The requirements are: Players can declare the puzzle size from 2x2 to 10x10 or more. Players can set the beginning and the finished positions of the tiles (or numbers). The movement of the tiles must be observable. Using C# and Winforms is a must. The student must write the algorithm by himself. Using a library is forbidden.

First, I tried to implement it by looking at visual tutorials on how to solve the puzzle on the console. I didn't succeed so I tried it on the forms.

At first glance, I thought I could use neighborhood relations of the tiles the determine what could be the next move. So I tried to write an algorithm that generates tiles dynamically, creates tile objects, assigning neighborhood relationships. I failed again.

Then, I created a 3x3 puzzle and objects and assigned the relationships manually to focus on the algorithm. I wrote it but it was solving the specific cases. Most of the time it was going on an endless loop.

So, how can I implement it truly as the teacher asks? I already got a bad score on the assignment but want to learn.

1

There are 1 best solutions below

1
FandangoOnCore On

Some time ago I implemented the A* algorithm for a project in Unity. I adapted the code a little and created a small interface in Winforms. This is the result:

enter image description here

If you think this can be of any help, you can get the project from GitHub:

https://github.com/FandangoOnCore/AStarWinforms