Universal Windows Platform – Lucky Roshambo

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 locate and select Visual Studio 2017.

vs2017-home

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

Step 4

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 5

From the Menu choose Project, then Add New Item…

vs2017-add-new-item

Step 6

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 7

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 8

Then in the Solution Explorer select MainPage.xaml

vs2017-library-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:

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

From the Menu choose View and then Code

vs2017-view-code

Step 12

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 13

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 14

Once started the Application should then appear

run-lucky-roshambo

Step 15

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 16

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Advertisements

One thought on “Universal Windows Platform – Lucky Roshambo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s