Universal Windows Platform – GifView App

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Media.Imaging;

public class Library
{
    public async void Open(BitmapImage display)
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker()
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary
            };
            picker.FileTypeFilter.Add(".gif");
            StorageFile open = await picker.PickSingleFileAsync();
            if (open != null)
            {
                display.AutoPlay = false;
                await display.SetSourceAsync(await open.OpenReadAsync());
            }
        }
        finally
        {
            // Ignore Exceptions
        }
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="Open" Icon="OpenFile" Label="Open" Click="Open_Click"/>
	<AppBarButton Name="Play" Icon="Play" Label="Play" Click="{x:Bind Display.Play}"/>
	<AppBarButton Name="Stop" Icon="Stop" Label="Stop" Click="{x:Bind Display.Stop}"/>
</CommandBar>
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None">
	<Image.Source>
		<BitmapImage x:Name="Display"/>
	</Image.Source>
</Image>

It should appear as such:

xaml-gifview-app

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

Library library = new Library();

private void Open_Click(object sender, RoutedEventArgs e)
{
	library.Open(Display);
}

It should then appear as such:

code-gifview-app

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-gifview-app

Step 16

After the Application has started running you can then select Open launch a FileOpenPicker then you need to select an animated gif file to open. Once done then you can select Play to start the image animating and to end this animation select Stop

ran-gifview-app

You can use the following sample animated GIF

animation

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Slide Game

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Linq;
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 Piece : Grid
{
    public Piece(int index)
    {
        this.Background = new SolidColorBrush(Colors.Black);
        Rectangle rect = new Rectangle()
        {
            Stroke = new SolidColorBrush(Colors.Gray),
        };
        TextBlock text = new TextBlock()
        {
            FontSize = 30,
            Text = index.ToString(),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            Foreground = new SolidColorBrush(Colors.White)
        };
        this.Children.Add(rect);
        this.Children.Add(text);
    }

    public int Row { get; set; }
    public int Column { get; set; }
}

public class Library
{
    private const int size = 4;
    private int moves = 0;
    private int[,] board = new int[size, size];
    private List<int> values;
    private Random random = new Random((int)DateTime.Now.Ticks);

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

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

    private bool Valid(int row, int column)
    {
        if (row < 0 || column < 0 || row > 3 || column > 3)
        {
            return false;
        }
        return (board[row, column] == 0);
    }

    private bool Check()
    {
        int previous = board[0, 0];
        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                if (board[row, column] > previous)
                {
                    return false;
                }
                previous = board[row, column];
            }
        }
        return true;
    }

    private void Move(Canvas canvas, Piece piece, int row, int column)
    {
        moves++;
        board[row, column] = board[piece.Row, piece.Column];
        board[piece.Row, piece.Column] = 0;
        piece.Row = row;
        piece.Column = column;
        Layout(canvas);
        if (Check())
        {
            Show($"Correct In {moves} Moves", "Slide Game");
        }
    }

    private void Layout(Canvas canvas)
    {
        canvas.Children.Clear();
        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                if (board[row, column] > 0)
                {
                    int index = board[row, column];
                    Piece piece = new Piece(index)
                    {
                        Width = canvas.Width / size,
                        Height = canvas.Height / size,
                        Row = row,
                        Column = column
                    };
                    Canvas.SetTop(piece, row * (canvas.Width / size));
                    Canvas.SetLeft(piece, column * (canvas.Width / size));
                    piece.PointerReleased += (object sender, PointerRoutedEventArgs e) =>
                    {
                        piece = (Piece)sender;
                        if (Valid(piece.Row - 1, piece.Column))
                        {
                            Move(canvas, piece, piece.Row - 1, piece.Column);
                        }
                        else if (Valid(piece.Row, piece.Column + 1))
                        {
                            Move(canvas, piece, piece.Row, piece.Column + 1);
                        }
                        else if (Valid(piece.Row + 1, piece.Column))
                        {
                            Move(canvas, piece, piece.Row + 1, piece.Column);
                        }
                        else if (Valid(piece.Row, piece.Column - 1))
                        {
                            Move(canvas, piece, piece.Row, piece.Column - 1);
                        }
                    };
                    canvas.Children.Add(piece);
                }
            }
        }
    }

    public void New(ref Canvas canvas)
    {
        int index = 1;
        values = Shuffle(1, board.Length);
        values.Insert(0, 0);
        for (int row = 0; row < size; row++)
        {
            for (int column = 0; column < size; column++)
            {
                board[row, column] = values[index++];
                if (index == size * size) index = 0;
            }
        }
        Layout(canvas);
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<Canvas Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-slide-game

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

Library library = new Library();

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

It should then appear as such:

code-slide-game

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-slide-game

Step 16

After the Application has started running you can then select New to start the game then you need to select a Piece nearest the empty slot to move it into that slot and then continue until all the Numbers are in the correct order from Left to Right from 15 to 1 to then win the game.

ran-slide-game

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Chase Game

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Popups;
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 = 4;
    private const int on = 1;
    private const int off = 0;

    private readonly Color lightOn = Colors.White;
    private readonly Color lightOff = Colors.Black;
    
    private int[,] board = new int[size, size];
    private DispatcherTimer timer = null;
    private Random random = new Random((int)DateTime.Now.Ticks);
    private List<int> positions = new List<int>();
    private int counter = 0;
    private int turns = 0;
    private int hits = 0;
    private int wait = 0;
    private bool waiting = false;
    private bool lost = false;

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

    private Rectangle Get(Color foreground)
    {
        Rectangle rect = new Rectangle()
        {
            Height = 50,
            Width = 50,
            Fill = new SolidColorBrush(foreground)
        };
        return rect;
    }

    private void Set(ref Grid grid, int row, int column)
    {
        Button button = (Button)grid.Children.Single(
                       w => Grid.GetRow((Button)w) == row
                       && Grid.GetColumn((Button)w) == column);
        button.Content = Get(board[row, column] == on ? lightOn : lightOff);
    }

    private void Add(Grid grid, TextBlock text, int row, int column, int index)
    {
        string name = string.Empty;
        Button button = new Button()
        {
            Height = 75,
            Width = 75,
            Content = Get(lightOff)
        };
        button.Click += (object sender, RoutedEventArgs e) =>
        {
            if (!lost)
            {
                button = (Button)sender;
                row = (int)button.GetValue(Grid.RowProperty);
                column = (int)button.GetValue(Grid.ColumnProperty);
                if (board[row, column] == on)
                {
                    board[row, column] = off;
                    Set(ref grid, row, column);
                    hits++;
                }
                else
                {
                    lost = true;
                }
            }
            else
            { 
                text.Text = $"Game Over in {turns} Turns!";
            }
        };
        button.SetValue(Grid.ColumnProperty, column);
        button.SetValue(Grid.RowProperty, row);
        grid.Children.Add(button);
    }

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

    private void Reset(Grid grid)
    {
        for (int column = 0; column < size; column++)
        {
            for(int row = 0; row < size; row++)
            {
                board[row, column] = off;
                Set(ref grid, row, column);
            }
        }        
        hits = 0;
        counter = 0;
        wait = Shuffle(3, 7, 1).First();
        waiting = true;
    }

    private void Choose(Grid grid)
    {
        int row = 0;
        int column = 0;
        positions = Shuffle(0, board.Length, board.Length);
        for (int i = 0; i < size; i++)
        {
            column = positions[i] % size;
            switch(positions[i])
            {
                case int n when n < 4:
                    row = 0;
                    break;
                case int n when n >= 4 && n < 8:
                    row = 1;
                    break;
                case int n when n >= 8 && n < 12:
                    row = 2;
                    break;
                case int n when n >= 12 && n <= 15:
                    row = 3;
                    break;
            }
            board[row, column] = on;
            Set(ref grid, row, column);
        }
        counter = 0;
        wait = 0;
        waiting = false;
    }

    private void Timer(Grid grid, TextBlock text)
    {
        if(timer != null)
        {
            timer.Stop();
            timer = null;
        }
        timer = new DispatcherTimer()
        {
            Interval = TimeSpan.FromSeconds(1)
        };
        timer.Tick += (object sender, object e) =>
        {
            if (!lost)
            {
                int countdown = 0;
                if (waiting)
                {
                    countdown = (wait - counter) + 1;
                    text.Text = $"Waiting {countdown}";
                    if (countdown == 0)
                    {
                        text.Text = string.Empty;
                        Choose(grid);
                    }
                }
                else
                {
                    countdown = (size - counter) + 1;
                    text.Text = $"Solve In {countdown}";
                    if (countdown == 0)
                    {
                        if (hits == size)
                        {
                            turns++;
                            text.Text = string.Empty;
                            Reset(grid);
                        }
                        else
                        {
                            lost = true;
                            text.Text = $"Game Over in {turns} Turns!";
                        }
                    }
                }
                counter++;
            }
        };
        timer.Start();
    }

    public void New(ref Grid grid, ref TextBlock text)
    {
        lost = false;
        waiting = true;
        wait = 3;
        turns = 1;
        Layout(ref grid, ref text);
        Timer(grid, text);
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<StackPanel VerticalAlignment="Center">
	<TextBlock Name="Label" HorizontalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/>
	<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>
</StackPanel>

It should appear as such:

xaml-chase-game

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

Library library = new Library();

private void New_Click(object sender, RoutedEventArgs e)
{
	library.New(ref Display, ref Label);
}

It should then appear as such:

code-chase-game

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-chase-game

Step 16

After the Application has started running you can then select New to start the game then begins a timer which will be displayed on the Label with Waiting Countdown and once complete a Random selection of Squares will be lit up White and you need to hit them all within the Solve Countdown otherwise it will be Game Over and you can keep going for as many Turns as you can!

ran-chase-game

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Flags Game

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Flag
{
    public enum Designs
    {
        HorizonalStripes = 1,
        VerticalStripes = 2
    };
    public Designs Design { get; set; }
    public string Name { get; set; }
    public string[] Colours { get; set; }
}

public class Library
{
    private const int size = 3;

    private List<Flag> flags = new List<Flag>()
    {
        new Flag() {
            Name = "Armenia",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE1018", "002984", "EF7b21" }
        },
        new Flag()
        {
            Name = "Austria",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE1018", "FFFFFF", "DE1018" }
        },
        new Flag()
        {
            Name = "Belgium",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "000000", "FFEF08", "DE1018" }
        },
        new Flag()
        {
            Name = "Bulgaria",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "FFFFFF", "109452", "DE1018" }
        },
        new Flag()
        {
            Name = "Estonia",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "295AA5", "000000", "FFFFFF" }
        },
        new Flag()
        {
            Name = "France",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "080863", "FFFFFF", "D60818" }
        },
        new Flag() {
            Name = "Gabon",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "109452", "FFEF08", "002984" }
        },
        new Flag()
        {
            Name = "Germany",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "000000", "DE0008", "FFDE08" }
        },
        new Flag()
        {
            Name = "Guinea",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "DE1018", "FFEF08", "109452" }
        },
        new Flag()
        {
            Name = "Hungary",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE0008", "FFFFFF", "087B39" }
        },
        new Flag()
        {
            Name = "Ireland",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "007339", "FFFFFF", "E76300" }
        },
        new Flag()
        {
            Name = "Italy",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "109452", "FFFFFF", "DE1018" }
        },
        new Flag()
        {
            Name = "Lithuania",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "FFEF08", "109452", "DE1018" }
        },
        new Flag()
        {
            Name = "Luxembourg",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE1018", "FFFFFF", "2984B5" }
        },
        new Flag()
        {
            Name = "Mali",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "109452", "FFEF08", "DE1018" }
        },
        new Flag()
        {
            Name = "Netherlands",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE1018", "FFFFFF", "002984" }
        },
        new Flag()
        {
            Name = "Nigeria",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "087B39", "FFFFFF", "087B39" }
        },
        new Flag()
        {
            Name = "Romania",
            Design = Flag.Designs.VerticalStripes,
            Colours = new string[] { "002984", "FFEF08", "DE1018" }
        },
        new Flag()
        {
            Name = "Russia",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "FFFFFF", "0852A5", "DE1018" }
        },
        new Flag()
        {
            Name = "Sierra Leone",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "5AB521", "FFFFFF", "0852A5" }
        },
        new Flag()
        {
            Name = "Yemen",
            Design = Flag.Designs.HorizonalStripes,
            Colours = new string[] { "DE1018", "FFFFFF", "000000" }
        }
    };

    private Random random = new Random((int)DateTime.Now.Ticks);
    private List<int> indexes = new List<int>();
    private List<int> choices = new List<int>();
    private int turns = 0;
    private string country = string.Empty;
    private bool lost = false;

    public static Color ConvertHexToColor(string hex)
    {
        hex = hex.Remove(0, 1);
        byte a = hex.Length == 8 ? Byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber) : (byte)255;
        byte r = Byte.Parse(hex.Substring(hex.Length - 6, 2), NumberStyles.HexNumber);
        byte g = Byte.Parse(hex.Substring(hex.Length - 4, 2), NumberStyles.HexNumber);
        byte b = Byte.Parse(hex.Substring(hex.Length - 2), NumberStyles.HexNumber);
        return Color.FromArgb(a, r, g, b);
    }

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

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

    private Viewbox GetFlag(ref string name, int index)
    {
        int pos = indexes[index];
        Flag flag = flags[pos];
        name = flag.Name;
        bool horizontal = flag.Design == Flag.Designs.HorizonalStripes;
        StackPanel panel = new StackPanel()
        {
            Height = 120,
            Width = 120,
            Orientation = horizontal ? Orientation.Vertical : Orientation.Horizontal
        };
        for(int f = 0; f < 3; f++)
        {
            Rectangle rect = new Rectangle()
            {
                Width = horizontal ? 120 : 40,
                Height = horizontal ? 40 : 120,
                Fill = new SolidColorBrush(ConvertHexToColor($"#FF{flag.Colours[f]}"))
            };
            panel.Children.Add(rect);
        }
        Viewbox view = new Viewbox()
        {
            Height = 40,
            Width = 40,
            Child = panel
        };
        return view;
    }

    private void SetButton(ref Grid grid, string name, bool show)
    {
        Button button = (Button)grid.FindName(name);
        button.Opacity = show ? 1 : 0;
    }

    private void Add(Grid grid, TextBlock text, int row, int column, int index)
    {
        string name = string.Empty;
        Viewbox view = GetFlag(ref name, index);
        Button button = new Button()
        {
            Name = name,
            Height = 75,
            Width = 75,
            Content = view
        };
        button.Click += (object sender, RoutedEventArgs e) =>
        {
            if(!lost)
            {
                string current = ((Button)sender).Name;
                if (country == current)
                {
                    SetButton(ref grid, current, false);
                    if (turns < 9)
                    {                        
                        Choose(ref grid, ref text);
                    }
                    else
                    {
                        text.Text = string.Empty;
                        Show("You Won!", "Flags Game");
                    }
                }
                else
                {
                    lost = true;
                }
            }
            if(lost)
            {
                Show("Game Over!", "Flags Game");
            }
        };
        button.SetValue(Grid.ColumnProperty, column);
        button.SetValue(Grid.RowProperty, row);
        grid.Children.Add(button);
    }

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

    private void Choose(ref Grid grid, ref TextBlock text)
    {
        int choice = choices[turns];
        int index = indexes[choice];
        country = flags[index].Name;
        text.Text = country;
        turns++;
    }

    public void New(ref Grid grid, ref TextBlock text)
    {
        lost = false;
        turns = 0;
        text.Text = string.Empty;
        indexes = Shuffle(0, flags.Count);
        choices = Shuffle(0, 9);
        Layout(ref grid, ref text);
        Choose(ref grid, ref text);
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<StackPanel VerticalAlignment="Center">
	<TextBlock Name="Label" HorizontalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/>
	<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>
</StackPanel>

It should appear as such:

xaml-flags-game

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

Library library = new Library();

private void New_Click(object sender, RoutedEventArgs e)
{
	library.New(ref Display, ref Label);
}

It should then appear as such:

code-flags-game

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-flags-game

Step 16

After the Application has started running you can then select New to start the game then nine Flags will be displayed and on the Label there will be displayed a Country and you need to guess the correct Flag if you guessed incorrectly then it will be Game Over otherwise you can keep going until you get all nine, then you Win!

ran-flags-game

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Codes Game

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

public class CommandHandler : ICommand
{
    public event EventHandler CanExecuteChanged = null;
    private Action _action;

    public CommandHandler(Action action)
    {
        _action = action;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _action();
    }
}

public class Code : INotifyPropertyChanged
{
    private int _value;
    private SolidColorBrush _foreGround;
    private SolidColorBrush _backGround;

    public virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public int Index { get; set; }
    public int Value { get { return _value; } set { _value = value; OnPropertyChanged("Value"); } }
    public SolidColorBrush Foreground { get { return _foreGround; } set { _foreGround = value; OnPropertyChanged("Foreground"); } }
    public SolidColorBrush Background { get { return _backGround; } set { _backGround = value; OnPropertyChanged("Background"); } }
    public Func<int, int> Action { get; set; }
    public ICommand Command { get { return new CommandHandler(() => this.Action(Index)); } }
    public event PropertyChangedEventHandler PropertyChanged;
}

public class Library
{
    private ObservableCollection<Code> codes = new ObservableCollection<Code>();
    private Random random = new Random((int)DateTime.Now.Ticks);
    private List<int> numbers = new List<int>();
    private int turns = 0;

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

    private Code GetCode(int value, int index)
    {
        return new Code()
        {
            Action = (int i) =>
            {
                Code code = codes[i];
                code.Value = (code.Value == 4) ? 1 : code.Value + 1;
                code.Foreground = new SolidColorBrush(Colors.Black);
                code.Background = new SolidColorBrush(Colors.White);
                return code.Value;
            },
            Index = index,
            Value = value,
            Foreground = new SolidColorBrush(Colors.Black),
            Background = new SolidColorBrush(Colors.White)
        };
    }

    private bool Check(int number, int index)
    {
        Code code = codes[index];
        if (number == code.Value)
        {
            code.Foreground = new SolidColorBrush(Colors.Black);
            code.Background = new SolidColorBrush(Colors.White);
            return true;
        }
        else
        {
            code.Foreground = new SolidColorBrush(Colors.White);
            code.Background = new SolidColorBrush(Colors.Black);
            return false;
        }
    }

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

    public void New(ref ItemsControl items)
    {
        turns = 0;
        codes.Clear();
        for (int i = 0; i < 4; i++)
        {
            codes.Add(GetCode(i + 1, i));
        }
        items.ItemsSource = codes;
        numbers = Shuffle(1, 4);
    }

    public void Accept(ref ItemsControl items)
    {
        int index = 0;
        int correct = 0;
        foreach (int number in numbers)
        {
            if (Check(number, index))
            {
                correct++;
            }
            index++;
        }
        turns++;
        if (correct == 4)
        {
            Show($"You got all the numbers correct in {turns} turns!", "Codes Game");
            New(ref items);
        }
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="Accept" Label="Accept" Click="Accept_Click"/>
</CommandBar>
<Grid HorizontalAlignment="Center" Height="300" Width="300">
	<Viewbox>
		<ItemsControl Name="Display">
			<ItemsControl.ItemTemplate>
				<DataTemplate>
					<Button Height="100" Width="100" Background="{Binding Background}" Command="{Binding Command}">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
						FontSize="60" Foreground="{Binding Foreground}" Text="{Binding Value}"/>
					</Button>
				</DataTemplate>
			</ItemsControl.ItemTemplate>
			<ItemsControl.ItemsPanel>
				<ItemsPanelTemplate>
					<StackPanel Orientation="Horizontal"/>
				</ItemsPanelTemplate>
			</ItemsControl.ItemsPanel>
		</ItemsControl>
	</Viewbox>
</Grid>

It should appear as such:

xaml-codes-game

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

Once in the Code View, below the 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 Higher_Click(object sender, RoutedEventArgs e)
{
	library.Higher(ref Display);
}

private void Lower_Click(object sender, RoutedEventArgs e)
{
	library.Lower(ref Display);
}

It should then appear as such:

code-codes-game

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-codes-game

Step 16

After the Application has started running you can then select New to start the game then four numbers between 1 and 4 then you need to guess the secret combination by selecting those numbers you can go through them to find that combination and once happy select Accept if you guessed a number incorrectly then it will turn Black with White text or if correct it will remain the same and if all of them are the same as the secret combination of numbers then you win.

ran-codes-game

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – High Or Low

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
{
    private const string club = "M 19.5155,51.3 C 17.1155,50.1 15.9155,43.7 " +
        "21.5155,43.7 C 27.1155,43.7 25.9155,50.1 23.5155,51.3 C 25.1155,50.1 " +
        "30.3155,48.5 30.3155,54.1 C 30.3155,59.7 23.1155,59.3 22.7155,54.9 L " +
        "21.9155,54.9 C 21.9155,54.9 22.3155,59.3 23.5155,61.3 L 19.5155,61.3 C " +
        "20.7155,59.3 21.1155,54.9 21.1155,54.9 L 20.3155,54.9 C 19.9155,59.3 " +
        "12.3155,59.7 12.3155,54.1 C 12.3155,49.3 17.9155,50.1 19.5155,51.3 z";
    private const string diamond = "M 170.1155,199.8 L 177.3155,191 L 184.5155,199.4 " +
        "L 177.3155,209 L 170.1155,199.8 z";
    private const string heart = "M 99.5,99.75 C 99.5,93.75 89.5,81.75 79.5,81.75 C " +
        "69.5,81.75 59.5,89.75 59.5,103.75 C 59.5,125.75 91.5,161.75 99.5,171.75 C " +
        "107.5,161.75 139.5,125.75 139.5,103.75 C 139.5,89.75 129.5,81.75 119.5,81.75 C " +
        "109.45012,81.75 99.5,93.75 99.5,99.75 z";
    private const string spade = "M 21.1155,43.3 C 17.9155,48.1 13.7155,50.9 13.7155,54.9 " +
        "C 13.7155,58.5 15.7155,59.3 16.9155,59.3 C 18.5155,59.3 19.7155,58.5 19.7155,55.7 " +
        "C 19.7155,54.9 20.5155,54.9 20.5155,55.7 C 20.5155,59.7 19.7155,59.7 18.9155,61.7 " +
        "L 23.3155,61.7 C 22.5155,59.7 21.7155,59.7 21.7155,55.7 C 21.7155,54.9 22.5155,54.9 " +
        "22.5155,55.7 C 22.5155,58.9 23.7155,59.3 25.3155,59.3 C 26.5155,59.3 28.9155,58.5 " +
        "28.9155,54.9 C 28.9155,50.84784 24.3155,48.1 21.1155,43.3 z";

    private string[] card_pips = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r" };
    private string[] card_values = { "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q" };

    private int[][] table =
    {
        //          a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 0
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 1
        new int[] { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 2
        new int[] { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, // 3
        new int[] { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 }, // 4
        new int[] { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 }, // 5
        new int[] { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0 }, // 6
        new int[] { 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0 }, // 7
        new int[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 }, // 8
        new int[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0 }, // 9
        new int[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0 }, // 10
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 11
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 12
        new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 13
    };

    private List<int> deck = new List<int>();
    private Random random = new Random((int)DateTime.Now.Ticks);
    private int turn;
    private int value;
    private bool lost;

    private Geometry GetGeometry(string data)
    {
        return (Geometry)XamlReader.Load(
            $"<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>{data}</Geometry>"
        );
    }

    private Path AddPip(string symbol, Color color, double height, int margin, string name)
    {
        return new Path()
        {
            Name = name,
            Data = GetGeometry(symbol),
            Fill = new SolidColorBrush(color),
            Height = height,
            Stretch = Stretch.Uniform,
            Margin = new Thickness(margin),
            Opacity = 0
        };
    }

    private void Add(ref Grid grid, int row, int rowspan, int column, string symbol, Color color, string name)
    {
        bool flip = row > 2;
        Path pip = AddPip(symbol, color, 100, 5, name);
        if (flip)
        {
            pip.RenderTransform = new ScaleTransform() { ScaleY = -1, CenterY = 50 };
        }
        pip.SetValue(Grid.RowProperty, row);
        pip.SetValue(Grid.RowSpanProperty, rowspan);
        pip.SetValue(Grid.ColumnProperty, column);
        grid.Children.Add(pip);
    }

    private void AddItem(ref Grid grid, int row, int column, string symbol, Color color, string value, bool flip, string name)
    {
        StackPanel item = new StackPanel();
        Path pip = AddPip(symbol, color, 40, 0, $"{name}.pip");
        if (flip)
        {
            pip.RenderTransform = new ScaleTransform() { ScaleY = -1, CenterY = 20 };
        }
        item.Children.Add(pip);
        item.Children.Add(new TextBlock()
        {
            Name = $"{name}.num",
            HorizontalAlignment = HorizontalAlignment.Center,
            Text = value,
            Foreground = new SolidColorBrush(color),
            FontSize = 40,
            Opacity = 0
        });
        item.SetValue(Grid.ColumnProperty, column);
        item.SetValue(Grid.RowProperty, row);
        grid.Children.Add(item);
    }

    private void AddFace(ref Grid grid, int row, int rowspan, int column, int colspan, Color color, string value, string name)
    {
        TextBlock face = new TextBlock()
        {
            Name = name,
            HorizontalAlignment = HorizontalAlignment.Center,
            Text = value,
            Foreground = new SolidColorBrush(color),
            FontSize = 300
        };
        face.SetValue(Grid.RowProperty, row);
        face.SetValue(Grid.RowSpanProperty, rowspan);
        face.SetValue(Grid.ColumnProperty, column);
        face.SetValue(Grid.ColumnSpanProperty, colspan);
        face.Opacity = 0;
        grid.Children.Add(face);
    }

    private void SetPip(ref Grid grid, string symbol, Color color, string name, int opacity)
    {
        Path pip = (Path)grid.FindName(name);
        pip.Data = GetGeometry(symbol);
        pip.Fill = new SolidColorBrush(color);
        pip.Opacity = opacity;
    }

    private void SetItem(ref Grid grid, string symbol, Color color, string value, string name)
    {
        Path pip = (Path)grid.FindName($"{name}.pip");
        pip.Data = GetGeometry(symbol);
        pip.Fill = new SolidColorBrush(color);
        pip.Opacity = 1;
        TextBlock num = (TextBlock)grid.FindName($"{name}.num");
        num.Foreground = new SolidColorBrush(color);
        num.Text = value.ToString();
        num.Opacity = 1;
    }

    private void SetFace(ref Grid grid, Color color, string value, string name, int opacity)
    {
        TextBlock face = (TextBlock)grid.FindName(name);
        face.Text = value;
        face.Foreground = new SolidColorBrush(color);
        face.Opacity = opacity;
    }

    private Viewbox Card(string name, Color back)
    {
        string suit = club;
        Grid card = new Grid()
        {
            Name = name,
            Margin = new Thickness(10),
            Padding = new Thickness(10, 20, 10, 10),
            CornerRadius = new CornerRadius(10),
            BorderThickness = new Thickness(5),
            BorderBrush = new SolidColorBrush(Colors.Black),
            Background = new SolidColorBrush(back),
        };
        for (int c = 0; c < 5; c++)
        {
            card.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(80) });
        }
        for (int r = 0; r < 6; r++)
        {
            card.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100) });
        }
        AddItem(ref card, 0, 0, suit, Colors.Black, "1", false, $"{name}.topleft");
        AddItem(ref card, 6, 0, suit, Colors.Black, "1", true, $"{name}.bottomleft");
        AddItem(ref card, 6, 4, suit, Colors.Black, "1", true, $"{name}.bottomright");
        AddItem(ref card, 0, 4, suit, Colors.Black, "1", false, $"{name}.topright");
        int count = 0;
        for (int i = 1; i < 5; i++)
        {
            Add(ref card, i, 1, 1, suit, Colors.Black, $"{name}.{card_pips[count]}");
            count++;
        }
        Add(ref card, 2, 2, 1, suit, Colors.Black, $"{name}.{card_pips[count]}");
        count++;
        for (int i = 1; i < 5; i++)
        {
            Add(ref card, i, 1, 2, suit, Colors.Black, $"{name}.{card_pips[count]}");
            count++;
        }
        for (int i = 1; i < 4; i++)
        {
            Add(ref card, i, 2, 2, suit, Colors.Black, $"{name}.{card_pips[count]}");
            count++;
        }
        Add(ref card, 2, 2, 3, suit, Colors.Black, $"{name}.{card_pips[count]}");
        count++;
        for (int i = 1; i < 5; i++)
        {
            Add(ref card, i, 1, 3, suit, Colors.Black, $"{name}.{card_pips[count]}");
            count++;
        }
        AddFace(ref card, 1, 4, 1, 3, Colors.Black, "A", $"{name}.{card_pips[count]}");
        Viewbox box = new Viewbox()
        {
            Child = card
        };
        return box;
    }

    private int SetCard(ref Grid grid, string name, int number)
    {
        int index = (number % 13);
        string suit = club;
        switch (number)
        {
            case int c when (number > 1 && number <= 13):
                suit = club;
                break;
            case int d when (number > 14 && number <= 26):
                suit = diamond;
                break;
            case int h when (number >= 27 && number <= 39):
                suit = heart;
                break;
            case int s when (number >= 40 && number <= 52):
                suit = spade;
                break;
        }
        Color color = (suit == heart || suit == diamond) ? Colors.Red : Colors.Black;
        string value = card_values[index];
        Grid card = (Grid)grid.FindName(name);
        card.Background = new SolidColorBrush(Colors.White);
        SetItem(ref card, suit, color, value, $"{name}.topleft");
        SetItem(ref card, suit, color, value, $"{name}.bottomleft");
        SetItem(ref card, suit, color, value, $"{name}.bottomright");
        SetItem(ref card, suit, color, value, $"{name}.topright");
        int[] values = table[index];
        SetPip(ref card, suit, color, $"{name}.a", values[0]);
        SetPip(ref card, suit, color, $"{name}.b", values[1]);
        SetPip(ref card, suit, color, $"{name}.c", values[2]);
        SetPip(ref card, suit, color, $"{name}.d", values[3]);
        SetPip(ref card, suit, color, $"{name}.e", values[4]);
        SetPip(ref card, suit, color, $"{name}.f", values[5]);
        SetPip(ref card, suit, color, $"{name}.g", values[6]);
        SetPip(ref card, suit, color, $"{name}.h", values[7]);
        SetPip(ref card, suit, color, $"{name}.i", values[8]);
        SetPip(ref card, suit, color, $"{name}.j", values[9]);
        SetPip(ref card, suit, color, $"{name}.k", values[10]);
        SetPip(ref card, suit, color, $"{name}.l", values[11]);
        SetPip(ref card, suit, color, $"{name}.m", values[12]);
        SetPip(ref card, suit, color, $"{name}.n", values[13]);
        SetPip(ref card, suit, color, $"{name}.o", values[14]);
        SetPip(ref card, suit, color, $"{name}.p", values[15]);
        SetPip(ref card, suit, color, $"{name}.q", values[16]);
        SetFace(ref card, color, value, $"{name}.r", values[17]);
        return index;
    }

    private void Layout(ref Grid grid)
    {
        Grid container = new Grid()
        {
            Width = 150
        };
        container.Children.Add(Card("card", Colors.Red));
        grid.Children.Add(container);
    }

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

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

    public void New(ref Grid grid)
    {
        turn = 0;
        lost = false;
        deck = Shuffle(52);
        Layout(ref grid);
        value = SetCard(ref grid, "card", deck[turn]);
    }

    public void Higher(ref Grid grid)
    {
        int higher = 0;
        if (turn < deck.Count && !lost)
        {
            turn++;
            higher = SetCard(ref grid, "card", deck[turn]);
            lost = (higher < value);
        }
        if(lost)
        {
            ShowAsync($"Game Over : Turn {turn} - Card Was Lower!", "High Or Low");
        }
    }

    public void Lower(ref Grid grid)
    {
        int lower = 0;
        if (turn < deck.Count && !lost)
        {
            turn++;
            lower = SetCard(ref grid, "card", deck[turn]);
            lost = (lower > value);
        }
        if (lost)
        {
            ShowAsync($"Game Over : Turn {turn} - Card Was Higher!", "High Or Low");
        }
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Name="Higher" Icon="Add" Label="Higher" Click="Higher_Click"/>
	<AppBarButton Name="Lower" Icon="Remove" Label="Lower" Click="Lower_Click"/>
</CommandBar>
<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-high-or-low

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

Once in the Code View, below the 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 Higher_Click(object sender, RoutedEventArgs e)
{
	library.Higher(ref Display);
}

private void Lower_Click(object sender, RoutedEventArgs e)
{
	library.Lower(ref Display);
}

It should then appear as such:

code-high-or-low

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-high-or-low

Step 16

After the Application has started running you can then select Newto start the game then select either Higher or Lower to guess if the next Card is has High or Low value compared to the previous one if you guess incorrectly then you lose.

ran-high-or-low

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Deal Or Not

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
{
    private double[] box_values = 
    {
            0.01, 0.10, 0.50, 1, 5, 10, 50, 100, 250, 500, 750,
            1000, 3000, 5000, 10000, 15000, 20000, 35000, 50000, 75000, 100000, 250000
    };
    private string[] box_colors = 
    {
        "0026ff", "0039ff", "004dff", "0060ff", "0073ff", "0086ff", "0099ff", "0099ff", "0099ff", "00acff", "00bfff",
        "ff5900", "ff4d00", "ff4000", "ff3300", "ff2600", "ff2600", "ff2600", "ff2600", "ff1a00", "ff1c00", "ff0d00",
    };
    private string[] box_names = {
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v"
    };

    private Random random = new Random((int)DateTime.Now.Ticks);
    private List<double> amounts = new List<double>();
    private double amount;
    private bool dealt;
    private int turn;

    public static Color ConvertHexToColor(string hex)
    {
        hex = hex.Remove(0, 1);
        byte a = hex.Length == 8 ? Byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber) : (byte)255;
        byte r = Byte.Parse(hex.Substring(hex.Length - 6, 2), NumberStyles.HexNumber);
        byte g = Byte.Parse(hex.Substring(hex.Length - 4, 2), NumberStyles.HexNumber);
        byte b = Byte.Parse(hex.Substring(hex.Length - 2), NumberStyles.HexNumber);
        return Color.FromArgb(a, r, g, b);
    }

    private async Task<ContentDialogResult> ShowDialogAsync(string primary, string secondary, object content)
    {
        ContentDialog dialog = new ContentDialog()
        {
            Title = "Deal Or Not",
            PrimaryButtonText = primary,
            SecondaryButtonText = secondary,
            Content = content
        };
        return await dialog.ShowAsync();
    }

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

    private Grid GetAmount(double value, Color background)
    {       
        Grid grid = new Grid()
        {
            Background = new SolidColorBrush(background)
        };
        TextBlock text = new TextBlock()
        {
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            Foreground = new SolidColorBrush(Colors.White),
            Margin = new Thickness(10),
            FontSize = 33,
            Text = String.Format(new CultureInfo("en-GB"), "{0:c}", value)
        };
        grid.Children.Add(text);
        return grid;
    }

    private double GetOffer()
    {
        int count = 0;
        double total = 0.0;
        foreach (double amount in amounts)
        {
            total += amount;
            count++;
        }
        double average = total / count;
        double offer = (average * turn) / 10;
        return Math.Round(offer, 0);
    }

    private Color GetBackground(double amount)
    {
        int position = 0;
        while (amount != box_values[position])
        {
            position++;
        }
        return ConvertHexToColor($"#ff{box_colors[position]}");
    }

    private async void Pick(Button button, string name)
    {
        if (turn < box_names.Length && !dealt)
        {
            double offer = 0;
            button.Opacity = 0;
            amount = amounts[Array.IndexOf(box_names, name)];
            await ShowDialogAsync("OK", string.Empty, GetAmount(amount, GetBackground(amount)));
            if (turn % 5 == 0 && turn > 1)
            {
                offer = GetOffer();
                ContentDialogResult result = await ShowDialogAsync("Deal", "Not", GetAmount(offer, Colors.Black));
                if (result == ContentDialogResult.Primary)
                {
                    amount = offer;
                    dealt = true;
                }
            }
            turn++;
        }
        if(turn == box_names.Length || dealt)
        {
            object content = dealt ? GetAmount(amount, Colors.Black) : GetAmount(amount, GetBackground(amount));
            await ShowDialogAsync("Game Over", string.Empty, content);
        }
    }

    private void AddBox(ref StackPanel panel, string name, int value)
    {
        Button button = new Button()
        {
            Name = $"box.{name}"
        };
        button.Click += (object sender, RoutedEventArgs e) =>
        {
            Pick((Button)sender, name);
        };
        StackPanel box = new StackPanel()
        {
            Width = 100
        };
        Rectangle lid = new Rectangle()
        {
            Height = 10,
            Fill = new SolidColorBrush(Colors.DarkRed)
        };
        Grid front = new Grid()
        {
            Height = 75,
            Background = new SolidColorBrush(Colors.Red)
        };
        Grid label = new Grid()
        {
            Width = 50,
            Background = new SolidColorBrush(Colors.White),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };
        TextBlock text = new TextBlock()
        {
            TextAlignment = TextAlignment.Center,
            FontWeight = FontWeights.Bold,
            Foreground = new SolidColorBrush(Colors.Black),
            FontSize = 32,
            Text = value.ToString()
        };
        label.Children.Add(text);
        front.Children.Add(label);
        box.Children.Add(front);
        button.Content = box;
        panel.Children.Add(button);
    }

    private StackPanel Boxes()
    {
        StackPanel panel = new StackPanel();
        int[] rows = { 5, 6, 6, 5 };
        int count = 0;
        for (int r = 0; r < 4; r++)
        {
            StackPanel places = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Center
            };
            for (int c = 0; c < rows[r]; c++)
            {
                AddBox(ref places, box_names[count], count + 1);
                count++;
            }
            panel.Children.Add(places);
        }
        return panel;
    }

    private void Layout(ref Grid grid)
    {
        grid.Children.Clear();
        Viewbox view = new Viewbox()
        {
            Child = Boxes()
        };
        grid.Children.Add(view);
    }

    public void New(ref Grid grid)
    {
        turn = 0;        
        dealt = false;
        List<int> positions = Shuffle(22);
        amounts = new List<double>();
        foreach (int position in positions)
        {
            amounts.Add(box_values[position]);
        }
        Layout(ref grid);
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-deal-or-not

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

 Library library = new Library();

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

It should then appear as such:

code-deal-or-not

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-deal-or-not

Step 16

After the Application has started running you can then select Newto start the game then select one of the boxes between 1 and 22 to display the amount after each five turns the next turn will result on an Offer and you can Deal or Not until there’s just one box left and that’s what you’ll win or if you did Deal you win that Offer.

ran-deal-or-not

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Lucky Roshambo

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

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

using System;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

public class Library
{
    private const int win = 1;
    private const int draw = 0;
    private const int lose = -1;

    private int[,] match = new int[3, 3]
    {
        { draw, lose, win },
        { win, draw, lose },
        { lose, win, draw }
    };

    private int[] values = new int[] { 0, 1, 2 };
    private string[] options = new string[] { "\uED5B", "\uE130", "\uE16B" };

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

    private async Task<ContentDialogResult> ShowDialogAsync(string title, int option)
    {
        ContentDialog dialog = new ContentDialog()
        {
            Title = title,
            Content = GetShape(option, false),
            PrimaryButtonText = "OK"
        };
        return await dialog.ShowAsync();
    }

    private async void Choose(int option)
    {
        int player = values[option];
        int computer = random.Next(0, 2);
        int result = match[player, computer];
        string message = string.Empty;
        switch(result)
        {
            case win:
                message = "You Win!";
                break;
            case lose:
                message = "You Lost";
                break;
            case draw:
                message = "You Draw";
                break;
        }
        await ShowDialogAsync($"Computer Picked - {message}", computer);
    }

    private Grid GetShape(int option, bool useEvent)
    {
        Grid grid = new Grid()
        {
            Tag = option,
            Margin = new Thickness(5),
            Height = 80,
            Width = 80,
            Background = new SolidColorBrush(Colors.Black),
        };
        TextBlock text = new TextBlock()
        {
            Text = options[option],
            FontSize = 66,
            FontFamily = new FontFamily("Segoe MDL2 Assets"),
            Foreground = new SolidColorBrush(Colors.White),
            VerticalAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Center
        };
        if (useEvent)
        {
            grid.Tapped += (object sender, TappedRoutedEventArgs e) =>
            {
                Grid selected = (Grid)sender;
                int tag = (int)selected.Tag;
                Choose(tag);
            };
        }
        grid.Children.Add(text);
        return grid;
    }

    private void Layout(ref Grid grid)
    {
        grid.Children.Clear();
        StackPanel panel = new StackPanel()
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Center
        };
        for(int i = 0; i < 3; i++)
        {
            panel.Children.Add(GetShape(i, true));
        }
        grid.Children.Add(panel);
    }

    public void New(ref Grid grid)
    {
        Layout(ref grid);
    }
}

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>
<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-lucky-roshambo

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

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

Library library = new Library();

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

It should then appear as such:

code-lucky-roshambo

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-lucky-roshambo

Step 16

After the Application has started running you can then select Newto start the game then press on the first button – Rock, the second – Paper or the third – Scissors then you can see what the Computer selects to see if you Win, Lose or Draw

ran-lucky-roshambo

Step 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Lucky Dominoes

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 7

From the Add New Item choose Visual C# from Installed then choose Code then select Code File and then in the Name as Library.cs and then select Add to add the file to the Project

vs2017-library

Step 8

Then 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 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,
            Background = new LinearGradientBrush(new GradientStopCollection()
            {
                new GradientStop() { Color = Colors.Black, Offset = 0.0 },
                new GradientStop() { Color = Colors.Gray, Offset = 1.0 }
            },
            90),
            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, $"{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);
    }

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

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

Step 9

Then in the Solution Explorer select MainPage.xaml

vs2017-library-mainpage

Step 10

From the Menu choose View and then Designer

vs2017-view-designer

Step 11

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="Accept" Label="New" Click="Choose_Click"/>
</CommandBar>
<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"/>

It should appear as such:

xaml-lucky-dominoes

Step 12

From the Menu choose View and then Code

vs2017-view-code

Step 13

Once in the Code View, below the 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);
}

It should then appear as such:

code-lucky-dominoes

Step 14

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 15

Once started the Application should then appear

run-lucky-dominoes

Step 16

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 17

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Universal Windows Platform – Shade Effect

Step 1

Enable your device for Developer Mode, if not done already then in Windows 10, choose Start and then Settings

win10-settings

From Windows Setttings choose Update & security

win10-update

Then choose For developers and select Developer mode

win10-dev

Step 2

In Windows 10 you need to choose Start then Visual Studio 2017

vs2017-home

If this is not already Downloaded and Installed then Download Visual Studio Community 2017 and then select Run

vs2017-download

Once Visual Studio Community 2017 has been downloaded then you will need to Run the Visual Studio Installer

vs2017-install.png

Then select Continue to prepare the installation, once ready you then to select at least the Universal Windows Platform development option from Workloads in the Visual Studio Installer and then choose Install and follow any on-screen instructions

vs2017-workload

Step 3

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 4

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

Step 5

Then in New Universal Windows Project you need to select the Target Version to be Windows 10 Creators Update (10.0; Build 15063) and the Minimum Version to be at least Windows 10 Anniversary Update (10.0; Build 14393) or Windows 10 Creators Update (10.0; Build 15063)

vs2017-platform

Step 6

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 7

From the Menu choose View and then Designer

vs2017-view-designer

Step 8

The Design View will be displayed along with the XAML View and in this below enter the following XAML:

<CommandBar IsOpen="True" VerticalAlignment="Bottom">
	<AppBarButton Name="Accept" Icon="Accept" Label="Accept" Click="Accept_Click"/>
	<AppBarButton Name="Clear" Icon="Cancel" Label="Clear" Click="Clear_Click"/>
</CommandBar>
<Grid Height="300" Width="300">
	<Border x:Name="ShadowElement"/>
	<Path Name="Logo" Fill="#FF68217A" Stretch="Uniform"
		Data="M27.021,0l8.897,3.592v28.815L26.938,36L12.653,21.796l-9.061,7.021L0,27.021V8.979l3.592-1.714l9.061,7.102
		L27.021,0z M3.592,12.653v10.939l5.388-5.551L3.592,12.653z M17.633,18.041l9.306,7.348V10.693L17.633,18.041z">
	</Path>
</Grid>

It should appear as such:

xaml-shade-effect

Step 9

From the Menu choose View and then Code

vs2017-view-code

Step 10

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

private Windows.UI.Composition.SpriteVisual spriteVisual;

private Windows.UI.Composition.Compositor Compositor
{
	get
	{
		return Windows.UI.Xaml.Hosting.ElementCompositionPreview.GetElementVisual(Logo).Compositor;
	}
}

private void Accept_Click(object sender, RoutedEventArgs e)
{
	spriteVisual = Compositor.CreateSpriteVisual();
	spriteVisual.Size = new System.Numerics.Vector2((float)Logo.ActualWidth, (float)Logo.ActualHeight);
	Windows.UI.Composition.DropShadow shadow = Compositor.CreateDropShadow();
	shadow.Color = Windows.UI.Colors.Black;
	shadow.Offset = new System.Numerics.Vector3(10, 10, 0);
	shadow.Mask = Logo.GetAlphaMask();
	spriteVisual.Shadow = shadow;
	Windows.UI.Xaml.Hosting.ElementCompositionPreview.SetElementChildVisual(ShadowElement, spriteVisual);
}

private void Clear_Click(object sender, RoutedEventArgs e)
{
	spriteVisual.Shadow = null;
}

It should then appear as such:

code-shade-effect

Step 11

That completes the Universal Windows Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-debug

Step 12

Once started the Application should then appear

run-shade-effect

Step 13

After the Application has started running you can then select Accept to enable the Shade Effect or use Clear to remove the effect

ran-shade-effect

Step 14

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License