views:

671

answers:

9

Hi! I'm a relative novice at C# and am thoroughly stuck! 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!

Thanks for your time!

A: 

I have no idea where to start!

A book on WinForms or WPF would be a good place to begin. Try this or this.

No one here is going to code up an application for you that does what you want, and if they did - what would you have learnt?

Winston Smith
I appreciate that, I'm not looking for any code! I just wanted ideas on structure and where to start
+2  A: 

Start by writing some code! :) Create a console project and try to print out those numbers.

reticent
A: 

Start by asking yourself a couple of questions such as: How will you store the numbers and how will you rearrange them. Then write a couple of tests that will you test your code. E.g. write a test case, that verifies that you can swap two numbers. Have a look at TDD on how to do this.

Also, think about how to present the board. Is this a console application or GUI? Console is easier, but less fancy.

Brian Rasmussen
A: 

Assuming you want to create a desktop application try researching winforms, buttons, click events and setting the buttons text.

A quick google bought up this:

http://www.codeproject.com/KB/game/slidingpuzzle.aspx

Which is probably too much detail at this point but should get you started.

oookiezooo
+1  A: 

I'd approach this using Domain-Driven Design. Concentrate on designing some classes which represent the entities in the problem domain - so I'd probably have Puzzle, Tile and EmptySpace classes, and maybe a Wall or Block class. When you can push the tiles around using unit tests then look at putting a GUI on the top.

Matt Howells
+1  A: 

Start with a UML Diagram and define the classes that you will need for the puzzle e.g. to begin you'll probably need

  • A game class
  • A tile class
  • etc...

Then define the properties and behaviours that each class will need. For example, for the tile class,

  • a number enum/identifier
  • a move method
  • etc...

Once you are happy with your UML diagram, start writing the code to implement the design

Russ Cam
A: 

You should start by deciding how to represent a position using c# objects. For example an array. So that you can start writing use cases for TDD.

You should then see if you in code can determine wether or not the position is legal (only one tile of each legal number, and one "missing" tile).

Then you should be able to, in code, to determine wether or not the final position has been reached (this may not be a requirement, but....)

Then you should start develop something that can hold such a representation and determine legal moves.

Then you should extend the implemnentation into something that accepts moves and determines the new position.

Tormod
+6  A: 

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

  • an engine that manages the state of the puzzle
    • the engine must be able to reset the puzzle
    • the engine must be able to scramble the puzzle without going into an unsolvable state
    • the engine must be able to perform moves
    • the engine must be able to detect whether the puzzle is solved
  • a user interface (Console, Web or Desktop) that
    • fetches the state from the engine and displays it
    • offers the user to start over
    • offers the user to perform a move
    • shows wheter the puzzle is solved (and congratulates the user if so).

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.

froh42
wow, wasn't expecting this much detail! thank you so much for your time!
A: 

This is most likely not useful at all for your assignment, but I couldn't help myself... ;)

You can store the grid of tiles in 19 bits of an integer. This is how you draw the grid:

public static void DrawGrid(int grid) {
   string display = " 12345678";
   for (int i = 9; i > 0; i--) {
      Console.Write("{0}{1}", display[grid % i], i % 3 == 1 ? "\r\n" : "");
      display = display.Remove(grid % i, 1);
      grid /= i;
   }
}

The tiles are stored as digits in a number with a variable base. The first digit is base 9 as there are 8 possible values for it. The second digit is base 8 as there are 7 possible values for it (the first value can not be repeated in the second position), and so on.

This grid:

1 2 3
4 5 6
7 8

is represented as this value, and drawn using the method above:

int grid = 1+9*(1+8*(1+7*(1+6*(1+5*(1+4*(1+3*1))))));
DrawGrid(grid);

Moving the pieces around is of course a bit more complicated... ;)

Guffa