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);
}
}
}
}
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;
}
}
}
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 indicesSS = new List(); // 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);
}
}
}
}