Universal Windows Platform – Touch Game

Touch Game demonstrates how to use a Grid to implement a Touch-based pattern matching 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 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.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;

public class Library
    private const string app_title = "Touch Game";
    private const int size = 2;
    private const int speed = 800;
    private const int light = 400;
    private const int click = 200;
    private const int level = 100;

    private readonly Color[] colours =
        Colors.Crimson, Colors.Green,
        Colors.Blue, Colors.Gold
    private readonly Color clicked = Colors.Black;
    private readonly Color lighted = Colors.WhiteSmoke;

    private int _turn = 0;
    private int _count = 0;
    private bool _play = false;
    private bool _isTimer = false;
    private List _items = new List();
    private DispatcherTimer _timer = new DispatcherTimer();
    private Random _random = new Random((int)DateTime.Now.Ticks);

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

    private void Highlight(Grid grid, int value, int period, Color background)
        Grid element = (Grid)grid.Children.Single(s =>
            (int)((Grid)s).Tag == value);
        element.Background = new SolidColorBrush(background);
        DispatcherTimer lightup = new DispatcherTimer()
            Interval = TimeSpan.FromMilliseconds(period)
        lightup.Tick += (object sender, object e) =>
            element.Background = new SolidColorBrush(colours[(int)element.Tag]);

    private List Shuffle(int start, int finish, int total)
        int number;
        List numbers = new List();
        while ((numbers.Count 
            if (_play)
                int value = (int)((Grid)sender).Tag;
                Highlight(grid, value, click, clicked);
                if (value == _items[_count])
                    if (_count < _turn)
                        _play = false;
                        _count = 0;
                        _isTimer = true;
                    _isTimer = false;
                    Show($"Game Over! You scored {_turn}!", app_title);
                    _play = false;
                    _turn = 0;
                    _count = 0;
        element.SetValue(Grid.ColumnProperty, column);
        element.SetValue(Grid.RowProperty, row);

    private void Layout(ref Grid grid)
        // Setup Grid
        for (int index = 0; (index < size); index++)
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        int count = 0;
        // Setup Board
        for (int column = 0; (column < size); column++)
            for (int row = 0; (row 
            if (_isTimer)
                if (_count  _turn)
                    _isTimer = false;
                    _play = true;
                    _count = 0;

In the Library.cs there are using statements to include the necessary functionality. There’s a List of int used to store the pattern. The Highlight method is used to indicate a stage of the pattern and Shuffle is used to randomise the pattern. Add is used by Layout to create the look-and-feel of the game, which is started with the New Method

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:



The first block of XAML the main user interface of the Application, this features a Grid where the pairs of shapes to select will appear. The second block of XAML is is the CommandBar which contains the New – to begin 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)

Below the MainPage() Method an instance of the Library Class is created, then in the New_Click Event the New Method to display the Game in the Grid and start the pattern

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 tap the New Button, then one of the squares will highlight, select the correct one, then each time one more square will highlight each turn, match the patterns to continue


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 )

Google+ photo

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

Connecting to %s