Universal Windows Platform – Lucky Dice

Lucky Dice demonstrates how to use Grid Controls to display a pair of Dice with Random values of displayed Pips

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 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 const int size = 3;
    private readonly byte[][] table =
    {
                  // a, b, c, d, e, f, g, h, i
        new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0
        new byte[] { 0, 0, 0, 0, 1, 0, 0, 0, 0 }, // 1
        new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }, // 2
        new byte[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 }, // 3
        new byte[] { 1, 0, 1, 0, 0, 0, 1, 0, 1 }, // 4
        new byte[] { 1, 0, 1, 0, 1, 0, 1, 0, 1 }, // 5
        new byte[] { 1, 0, 1, 1, 0, 1, 1, 0, 1 }, // 6
    };

    private Random _random = new Random((int)DateTime.Now.Ticks);

    private void Add(ref Grid grid, int row, int column, byte opacity)
    {
        Ellipse dot = new Ellipse()
        {
            Fill = new SolidColorBrush(Colors.Black),
            Margin = new Thickness(5),
            Opacity = opacity
        };
        dot.SetValue(Grid.ColumnProperty, column);
        dot.SetValue(Grid.RowProperty, row);
        grid.Children.Add(dot);
    }

    private Grid Dice(int value)
    {
        Grid grid = new Grid()
        {
            Width = 100,
            Height = 100,
            Background = new SolidColorBrush(Colors.WhiteSmoke),
            Padding = new Thickness(5)
        };
        // 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, table[value][count]);
                count++;
            }
        }
        return grid;
    }

    private int Roll()
    {
        return _random.Next(1, 7);
    }

    public void New(ref Grid grid)
    {
        grid.Children.Clear();
        grid.Children.Add(Dice(Roll()));
    }
}

In the Library.cs there are using statements to include the necessary functionality. The byte[][] is a Table of values that will represent the pips to be displayed on the Dice. Random is used to create the numbers needed for the Dice, Add is used to place the Pips onto the Grid for each of the Dice. The Dice Method is used to create the Layout for the Dice themselves, the Roll Method picks the numbers for the Dice and New sets up a Grid passed in to give it the face of a single Dice

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>
		<Grid.RowDefinitions>
			<RowDefinition Height="*"/>
			<RowDefinition Height="Auto"/>
			<RowDefinition Height="*"/>
		</Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="Auto"/>
			<ColumnDefinition Width="*"/>
			<ColumnDefinition Width="Auto"/>
			<ColumnDefinition Width="*"/>
		</Grid.ColumnDefinitions>
		<Grid Margin="50" Grid.Column="1" Grid.Row="1" Name="DiceOne" Background="WhiteSmoke" Height="100" Width="100" Tapped="DiceOne_Tapped"/>
		<Grid Margin="50" Grid.Column="3" Grid.Row="1" Name="DiceTwo" Background="WhiteSmoke" Height="100" Width="100" Tapped="DiceTwo_Tapped"/>
	</Grid>
</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 within these are two other Grid elements to represent the Dice. The second block of XAML is is the CommandBar which contains the New – to reset the Dice

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)
{
	DiceOne.Children.Clear();
	DiceTwo.Children.Clear();
}

private void DiceOne_Tapped(object sender, RoutedEventArgs e)
{
	library.New(ref DiceOne);
}

private void DiceTwo_Tapped(object sender, RoutedEventArgs e)
{
	library.New(ref DiceTwo);
}

Below the MainPage() Method an instance of the Library Class is created, then in the New_Click Event DiceOne and DiceTwo are cleared, the DiceOne_Tapped and DiceTwo_Tapped Events will use the New Method to get the Dice value

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 on either of the squares to roll the dice or use New to reset them

ran-lucky-dice

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