Assignment Description:
You are to write a graphical slide puzzle game. The goal of this game is to put the numbers 1-15 in order (from left to right, and then top to bottom) with the blank piece in the lower-right. When the user clicks on a piece, it slides all pieces either horizontally or vertically from the clicked square towards the blank piece / spot.
When your project starts, it should randomly position the numbers 1-15 and the blank square. Here is a possible random configuration See image.
As an example, if the user clicks on the 11, the pieces in that row slide left, towards the blank square:
If the user were to click on the 14 at this point, NOTHING should happen (because there is no way to slide the 14 horizontally or vertically towards the blank square).
Your program should allow the user to keep moving pieces until they reach the winning configuration: See image.
(Note that half of all random starting positions are unsolvable no matter how many moves you make. Thats OK you dont need to check for this.)
Implementation Requirements:
Your program will be written in C# using Visual Studio. You should use the configuration show in the screenshots above for setting up your display (buttons for each piece of the puzzle and a label for displaying the winning message.) For the most part you are free to implement your project in whatever way you choose, but here a few additional requirements:
- You must store each button in an array (like the Tic-Tac-Toe game demonstrated in class)
- You will have a single method that is called whenever any button is clicked and you should not have lots of if-statements for doing things differently for different buttons. Your code should be general enough to work for ANY button that is clicked. (This will be discussed in class)
- You must have methods for randomly positioning the starting board, checking if the user has won, and handling a move (the button click event). You may have additional methods if you wish.
- Once the user wins, display a You Win! message and disable the buttons. (You can disable a button by setting its Enabled property to false.)
Random Board Placement:
The puzzle numbers should be randomly initialized (including the placement of the blank space), with different values for each game. Random number generators in C# are very similar to what we did in Java. Here is an example:
Random r = new Random();
int num = r.Next(5);
Now num is randomly 0, 1, 2, 3, or 4. You may assign random numbers in whatever way you choose, but you might consider using the algorithm below:
- Create a List object that can hold ints. Every time you place one of the numbers on the board, you will at it to the List.
- Create a new random number generator
- Use a nested loop to go through each button in your 2D array
- Loop to continually generate a random number from 1-16 (16 will be the blank square) until you get a number that is not in your List (you can call the Contains method to see if an item is in a List
- Make that number be the text of the current button
- Add that number to your List
- If the number was 16 (the blank button), set its Visible property to false
Shifting Pieces:
You should have a single Shift method that is the Click event for ALL buttons in your 2D array. This method should handle shifting pieces around and should be flexible enough to work no matter which button was clicked. You may shift the pieces using whatever method you want, but you might consider using the algorithm below:
- Get the Button that was clicked (it is your source parameter you need to cast it to a Button)
- Get the row and column indices of the clicked button (hint: loop through the 2D array of buttons and compare the Text values until you find a match)
- Get the row and column indices of the blank button (remember, its Text is 16)
- If the rows of the clicked and blank buttons arent equal and the columns of the clicked and blank buttons arent equal, the board will not change
- If the columns of the clicked and blank buttons match but the rows dont
- Loop to copy the Text values from the clicked button towards the blank button.
- The clicked button should now become the new blank button and the old blank button should become visible update the necessary Visible and Text properties
- Similarly, handle the case if the rows of the clicked and blank buttons match but the columns do not
- Call your method to check for a winner.The Winner method should:
- Check to see if Text on the buttons goes 1,2,3,,15,16 (the blank button) this means the puzzle is solved
- Display your winning message and loop through your 2D buttons array to disable all the buttons.