Windows 10 Universal Windows Platform – Playing Cards

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 Windows.Foundation;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

public class Library
{
    private const string clubs = "♣";
    private const string diamonds = "♦";
    private const string hearts = "♥";
    private const string spades = "♠";
    private const string ace = "A";
    private const string jack = "J";
    private const string queen = "Q";
    private const string king = "K";

    private List<int> deckOne = new List<int>();
    private List<int> deckTwo = new List<int>();
    private int cardOne, cardTwo;
    private int first, second;
    private int score, counter;
    private Random random = new Random((int)DateTime.Now.Ticks);

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

        }
    }

    private TextBlock pip(string content, Color foreground, double fontSize, string fontFamily, double left, double top)
    {
        TextBlock pip = new TextBlock();
        pip.FontSize = fontSize;
        pip.FontFamily = new FontFamily(fontFamily);
        pip.Text = content;
        pip.Foreground = new SolidColorBrush(foreground);
        pip.SetValue(Canvas.LeftProperty, left);
        pip.SetValue(Canvas.TopProperty, top);
        return pip;
    }

    private Canvas deck(ref int card, ref Color back)
    {
        Canvas canvas = new Canvas();
        Color foreground = Colors.Black;
        string suite = "";
        string display = "";
        int value;
        if (card >= 1 && card <= 13)
        {
            foreground = Colors.Black;
            suite = clubs;
            display = card.ToString();
        }
        else if (card >= 14 && card <= 26)
        {
            foreground = Colors.Red;
            suite = diamonds;
            display = (card - 13).ToString();
        }
        else if (card >= 27 && card <= 39)
        {
            foreground = Colors.Red;
            suite = hearts;
            display = (card - 26).ToString();
        }
        else if (card >= 40 && card <= 52)
        {
            foreground = Colors.Black;
            suite = spades;
            display = (card - 39).ToString();
        }
        value = int.Parse(display);
        canvas.Width = 140;
        canvas.Height = 230;
        canvas.Background = new SolidColorBrush(back);
        switch (value)
        {
            case 1:
                display = ace;
                canvas.Children.Add(pip(suite, foreground, 100, "Times New Roman", 40, 50)); // Centre
                break;
            case 2:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 10)); // Middle Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 150)); // Middle Bottom
                break;
            case 3:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 10)); // Middle Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 80)); // Centre
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 150)); // Middle Bottom
                break;
            case 4:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 5:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 80)); // Centre
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 6:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 80)); // Centre Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 80)); // Centre Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 7:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 80)); // Centre Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 45)); // Centre Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 80)); // Centre Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 8:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 80)); // Centre Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 45)); // Centre Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 115)); // Centre Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 80)); // Centre Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 9:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 55)); // Centre Left Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 100)); // Centre Left Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 80)); // Centre
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 55)); // Centre Right Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 100)); // Centre Right Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 10:
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 10)); // Top Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 10)); // Top Right
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 55)); // Centre Left Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 100)); // Centre Left Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 55)); // Centre Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 55, 115)); // Centre Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 55)); // Centre Right Top
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 100)); // Centre Right Bottom
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 10, 150)); // Bottom Left
                canvas.Children.Add(pip(suite, foreground, 60, "Arial", 100, 150)); // Bottom Right
                break;
            case 11:
                display = jack;
                canvas.Children.Add(pip(display, foreground, 100, "Times New Roman", 55, 55)); // Centre
                break;
            case 12:
                display = queen;
                canvas.Children.Add(pip(display, foreground, 100, "Times New Roman", 40, 55)); // Centre
                break;
            case 13:
                display = king;
                canvas.Children.Add(pip(display, foreground, 100, "Times New Roman", 40, 55)); // Centre
                break;
        }
        if ((display.Length == 1))
        {
            canvas.Children.Add(pip(display, foreground, 20, "Times New Roman", 5, 0));
            canvas.Children.Add(pip(display, foreground, 20, "Times New Roman", 124, 205));
        }
        else
        {
            canvas.Children.Add(pip(display, foreground, 20, "Times New Roman", 5, 0));
            canvas.Children.Add(pip(display, foreground, 20, "Times New Roman", 120, 205));
        }
        return canvas;
    }

    private Canvas card(int value, bool facing, Color back)
    {
        if (facing)
        {
            return deck(ref value, ref back);
        }
        else
        {
            Canvas card = new Canvas();
            card.Width = 140;
            card.Height = 230;
            card.Background = new SolidColorBrush(back);
            return card;
        }
    }

    private void match(ref Canvas pileone, ref Canvas piletwo)
    {
        if ((cardOne < 52) && (cardTwo < 52))
        {
            first = deckOne[cardOne];
            pileone.Children.Clear();
            pileone.Children.Add(card(first, true, Colors.White));
            cardOne++;
            second = deckTwo[cardTwo];
            piletwo.Children.Clear();
            piletwo.Children.Add(card(second, true, Colors.White));
            cardTwo++;
            if ((first % 13) == (second % 13)) // Ignore Suite for Match
            {
                score++;
                Show("Match!", "Playing Cards");
            }
            counter++;
        }
        else
        {
            Show("Game Over! Matched " + score + " out of " + counter + " cards!", "Playing Cards");
        }
    }

    private List<int> select()
    {
        int number;
        List<int> numbers = new List<int>();
        while ((numbers.Count < 52)) // Select 52 Numbers
        {
            number = random.Next(1, 53); // Seeded Random Number
            if ((!numbers.Contains(number)) || (numbers.Count < 1))
            {
                numbers.Add(number); // Add if number Chosen or None
            }
        }
        return numbers;
    }

    public void New(Canvas pileone, Canvas piletwo)
    {
        score = 0;
        counter = 0;
        cardOne = 0;
        cardTwo = 0;
        deckOne = select();
        deckTwo = select();
        pileone.Children.Clear();
        pileone.Children.Add(card(1, false, Colors.IndianRed));
        pileone.Tapped += (object sender, TappedRoutedEventArgs e) =>
        {
            if (((Canvas)sender).Children.Count > 0)
            {
                match(ref pileone, ref piletwo);
            }
        };
        piletwo.Children.Clear();
        piletwo.Children.Add(card(1, false, Colors.DeepSkyBlue));
        piletwo.Tapped += (object sender, TappedRoutedEventArgs e) =>
        {
            if (((Canvas)sender).Children.Count > 0)
            {
                match(ref pileone, ref piletwo);
            }
        };
    }
}

It should then appear as such:

10-library-playingcards

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 Background="DarkGreen">
	<Grid.ColumnDefinitions>
		<ColumnDefinition Width="*"/>
		<ColumnDefinition Width="Auto"/>
		<ColumnDefinition Width="*"/>
		<ColumnDefinition Width="Auto"/>
		<ColumnDefinition Width="*"/>
	</Grid.ColumnDefinitions>
	<Canvas Grid.Column="1" Width="140" Height="230" Name="PileOne"/>
	<Canvas Grid.Column="3" Width="140" Height="230" Name="PileTwo"/>
</Grid>

It should appear as such:

10-xaml-playingcards

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.New(PileOne, PileTwo);
}

It should then appear as such:

10-code-playingcards

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

Step 14

After the Application has started running you can then tap the New Button, then Tap on either of the decks to show a Card, match value in both decks to score a point, do this until all the cards have been displayed

10-simulator-ran-playingcards

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

Step 18

After the Application has started running you can then tap the New Button, then Tap on either of the decks to show a Card, match value in both decks to score a point, do this until all the cards have been displayed

10-emulator-ran-playingcards

Step 19

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

10-stop

Creative Commons License

3 thoughts on “Windows 10 Universal Windows Platform – Playing Cards

  1. There is a bug in this program. Each time, click button new, code below runs. And after several click, this event will have a lots of handlers.

    pileone.Tapped += (object sender, TappedRoutedEventArgs e) =>
    {
    if (((Canvas)sender).Children.Count > 0)
    {
    match(ref pileone, ref piletwo);
    }
    };

    Like

    • Ideally wouldn’t do this indeed but in that example it was one of the places it wasn’t avoided due to pacing issue but I’ll make sure future tutorials don’t do this

      Like

Leave a comment