I’m a relative novice at C# and am thoroughly stuck! For a school assignment I need to make a sliding puzzle where numbered tiles are to be rearranged in order by using a blank space, i.e.
[1] [2] [3]
[4] [5] [6]
[7] [8] [ ]
I have no idea where to start.
Regardless on how the application will be used (Winforms UI, Console, Web or whatever) you need to focus on how to build such an application. So start by considering what you will need to do:
You’ll need
At beginner level I’d start with the engine. You will want to write a C# class that holds the data. Consult a C# tutorial on info how to use the language, I’ll focus on the problem here:
The enigne needs to hold the puzzle. We have 9 fields and 8 tiles. So we might just use a fixed array of length 9. Each entry in the array is a number that describes a tile. 1 is tile one, 2 is tile two and so on up to tile 8. We use 0 to describe the empty tile.
Then you need to implement methods for the moves. At any time you can try to move tile into the empty slot from up, right, bottom or left. Which one is the empty tile? The entry in our array that contains 0.
So let’s write four methods up, down, left, right to implement the moves. Let’s focus on “up” that moves a tile from the upper slot into the empty slot. We can assume our array maps to the puzzle as follows:
0 1 2
3 4 5
6 7 8
So if the array contains “7 6 5 3 0 1 2 4 8” the puzzle would look
7 6 5
3 _ 1
2 4 8
The up methods now needs to find the “0” in the array and exchange it with the value in the row above:
If the “0” is in the upper row (array index 0, index 1 or index 2) there is no upper row and “up” throws an exception. It can’t work.
If “0” is on another index i the index “above” i will be index i-3. So we exchange the values of index i and index i-3 in the array.
You’d implement the “left”, “right” and “bottom” methods similarily. Have a look at so called “unit-testing” software on how to write test cases for your software. (Nunit, MBUnit)
At last build a method or property in your puzzle class that checks whether the contents of the array are in the correct order “1 2 3 4 5 6 7 8 0” when it is solved.
Now you have a puzzle class that implements the logic.
As a last (but nevertheless big step) you now need to read a Winforms or WPF tutorial on how to build a UI. But now you SHOULD have learned enough about C# to find & read a tutorial and follow it through.