4. Game Functionality

Before I start writing the functionality, I will make some more edits to the MainWindow.xaml file
I will hide the second button (it is not needed yet) and name the Canvas container, since it will have to be accessed from the MainWindow.xaml.cs file.
The playing field is 300/300 pixels in size. On which we will place 9 buttons with the size of 100 pixels.
Go to the MainWindow.xaml.cs file
I have provided key places of the code with comments that should help to understand the principle of the program operation
				
					using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


namespace LogicGame
{
    public partial class MainWindow : Window
    {
        short btnSize = 100;  // Buttons size (pixels)
        short gameSize = 3;   // Game Size 3 buttons in row and 3 buttons in column
        const short arrSize = 9; // Buttons array size
        Button[] btnArr = new Button[arrSize]; // Buttons array
        short[] fArr = new short[arrSize]; // if fArr[i] = 1, then btn[i] is painted
        bool IsStart = true;  // push startStopButton flag
        Color btnColor = Colors.Green; // normal button color
        Color fColor = Colors.Blue; // figure color


        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BtnsArrInit(); // button array initialization
        }

        void BtnsArrInit()
        {
            for (int y = 0; y < gameSize; y++)
                for (int x = 0; x < gameSize; x++)
                {
                    int i = y * gameSize + x;
                    {
                        btnArr[i] = new Button();
                        can.Children.Add(btnArr[i]);
                        btnArr[i].Width = btnSize;
                        btnArr[i].Height = btnSize;
                        btnArr[i].Background = new SolidColorBrush(btnColor);
                        Canvas.SetLeft(btnArr[i], x * (btnSize + 10) + 10);
                        Canvas.SetTop(btnArr[i], y * (btnSize +10) + 10);
                        // save the button index to its name
                        string name = "n" + i.ToString();
                        btnArr[i].Name = name;
                        // subscription to the left mouse button click on the button
                        btnArr[i].PreviewMouseLeftButtonDown += MainWindow_PreviewMouseLeftButtonDown;
                    }
                }
        }

        //Vector relativeMousePos;
        FrameworkElement draggedObject;

        private void MainWindow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            draggedObject = (FrameworkElement)sender;

            // The function extracts the index from the object name
            short index = Fn.IndexFromName(draggedObject.Name);
            if (fArr[index] == 1)
                btnArr[index].Background = new SolidColorBrush(fColor);
        }

        private void startStopButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsStart)
            {
                ShowFigure();
                IsStart = false;
                startStopButton.Content = "Clear";
            }
            else
            {
                Clear();
                IsStart = true;
                startStopButton.Content = "Start";
            }
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            // Restart button is not used yet
        }

        // function that shows the shape
        private async void ShowFigure()
        {
            Random rnd = new Random();
            for (short i = 0; i < arrSize; i++)
            {
                fArr[i] = (short)(rnd.Next(0, 2));
                if (fArr[i] == 1) btnArr[i].Background = new SolidColorBrush(fColor);
            }

            await Task.Delay(1000);

            for (short i = 0; i < arrSize; i++) btnArr[i].Background = new SolidColorBrush(btnColor);
        }

        // clearing the playing field
        private void Clear()
        {
            for(short i = 0; i < arrSize; i++)
            {
                btnArr[i].Background = new SolidColorBrush(btnColor);
            }
        }

    }
}

				
			
  1. An array of btnArr buttons is created, the buttons are placed inside the Canvas container in three rows and three columns. Also, its index is written in the Name field of the button, so that you can track which button the “MouseLeftButtonDown” event occurred on.
  2. An integer array fArr of type short is created and its elements are assigned 0 or 1 randomly. If the element is equal to 1, the corresponding button is colored in fColor (Blue). Thus a figure is formed, which is displayed for a certain time (1 sec).
  3. Determining which element of the button array was clicked with the left mouse button. The index is extracted from the name of the object that was clicked by the IndexFromName() function, which is located in the Fn class.

 

Fn class code:
				
					using System;

namespace LogicGame
{
    class Fn
    {
        public static short IndexFromName(string name)
        {
            char n = 'n';
            short index = 0;
            name = name.Trim(n);
            index = Int16.Parse(name);
            return index;
        }
    }
}

				
			
This video demonstrates the current version of the game:


 
What remains to be added is the counting of the total number and the number of figures guessed.
Made the following changes to the program:
Declare the necessary variables and create a list that will contain the indices of the squares that make up the figure.
When a button that is part of a shape is clicked, the list of button indexes of buttons that have not yet been clicked is enumerated. If the corresponding index is found, the button is colored blue and the corresponding list item is deleted.
In the next block, the presence of elements in the list is checked. If the list is empty, the figure is completed.
When you press the Start button, you reset the figure completion flag to move on to the next figure.
Adding the index of the button included in the shape to the index list.
Current C# code:
				
					using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace LogicGame
{
    public partial class MainWindow : Window
    {
        short btnSize = 100;  // Buttons size (pixels)
        short gameSize = 3;   // Game Size 3 buttons in row and 3 buttons in column
        const short arrSize = 9; // Buttons array size
        Button[] btnArr = new Button[arrSize]; // Buttons array
        short[] fArr = new short[arrSize]; // if fArr[i] = 1, then btn[i] is painted
        bool IsStart = true;  // push startStopButton flag
        Color btnColor = Colors.Green; // normal button color
        Color fColor = Colors.Blue; // figure color

        short totalFigures = 0; // Total figures
        short completedFigures = 0; // Completed figures
        bool IsFigureDone = false; // Figure Completion Flag.
        List<short> indicesSS = new List<short>(); // indices of selected quadrants

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BtnsArrInit(); // button array initialization
        }

        void BtnsArrInit()
        {
            for (int y = 0; y < gameSize; y++)
                for (int x = 0; x < gameSize; x++)
                {
                    int i = y * gameSize + x;
                    {
                        btnArr[i] = new Button();
                        can.Children.Add(btnArr[i]);
                        btnArr[i].Width = btnSize;
                        btnArr[i].Height = btnSize;
                        btnArr[i].Background = new SolidColorBrush(btnColor);
                        Canvas.SetLeft(btnArr[i], x * (btnSize + 10) + 10);
                        Canvas.SetTop(btnArr[i], y * (btnSize +10) + 10);
                        // save the button index to its name
                        string name = "n" + i.ToString();
                        btnArr[i].Name = name;
                        // subscription to the left mouse button click on the button
                        btnArr[i].PreviewMouseLeftButtonDown += MainWindow_PreviewMouseLeftButtonDown;
                    }
                }
        }

        //Vector relativeMousePos;
        FrameworkElement draggedObject;

        private void MainWindow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            draggedObject = (FrameworkElement)sender;

            // The function extracts the index from the object name
            short index = Fn.IndexFromName(draggedObject.Name);
            if (fArr[index] == 1)
            {
                for(short i = 0; i < indicesSS.Count; i++)
                {
                    if (indicesSS[i] == index)
                    {
                        btnArr[index].Background = new SolidColorBrush(fColor);
                        indicesSS.RemoveAt(i);
                        break;
                    }
                }
            }

            if (indicesSS.Count == 0 && !IsFigureDone)
            {
                IsFigureDone = !IsFigureDone;
                completedFigures++;
                donelLabel.Content = completedFigures;
            }
        }

        private void startStopButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsStart)
            {
                ShowFigure();
                IsStart = false;
                startStopButton.Content = "Clear";
                IsFigureDone = false;
            }
            else
            {
                Clear();
                IsStart = true;
                startStopButton.Content = "Start";
            }
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            // Restart button is not used yet
        }

        // function that shows the shape
        private async void ShowFigure()
        {
            totalFigures++;
            totalLabel.Content = totalFigures;
            Random rnd = new Random();
            for (short i = 0; i < arrSize; i++)
            {
                fArr[i] = (short)(rnd.Next(0, 2));
                if (fArr[i] == 1)
                {
                    indicesSS.Add(i);
                    btnArr[i].Background = new SolidColorBrush(fColor);
                }
            }

            await Task.Delay(1000);

            for (short i = 0; i < arrSize; i++)
            {
                btnArr[i].Background = new SolidColorBrush(btnColor);
            }
        }

        // clearing the playing field
        private void Clear()
        {
            for(short i = 0; i < arrSize; i++)
            {
                btnArr[i].Background = new SolidColorBrush(btnColor);
            }
        }

    }
}

				
			
Compile and test the program: