Windows 10 Universal Windows Platform – Memory Game

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

10-new-project

Step 4

Once done select from the Menu, Project, then Add New Item…

10-project-add-new-item

Step 5

From the Add New Item window select Visual C# 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

10-add-new-item-library

Step 6

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 int moves = 0;
    private int firstId = 0;
    private int secondId = 0;
    private Canvas first;
    private Canvas second;
    private int[,] board = new int[4, 4];
    private List<int> matches = new List<int>();
    private Random random = new Random((int)DateTime.Now.Ticks);

    public Brush Background { get; set; }

    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();
        polygon.Stretch = Stretch.Uniform;
        polygon.StrokeLineJoin = PenLineJoin.Round;
        polygon.Points = points;
        polygon.Height = 40;
        polygon.Width = polygon.Height;
        polygon.Fill = new SolidColorBrush(fill);
        polygon.Stroke = new SolidColorBrush(fill);
        polygon.StrokeThickness = 5;
        polygon.Margin = new Thickness(10);
        return polygon;
    }

    private UIElement card(int type)
    {
        PointCollection points = new PointCollection();
        switch (type)
        {
            case 1: // Circle
                EllipseGeometry ellipse = new EllipseGeometry();
                Path circle = new Path();
                ellipse.Center = new Point(20, 20);
                ellipse.RadiusX = 20;
                ellipse.RadiusY = 20;
                circle.Data = ellipse;
                circle.Stroke = new SolidColorBrush(Colors.DarkRed);
                circle.Fill = new SolidColorBrush(Colors.DarkRed);
                circle.StrokeThickness = 5;
                circle.Margin = new Thickness(10);
                return circle;
            case 2: // Cross
                Path lines = new Path();
                LineGeometry line1 = new LineGeometry();
                LineGeometry line2 = new LineGeometry();
                GeometryGroup linegroup = new GeometryGroup();
                line1.StartPoint = new Point(0, 0);
                line1.EndPoint = new Point(40, 40);
                line2.StartPoint = new Point(40, 0);
                line2.EndPoint = new Point(0, 40);
                linegroup.Children.Add(line1);
                linegroup.Children.Add(line2);
                lines.Data = linegroup;
                lines.Stroke = new SolidColorBrush(Colors.DarkBlue);
                lines.StrokeThickness = 5;
                lines.Margin = new Thickness(10);
                return lines;
            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();
        canvas.Height = 60;
        canvas.Width = 60;
        canvas.Background = Background;
        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!", "Memory Game");
                            }
                        }
                        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 4x4 Grid
        for (int Index = 0; (Index <= 3); Index++)
        {
            Grid.RowDefinitions.Add(new RowDefinition());
            Grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        add(ref Grid, 0, 0);
        add(ref Grid, 0, 1);
        add(ref Grid, 0, 2);
        add(ref Grid, 0, 3);
        add(ref Grid, 1, 0);
        add(ref Grid, 1, 1);
        add(ref Grid, 1, 2);
        add(ref Grid, 1, 3);
        add(ref Grid, 2, 0);
        add(ref Grid, 2, 1);
        add(ref Grid, 2, 2);
        add(ref Grid, 2, 3);
        add(ref Grid, 3, 0);
        add(ref Grid, 3, 1);
        add(ref Grid, 3, 2);
        add(ref Grid, 3, 3);
    }

    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 < 17)
        {
            List<int> numbers = select(1, 8, 8); // Random 1 - 8
            for (int number = 0; (number <= 7); number++)
            {
                values.Add(numbers[number]); // Add to Cards
            }
        }
        indices = select(1, 16, 16); // Random 1 - 16
        for (int Column = 0; (Column <= 3); Column++) // Board Columns
        {
            for (int Row = 0; (Row <= 3); Row++) // Board Rows
            {
                board[Column, Row] = values[indices[counter] - 1];
                counter++;
            }
        }
    }
}

It should then appear as such:

10-library-memorygame

Step 7

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 8

Select from the Menu, View then Designer

10-designer

Step 9

The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Icon="Page" Label="New" Click="New_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

10-xaml-memorygame

Step 10

Select from the Menu, View then Code

10-code

Step 11

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

public Library Library = new Library();

private void New_Click(object sender, RoutedEventArgs e)
{
	Library.Background = ((Brush)App.Current.Resources["ApplicationSecondaryForegroundThemeBrush"]);
	Library.New(Display);
}

It should then appear as such:

10-code-memorygame

Step 12

That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application

10-simulator

Step 13

Once the Simulator has started the Application should then appear

10-simulator-run-memorygame

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

10-simulator-ran-memorygame

Step 15

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 16

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 17

Once the Emulator has started the Application should then appear

10-emulator-run-memorygame

Step 18

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

10-emulator-ran-memorygame

Step 19

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

4 thoughts on “Windows 10 Universal Windows Platform – Memory Game

    • You could use images instead of the shapes by setting the source of the canvas to be an image in the resources of the application

      Like

    • You could add animations with Storyboards where you can have animation of frames and also link to sound resources – don’t have much room to go into it here but suggest looking at playing sounds and animations in UWP – but might consider doing something with animation in a future tutorial

      Like

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