Universal Windows Platform – Order Game

Order Game demonstrates how to create a simple game where the objective is to re-arrange numbered squares in the correct sequence in the quickest time possible.

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 Fall Creators Update (10.0; Build 16299) 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.Collections.ObjectModel;
using System.Linq;
using Windows.Foundation;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

public class Library
    private const string app_title = "Order Game";
    private const int size = 6;
    private const int total = size * size;

    private DateTime _timer;
    private ObservableCollection<int> _list = new ObservableCollection<int>();
    private Random _random = new Random((int)DateTime.Now.Ticks);

    public void Show(string content, string title)
        IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync();

    private List<int> Select(int start, int finish, int total)
        int number;
        List<int> numbers = new List<int>();
        while ((numbers.Count < total)) // Select Numbers
            // Random Number between Start and Finish
            number = _random.Next(start, finish + 1);
            if ((!numbers.Contains(number)) || (numbers.Count < 1))
                numbers.Add(number); // Add if number Chosen or None
        return numbers;

    private bool Winner()
        return _list.OrderBy(o => o).ToList().SequenceEqual(_list.ToList());

    private void Layout(ref GridView grid)
        _timer = DateTime.UtcNow;
        grid.IsEnabled = true;
        grid.ItemsSource = null;
        _list = new ObservableCollection<int>();
        List<int> numbers = Select(1, total, total);
        int index = 0;
        while (index < numbers.Count)
        grid.ItemsSource = _list;

    public void New(ref GridView grid)
        Layout(ref grid);

    public void Order(ref GridView grid)
        if (Winner())
            TimeSpan duration = (DateTime.UtcNow - _timer).Duration();
            Show($"Well Done! Completed in {duration.Hours} Hours, {duration.Minutes} Minutes and {duration.Seconds} Seconds!", app_title);
            grid.IsEnabled = false;

In the Library.cs there are using statements to include the necessary functionality. There are Members including a DateTime which will be used to measure how long the game takes to complete and an ObservableCollection of int which will be the list of items itself. The Show Method is used to display a MessageDialog and Select is used to pick a selection of numbers using Random. Winner is used to determine if the game has been completed, Layout is used with New to create the look-and-feel of the application and Order is used when ordering items in the game until the game has been won.

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:

	<GridView Margin="50" HorizontalAlignment="Center" VerticalAlignment="Center" CanDrag="True" CanDragItems="True" AllowDrop="True"
		CanReorderItems="True" SelectionMode="Single" Name="Display" DragItemsCompleted="Display_DragItemsCompleted">
				<Grid Width="48" Height="48" Background="{ThemeResource ApplicationSecondaryForegroundThemeBrush}">
					<TextBlock Text="{Binding}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
				<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="6"/>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>

The first block of XAML the main user interface of the Application, this features a ViewBox containing a GridView Control that will be the look-and-feel of the game which has the ability to reorder items and has a DataTemplate which will represent the items themselves and the ItemsPanel of the GridView is a ItemsWrapGrid to make the items display correctly. 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);

private void Display_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
	library.Order(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 in the Library Class and Display_DragItemsCompleted is used when re-ordering the items to call the Order Method in the Library Class.

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 use New to start playing, you can win by putting all the numbers in order from 1 to 36 from left to right as quickly as possible in the Application by draging and moving them into the correct positions.


Step 15

To Exit the Application select the Close button in the top right of the Application


Creative Commons License

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 )

Connecting to %s