Universal Windows Platform – Codes Game

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu 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.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 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"/>
	<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 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);
}

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 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-codes-game

Step 15

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 16

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

One thought on “Universal Windows Platform – Codes Game

Leave a comment