Universal Windows Platform – Memory Game

Memory Game demonstrates how to use a Grid to implement a simple shape pairing memory 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.

vs2017

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-window

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.

vs2017-target-platform

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…

vs2017-project-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

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
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 Library
{
    private const string app_title = "Memory Game";
    private const int size = 4;

    private int _moves = 0;
    private int _firstId = 0;
    private int _secondId = 0;
    private Canvas _first;
    private Canvas _second;
    private int[,] _board = new int[size, size];
    private List<int> _matches = new List<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 UIElement Shape(ref PointCollection points, Color fill)
    {
        Polygon polygon = new Polygon
        {
            Height = 40,
            Width = 40,
            Stretch = Stretch.Uniform,
            StrokeLineJoin = PenLineJoin.Round,
            Points = points,
            Fill = new SolidColorBrush(fill),
            Stroke = new SolidColorBrush(fill),
            StrokeThickness = 5,
            Margin = new Thickness(10)
        };
        return polygon;
    }

    private UIElement Card(int type)
    {
        PointCollection points = new PointCollection();
        switch (type)
        {
            case 1: // Circle
                Path circle = new Path();
                EllipseGeometry ellipse = new EllipseGeometry
                {
                    Center = new Point(20, 20),
                    RadiusX = 20,
                    RadiusY = 20
                };
                circle.Data = ellipse;
                circle.Fill = new SolidColorBrush(Colors.Blue);
                circle.Stroke = new SolidColorBrush(Colors.Blue);
                circle.Margin = new Thickness(10);
                return circle;
            case 2: // Cross
                Path cross = new Path();
                LineGeometry line1 = new LineGeometry
                {
                    StartPoint = new Point(0, 0),
                    EndPoint = new Point(40, 40)
                };
                LineGeometry line2 = new LineGeometry
                {
                    StartPoint = new Point(40, 0),
                    EndPoint = new Point(0, 40)
                };
                GeometryGroup linegroup = new GeometryGroup();
                linegroup.Children.Add(line1);
                linegroup.Children.Add(line2);
                cross.Data = linegroup;
                cross.Stroke = new SolidColorBrush(Colors.Red);
                cross.StrokeThickness = 5;
                cross.Margin = new Thickness(10);
                return cross;
            case 3: // Triangle
                points.Add(new Point(150, 0));
                points.Add(new Point(0, 250));
                points.Add(new Point(300, 250));
                return Shape(ref points, Colors.Green);
            case 4: // Square
                points.Add(new Point(0, 0));
                points.Add(new Point(0, 100));
                points.Add(new Point(100, 100));
                points.Add(new Point(100, 0));
                return Shape(ref points, Colors.DarkMagenta);
            case 5: // Pentagon
                points.Add(new Point(0, 125));
                points.Add(new Point(150, 0));
                points.Add(new Point(300, 125));
                points.Add(new Point(250, 300));
                points.Add(new Point(50, 300));
                return Shape(ref points, Colors.Crimson);
            case 6: // Hexagon
                points.Add(new Point(75, 0));
                points.Add(new Point(225, 0));
                points.Add(new Point(300, 150));
                points.Add(new Point(225, 300));
                points.Add(new Point(75, 300));
                points.Add(new Point(0, 150));
                return Shape(ref points, Colors.DarkCyan);
            case 7: // Star
                points.Add(new Point(9, 2));
                points.Add(new Point(11, 7));
                points.Add(new Point(17, 7));
                points.Add(new Point(12, 10));
                points.Add(new Point(14, 15));
                points.Add(new Point(9, 12));
                points.Add(new Point(4, 15));
                points.Add(new Point(6, 10));
                points.Add(new Point(1, 7));
                points.Add(new Point(7, 7));
                return Shape(ref points, Colors.Gold);
            case 8: // Rhombus
                points.Add(new Point(50, 0));
                points.Add(new Point(100, 50));
                points.Add(new Point(50, 100));
                points.Add(new Point(0, 50));
                return Shape(ref points, Colors.OrangeRed);
            default:
                return null;
        }
    }

    private void Add(ref Grid grid, int row, int column)
    {
        Canvas canvas = new Canvas
        {
            Height = 60,
            Width = 60,
            Margin = new Thickness(10),
            Background = new SolidColorBrush(Colors.WhiteSmoke) 
        };
        canvas.Tapped += async (object sender, TappedRoutedEventArgs e) =>
        {
            int selected;
            canvas = ((Canvas)(sender));
            row = (int)canvas.GetValue(Grid.RowProperty);
            column = (int)canvas.GetValue(Grid.ColumnProperty);
            selected = _board[row, column];
            if ((_matches.IndexOf(selected) < 0))
            {
                if ((_firstId == 0)) // No Match
                {
                    _first = canvas;
                    _firstId = selected;
                    _first.Children.Clear();
                    _first.Children.Add(Card(selected));
                }
                else if ((_secondId == 0))
                {
                    _second = canvas;
                    if (!_first.Equals(_second)) // Different
                    {
                        _secondId = selected;
                        _second.Children.Clear();
                        _second.Children.Add(Card(selected));
                        if ((_firstId == _secondId)) // Is Match
                        {
                            _matches.Add(_firstId);
                            _matches.Add(_secondId);
                            if (!(_first == null))
                            {
                                _first.Background = null;
                                _first = null;
                            }
                            if (!(_second == null))
                            {
                                _second.Background = null;
                                _second = null;
                            }
                            if ((_matches.Count == 16))
                            {
                                Show($"Well Done! You matched them all in {_moves} moves!", app_title);
                            }
                        }
                        else // No Match
                        {
                            await Task.Delay(TimeSpan.FromSeconds(1.5));
                            if (!(_first == null))
                            {
                                _first.Children.Clear();
                                _first = null;
                            }
                            if (!(_second == null))
                            {
                                _second.Children.Clear();
                                _second = null;
                            }
                        }
                        _moves++;
                        _firstId = 0;
                        _secondId = 0;
                    }
                }
            }
        };
        canvas.SetValue(Grid.ColumnProperty, column);
        canvas.SetValue(Grid.RowProperty, row);
        grid.Children.Add(canvas);
    }

    private void Layout(ref Grid grid)
    {
        _moves = 0;
        _matches.Clear();
        grid.Children.Clear();
        grid.ColumnDefinitions.Clear();
        grid.RowDefinitions.Clear();
        // Setup Grid
        for (int index = 0; (index < size); index++)
        {
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        // Setup Board
        for (int row = 0; (row < size); row++)
        {
            for (int column = 0; (column < size); column++)
            {
                Add(ref grid, row, column);
            }
        }
    }

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

    public void New(Grid grid)
    {
        Layout(ref grid);
        List<int> values = new List<int>();
        List<int> indices = new List<int>();
        int counter = 0;
        while (values.Count <= size * size)
        {
            List<int> numbers = Select(1, size * 2, size * 2); // Random 1 - 8
            for (int number = 0; number < size * 2; number++)
            {
                values.Add(numbers[number]); // Add to Cards
            }
        }
        indices = Select(1, size * size, size * size); // Random 1 - 16
        for (int column = 0; column < size; column++) // Board Columns
        {
            for (int row = 0; row < size; row++) // Board Rows
            {
                _board[column, row] = values[indices[counter] - 1];
                counter++;
            }
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. There’s a two-dimensional array called _board which represents what will appear in the game and Random is used as part of the process to place the pairs of shapes randomly. The Shape method is used to create polygons and Card is used to build up all the shapes including polygons. Then Add is used to create the look-and-feel and behaviour of the game which is used by Layout

Step 8

In the Solution Explorer select MainPage.xaml

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

<Viewbox>
	<Grid Margin="50" Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Viewbox>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>

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

vs2017-view-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(Display);
}

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 Board in the Grid

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

vs2017-local-machine

Step 14

After the Application has started running you can then tap the New Button, then tap on any two squares shown to display a Shape, match the shapes to make a pair, match them all to win

ran-memory-game

Step 15

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

vs2017-close

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