You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SimplePuzzle
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Button[,] _puzzleButtons;
private Random _random = new Random();
public MainWindow()
{
InitializeComponent();
InitPuzzle();
}
private void InitPuzzle()
{
_puzzleButtons = new Button[3, 3];
int counter = 1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
Button button = new Button
{
Content = counter == 9 ? "" : counter.ToString(),
Width = 50,
Height = 50
};
button.Click += PuzzleButton_Click;
_puzzleButtons[row, col] = button;
Grid.SetRow(button, row);
Grid.SetColumn(button, col);
PuzzleGrid.Children.Add(button);
counter++;
}
}
ShufflePuzzle();
}
private void ShufflePuzzle()
{
int n = 3 * 3;
while (n > 1)
{
n--;
int k = _random.Next(n + 1);
int row1 = n / 3;
int col1 = n % 3;
int row2 = k / 3;
int col2 = k % 3;
Button temp = _puzzleButtons[row1, col1];
_puzzleButtons[row1, col1] = _puzzleButtons[row2, col2];
_puzzleButtons[row2, col2] = temp;
Grid.SetRow(_puzzleButtons[row1, col1], row1);
Grid.SetColumn(_puzzleButtons[row1, col1], col1);
Grid.SetRow(_puzzleButtons[row2, col2], row2);
Grid.SetColumn(_puzzleButtons[row2, col2], col2);
}
}
private void PuzzleButton_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
if (clickedButton == null)
return;
int row = Grid.GetRow(clickedButton);
int col = Grid.GetColumn(clickedButton);
if (!CheckMoveValidation(row, col))
return;
Button emptyButton = FindEmptyButton();
SwapButton(clickedButton, emptyButton);
if (IsPuzzleSolved())
MessageBox.Show("🎊 Congratulations!! 🎊");
}
private bool CheckMoveValidation(int row, int col)
{
return (Math.Abs(row - FindEmptyButtonRow()) + Math.Abs(col - FindEmptyButtonCol()) == 1);
}
private Button FindEmptyButton()
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (string.IsNullOrEmpty(_puzzleButtons[row, col].Content.ToString()))
return _puzzleButtons[row, col];
}
}
return null;
}
private int FindEmptyButtonRow()
{
return Grid.GetRow(FindEmptyButton());
}
private int FindEmptyButtonCol()
{
return Grid.GetColumn(FindEmptyButton());
}
private void SwapButton(Button button1, Button button2)
{
int row1 = Grid.GetRow(button1);
int col1 = Grid.GetColumn(button1);
int row2 = Grid.GetRow(button2);
int col2 = Grid.GetColumn(button2);
_puzzleButtons[row1, col1] = button2;
_puzzleButtons[row2, col2] = button1;
Grid.SetRow(button1, row2);
Grid.SetColumn(button1, col2);
Grid.SetRow(button2, row1);
Grid.SetColumn(button2, col1);
}
private bool IsPuzzleSolved()
{
int counter = 1;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if (row == 2 && col == 2)
return string.IsNullOrEmpty(_puzzleButtons[row, col].Content.ToString());
if (_puzzleButtons[row, col].Content.ToString() != counter.ToString())
return false;
counter++;
}
}
return true;
}
}
}