Slide Game demonstrates how to create a game which is based on those sliding fifteen-piece puzzle games but where you tap on a piece to move it rather than slide it to solve the puzzle put all the numbers in descending numerical order.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Linq; using Windows.Foundation; using Windows.UI; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; public class Piece : Grid { public Piece(int index) { this.Background = new SolidColorBrush(Colors.Black); Rectangle rect = new Rectangle() { Stroke = new SolidColorBrush(Colors.Gray), }; TextBlock text = new TextBlock() { FontSize = 30, Text = index.ToString(), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Foreground = new SolidColorBrush(Colors.White) }; this.Children.Add(rect); this.Children.Add(text); } public int Row { get; set; } public int Column { get; set; } } public class Library { private const string app_title = "Slide Game"; private const int size = 4; private int _moves = 0; private int[,] _board = new int[size, size]; private List<int> _values; private Random _random = new Random((int)DateTime.Now.Ticks); private List<int> Shuffle(int start, int total) { return Enumerable.Range(start, total).OrderBy(r => _random.Next(start, total)).ToList(); } public void Show(string content, string title) { IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync(); } private bool Valid(int row, int column) { if (row < 0 || column < 0 || row > 3 || column > 3) { return false; } return (_board[row, column] == 0); } private bool Check() { int previous = _board[0, 0]; for (int row = 0; row < size; row++) { for (int column = 0; column < size; column++) { if (_board[row, column] < previous) { return false; } previous = _board[row, column]; } } return true; } private void Move(Canvas canvas, Piece piece, int row, int column) { _moves++; _board[row, column] = _board[piece.Row, piece.Column]; _board[piece.Row, piece.Column] = 0; piece.Row = row; piece.Column = column; Layout(canvas); if (Check()) { Show($"Correct In {_moves} Moves", app_title); } } private void Layout(Canvas canvas) { canvas.Children.Clear(); for (int row = 0; row < size; row++) { for (int column = 0; column < size; column++) { if (_board[row, column] > 0) { int index = _board[row, column]; Piece piece = new Piece(index) { Width = canvas.Width / size, Height = canvas.Height / size, Row = row, Column = column }; Canvas.SetTop(piece, row * (canvas.Width / size)); Canvas.SetLeft(piece, column * (canvas.Width / size)); piece.PointerReleased += (object sender, PointerRoutedEventArgs e) => { piece = (Piece)sender; if (Valid(piece.Row - 1, piece.Column)) { Move(canvas, piece, piece.Row - 1, piece.Column); } else if (Valid(piece.Row, piece.Column + 1)) { Move(canvas, piece, piece.Row, piece.Column + 1); } else if (Valid(piece.Row + 1, piece.Column)) { Move(canvas, piece, piece.Row + 1, piece.Column); } else if (Valid(piece.Row, piece.Column - 1)) { Move(canvas, piece, piece.Row, piece.Column - 1); } }; canvas.Children.Add(piece); } } } } public void New(ref Canvas canvas) { int index = 1; _values = Shuffle(1, _board.Length); _values.Insert(0, 0); for (int row = 0; row < size; row++) { for (int column = 0; column < size; column++) { _board[row, column] = _values[index++]; if (index == size * size) index = 0; } } Layout(canvas); } }
The Piece Class that represents each item that will be displayed on the screen and extends the Grid – it is a Rectangle with a TextBlock on it plus properties to indicate the Row and Column. The main Library Class has an const int to set the size of the game layout plus an int[,] for this set to that size plus a list of values and a random number generator, there’s a Shuffle method to use the random number generator and a Show method to display a message on screen.
The Valid method checkes to see if a move is valid or contains an empty square to move into, Check validates if the board contains a decending sequence of numbers to indicate that the puzzle is complete if that’s the case and is used by the Move method which will use Piece to set the appropriate Row and Column value. Layout is used to set up the game layout and declare each Piece and set it’s initial position with the PointerReleased event on a Piece which will use Valid to verify a move is valid then use Move to before the movement with each possible direction this can be – Up, Right, Down and Left. Then there’s the New method to create a new game and set up the game ready to be played.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Viewbox Margin="50"> <Canvas Name="Display" HorizontalAlignment="Center" Height="400" Width="400"/> </Viewbox> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Page2" Label="New" Click="New_Click"/> </CommandBar>
The first block of XAML the main user interface of the Game, this features a ViewBox containing a Canvas to display the game itself within. The second block of XAML is is the CommandBar which contains New to start a Game
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { library.New(ref Display); }
Below the MainPage() Method an instance of the Library Class is created, then New_Click is used to call the New Method to setup the game and start playing.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then select New to start the game then you need to select a Piece nearest the empty slot to move it into that slot and then continue until all the Numbers are in the correct order from Left to Right from 1 to 15 to then complete the game.
Step 15
To Exit the Application select the Close button in the top right of the Application
This game is based on the fifteen piece puzzle and could be extended to actually allow the pieces to be slid but it shows off ability to obey the correct rules for this puzzle as well as checking for a win condition – the pieces don’t just have to be numbers it could be made to display a picture just like the puzzle the example is based on and then more complex functionality can be added to fine tune it to be more like a real fifteen-piece sliding puzzle.