Universal Windows Platform – Chase 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.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 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>
<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 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, ref Label);
}

It should then appear as such:

code-chase-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-chase-game

Step 15

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 16

To Exit the application select Stop in Visual Studio

vs2017-stop

Creative Commons License

Advertisements

One thought on “Universal Windows Platform – Chase Game

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