Universal Windows Platform – Lucky Dominoes

Lucky Dominoes demonstrates a simple way of creating the look-and-feel of a Domino as well as obeying the rules of a single Domino. Although it’s a simple example it could form the basis of any domino-based 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, version 1803 (10.0; Build 17134) which is the April 2018 Update 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.Linq;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
{
    private string[] tags = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    private string[] tiles =
    {
        "0,0",
        "0,1", "1,1",
        "0,2", "1,2", "2,2",
        "0,3", "1,3", "2,3", "3,3",
        "0,4", "1,4", "2,4", "3,4", "4,4",
        "0,5", "1,5", "2,5", "3,5", "4,5", "5,5",
        "0,6", "1,6", "2,6", "3,6", "4,6", "5,6", "6,6"
    };
    private int[][] table =
    {
                 // a, b, c, d, e, f, g, h, i
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0
        new int[] { 0, 0, 0, 0, 1, 0, 0, 0, 0 }, // 1
        new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
        new int[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 }, // 3
        new int[] { 1, 0, 1, 0, 0, 0, 1, 0, 1 }, // 4
        new int[] { 1, 0, 1, 0, 1, 0, 1, 0, 1 }, // 5
        new int[] { 1, 0, 1, 1, 0, 1, 1, 0, 1 }, // 6
    };

    private Random _random = new Random((int)DateTime.Now.Ticks);
    private List<int> _one = new List<int>();
    private List<int> _two = new List<int>();
    private int _turns = 0;

    private List<int> Shuffle(int total)
    {
        return Enumerable.Range(0, total).OrderBy(r => _random.Next(0, total)).ToList();
    }

    private void Add(ref Grid grid, int row, int column, string name)
    {
        Ellipse ellipse = new Ellipse()
        {
            Name = name,
            Fill = new SolidColorBrush(Colors.White),
            Margin = new Thickness(5),
            Opacity = 0
        };
        ellipse.SetValue(Grid.ColumnProperty, column);
        ellipse.SetValue(Grid.RowProperty, row);
        grid.Children.Add(ellipse);
    }

    private Grid Portion(string name)
    {
        int size = 3;
        Grid grid = new Grid()
        {
            Name = name,
            Width = 100,
            Height = 100,
            Padding = new Thickness(5),
            Background = new LinearGradientBrush(new GradientStopCollection()
            {
                new GradientStop() { Color = Colors.Black, Offset = 0.0 },
                new GradientStop() { Color = Colors.Gray, Offset = 1.0 }
            }, 90)
        };
        // Setup Grid
        for (int index = 0; (index < size); index++)
        {
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        int count = 0;
        for (int row = 0; (row < size); row++)
        {
            for (int column = 0; (column < size); column++)
            {
                Add(ref grid, row, column, $"{name}.{tags[count]}");
                count++;
            }
        }
        return grid;
    }

    private StackPanel Domino(string name)
    {
        Grid upper = Portion($"{name}.upper");
        Grid lower = Portion($"{name}.lower");
        StackPanel panel = new StackPanel()
        {
            Name = name,
            Orientation = Orientation.Vertical,
            Margin = new Thickness(25),
        };
        panel.Children.Add(upper);
        panel.Children.Add(lower);
        return panel;
    }

    private void Layout(ref Grid grid)
    {
        StackPanel one = Domino("one");
        StackPanel two = Domino("two");
        StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };
        panel.Children.Add(one);
        panel.Children.Add(two);
        grid.Children.Add(panel);
    }

    private Ellipse GetPip(ref Grid portion, string name)
    {
        return (Ellipse)portion.FindName(name);
    }

    private void SetPortion(ref StackPanel domino, string name, int value)
    {
        Grid portion = (Grid)domino.FindName(name);
        int[] values = table[value];
        GetPip(ref portion, $"{name}.a").Opacity = values[0];
        GetPip(ref portion, $"{name}.b").Opacity = values[1];
        GetPip(ref portion, $"{name}.c").Opacity = values[2];
        GetPip(ref portion, $"{name}.d").Opacity = values[3];
        GetPip(ref portion, $"{name}.e").Opacity = values[4];
        GetPip(ref portion, $"{name}.f").Opacity = values[5];
        GetPip(ref portion, $"{name}.g").Opacity = values[6];
        GetPip(ref portion, $"{name}.h").Opacity = values[7];
        GetPip(ref portion, $"{name}.i").Opacity = values[8];
    }

    private void SetDomino(ref Grid grid, string name, string tile)
    {
        StackPanel domino = (StackPanel)grid.FindName(name);
        SetPortion(ref domino, $"{name}.upper", 0);
        SetPortion(ref domino, $"{name}.lower", 0);
        string[] values = tile.Split(',');
        int upper = int.Parse(values[0]);
        int lower = int.Parse(values[1]);
        SetPortion(ref domino, $"{name}.upper", upper);
        SetPortion(ref domino, $"{name}.lower", lower);
    }

    public void New(ref Grid grid)
    {
        _one = Shuffle(tiles.Count());
        _two = Shuffle(tiles.Count());
        Layout(ref grid);
        _turns = tiles.Count() - 1;
    }

    public void Choose(ref Grid grid)
    {
        if (_turns > 0)
        {
            SetDomino(ref grid, "one", tiles[_one[_turns]]);
            SetDomino(ref grid, "two", tiles[_two[_turns]]);
            _turns--;
        }
        else
        {
            New(ref grid);
        }
    }
}

There’s the use of two string[], the first is a list of all the positions a dot or “pip” can appear on Domino but there are also a few extra if you wanted to make each Domino go up to nine, the second is a list of all the possible combination of Domino Tiles there can be from blank to a pair of sixes. There is also int[][] which is a list of a list of numbers – this encodes all the combinations needed to display a set number of pips from 0 to 6 there are some positions that are never set as this is a three-by-three grid but is done this way to make it easier to understand.

There’s a Random number and set of two lists to store the values of a Domino in plus how many Turns have been made. There are many methods to make the displaying of a Domino work, the first is the Add method to add one of the “pips” which is represented by an Ellipse and set to a given Row and Column on a Grid. There is also a Portion method which represents the upper and lower part of a Domino with a given name and also has a Gradient background set – the pips are added using the previous Add method and are given a name composed of the given name and position of the pip as a letter from lost of all the positions a pip can appear on a Domino.

There’s a method to define a Domino called Domino and sets the Upper and Lower Portion, this method is used by the Layout to create the pair of Dominoes used in the example. Then there is a method to get a pip GetPip used by SetPortion to set the Opacity of a given pip – this will allow each value to be displayed as needed and uses the list of list of numbers that encode all the display combinations and the SetDomino method calls this to set the upper and lower parts of a Domino to the one of the valid values from the Tiles list. There are then methods to Shuffle a list of generated random numbers and start a New game and Choose a value from the shuffled list.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

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 Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="Accept" Label="New" Click="Choose_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a ViewBox containing a Grid Control that will be use for the contents of the game. The second block of XAML is is the CommandBar which contains New – to start a Game and Choose to display a Domino.

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

private void Choose_Click(object sender, RoutedEventArgs e)
{
	library.Choose(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 and Pick_Click is used to call the Choose 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

vs2017-local-machine

Step 14

After the Application has started running you can then select New to start then Choose to set each Domino to a random selection of all the possible values of a Domino

ran-lucky-dominoes

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 )

Google+ photo

You are commenting using your Google+ 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