Universal Windows Platform – Slide Game

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 locate and select Visual Studio 2017.

vs2017-home

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-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

vs2017-new-project

Step 4

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 5

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 6

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 7

Then 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 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", "Slide Game");
        }
    }

    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);
    }
}

Step 8

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-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:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<Canvas Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-slide-game

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the public MainPage() { … } the following Code should be entered:

Library library = new Library();

private void New_Click(object sender, RoutedEventArgs e)
{
	library.New(ref Display);
}

It should then appear as such:

code-slide-game

Step 13

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 14

Once started the Application should then appear

run-slide-game

Step 15

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 win the game.

ran-slide-game

Step 16

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Advertisements

One thought on “Universal Windows Platform – Slide Game

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s