Universal Windows Platform – Lucky Roulette

Lucky Roulette demonstrates how to create a simple Random Number game displayed with a Grid and TextBlock where can pick choice from a ComboBox.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

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

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

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

using System;
using 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;

public class Library
{
    private const string app_title = "Lucky Roulette";

    private int _spins = 0;
    private int _spinValue = 0;
    private int _pickValue = 0;
    private Grid _pocket;
    private Random _random = new Random((int)DateTime.Now.Ticks);

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

    private bool IsOdd(int value)
    {
        return (value % 2 != 0);
    }

    private void Pick()
    {
        _spins++;
        _pocket.Children.Clear();
        _spinValue = _random.Next(0, 36);
        Color fill = Colors.Transparent;
        if (_spinValue >= 1 && _spinValue <= 10 || _spinValue >= 19 && _spinValue <= 28)
        {
            fill = IsOdd(_spinValue) ? Colors.Black : Colors.DarkRed;
        }
        else if (_spinValue >= 11 && _spinValue <= 18 || _spinValue >= 29 && _spinValue <= 36)
        {
            fill = IsOdd(_spinValue) ? Colors.DarkRed : Colors.Black;
        }
        else if (_spinValue == 0)
        {
            fill = Colors.DarkGreen;
        }
        Grid container = new Grid()
        {
            Height = 200,
            Width = 180,
            BorderBrush = new SolidColorBrush(Colors.Black),
            BorderThickness = new Thickness(5),
            Background = new SolidColorBrush(fill)
        };
        TextBlock text = new TextBlock()
        {
            Foreground = new SolidColorBrush(Colors.White),
            FontSize = 120,
            Text = _spinValue.ToString(),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };
        container.Children.Add(text);
        _pocket.Children.Add(container);
        if (_spinValue == _pickValue) // Check Win
        {
            Show($"Spin {_spins} matched {_spinValue}", app_title);
            _spins = 0;
        }
    }

    private void Layout(ref Grid grid)
    {
        grid.Children.Clear();
        _spins = 0;
        List<int> values = Enumerable.Range(0, 36).ToList();
        _pocket = new Grid()
        {
            Height = 200,
            Width = 180,
            HorizontalAlignment = HorizontalAlignment.Center
        };
        ComboBox combobox = new ComboBox()
        {
            Margin = new Thickness(10),
            HorizontalAlignment = HorizontalAlignment.Center,
            ItemsSource = values,
            SelectedIndex = 0,
        };
        combobox.SelectionChanged += (object sender, SelectionChangedEventArgs e) =>
        {
            _pickValue = (int)combobox.SelectedValue;
        };
        StackPanel panel = new StackPanel();
        panel.Children.Add(_pocket);
        panel.Children.Add(combobox);
        grid.Children.Add(panel);
    }

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

    public void Pick(Grid grid)
    {
        if (_pocket == null) Layout(ref grid);
        Pick();
    }
}

In the Library.cs there are using statements to include the necessary functionality. The Show Method is used to display a MessageDialog and IsOdd is used to determine if a number is odd. Pick is used to select the number to display in a Grid with a TextBlock using the appropriate colour for a Roulette Wheel Pocket number. Layout is used to create the main layout which is a Grid for the Pocket and then a ComboBox to pick your selection within a StackPanel. The New Method uses Layout Method and Pick will also create the look-and-feel of the game if not done already and then it will call Pick.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-library

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Viewbox>
	<Grid Margin="50" Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Viewbox>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Name="Pick" Icon="Accept" Label="Pick" Click="Pick_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a ViewBox containing a Grid Control that will be use for the contents of the game. The second block of XAML is is the CommandBar which contains New – to start a gane and Pick to pick numbers and compare against the selection from the ComboBox.

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

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

Library library = new Library();

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

private void Pick_Click(object sender, RoutedEventArgs e)
{
	library.Pick(Display);
}

Below the MainPage() Method an instance of the Library Class is created, then New_Click is used to call the New method to setup the game and Pick_Click is used to call the Pick Method in the Library Class

Step 13

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

vs2017-local-machine

Step 14

After the Application has started running you can use New to start the Game then select a number from the dropdown list and choose Pick – if you get it right, you win!

ran-lucky-roulette

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

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

Twitter picture

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

Facebook photo

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

w

Connecting to %s