From 67eae0d3f7a678f40abd08c10c60b29968780742 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 21 Dec 2023 17:33:59 +0900 Subject: [PATCH] Simple puzzle --- PacticeSolution/PacticeSolution.sln | 8 +- PacticeSolution/SimplePuzzle/App.xaml | 9 + PacticeSolution/SimplePuzzle/App.xaml.cs | 17 ++ PacticeSolution/SimplePuzzle/AssemblyInfo.cs | 10 ++ PacticeSolution/SimplePuzzle/MainWindow.xaml | 21 +++ .../SimplePuzzle/MainWindow.xaml.cs | 167 ++++++++++++++++++ .../SimplePuzzle/SimplePuzzle.csproj | 10 ++ 7 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 PacticeSolution/SimplePuzzle/App.xaml create mode 100644 PacticeSolution/SimplePuzzle/App.xaml.cs create mode 100644 PacticeSolution/SimplePuzzle/AssemblyInfo.cs create mode 100644 PacticeSolution/SimplePuzzle/MainWindow.xaml create mode 100644 PacticeSolution/SimplePuzzle/MainWindow.xaml.cs create mode 100644 PacticeSolution/SimplePuzzle/SimplePuzzle.csproj diff --git a/PacticeSolution/PacticeSolution.sln b/PacticeSolution/PacticeSolution.sln index 8347091..832d33a 100644 --- a/PacticeSolution/PacticeSolution.sln +++ b/PacticeSolution/PacticeSolution.sln @@ -149,7 +149,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlurText", "BlurText\BlurTe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelnetCommunicator", "TelnetCommunicator\TelnetCommunicator.csproj", "{06C7726F-CD7F-409A-97ED-343FFCD13BAA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CancellationTokenTest", "CancellationTokenTest\CancellationTokenTest.csproj", "{BB6710B9-7E6B-4251-ACCA-7640A701CD57}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CancellationTokenTest", "CancellationTokenTest\CancellationTokenTest.csproj", "{BB6710B9-7E6B-4251-ACCA-7640A701CD57}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplePuzzle", "SimplePuzzle\SimplePuzzle.csproj", "{7D506F5F-9D17-4343-A39C-F78D130F5BDE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -453,6 +455,10 @@ Global {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Debug|Any CPU.Build.0 = Debug|Any CPU {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Release|Any CPU.ActiveCfg = Release|Any CPU {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Release|Any CPU.Build.0 = Release|Any CPU + {7D506F5F-9D17-4343-A39C-F78D130F5BDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D506F5F-9D17-4343-A39C-F78D130F5BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D506F5F-9D17-4343-A39C-F78D130F5BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D506F5F-9D17-4343-A39C-F78D130F5BDE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PacticeSolution/SimplePuzzle/App.xaml b/PacticeSolution/SimplePuzzle/App.xaml new file mode 100644 index 0000000..5c46a8c --- /dev/null +++ b/PacticeSolution/SimplePuzzle/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/PacticeSolution/SimplePuzzle/App.xaml.cs b/PacticeSolution/SimplePuzzle/App.xaml.cs new file mode 100644 index 0000000..aa4b5f8 --- /dev/null +++ b/PacticeSolution/SimplePuzzle/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace SimplePuzzle +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/PacticeSolution/SimplePuzzle/AssemblyInfo.cs b/PacticeSolution/SimplePuzzle/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/PacticeSolution/SimplePuzzle/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/PacticeSolution/SimplePuzzle/MainWindow.xaml b/PacticeSolution/SimplePuzzle/MainWindow.xaml new file mode 100644 index 0000000..8f03e27 --- /dev/null +++ b/PacticeSolution/SimplePuzzle/MainWindow.xaml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/PacticeSolution/SimplePuzzle/MainWindow.xaml.cs b/PacticeSolution/SimplePuzzle/MainWindow.xaml.cs new file mode 100644 index 0000000..1194b79 --- /dev/null +++ b/PacticeSolution/SimplePuzzle/MainWindow.xaml.cs @@ -0,0 +1,167 @@ +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 +{ + /// + /// Interaction logic for MainWindow.xaml + /// + 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; + } + } +} diff --git a/PacticeSolution/SimplePuzzle/SimplePuzzle.csproj b/PacticeSolution/SimplePuzzle/SimplePuzzle.csproj new file mode 100644 index 0000000..4106cb0 --- /dev/null +++ b/PacticeSolution/SimplePuzzle/SimplePuzzle.csproj @@ -0,0 +1,10 @@ + + + + WinExe + net6.0-windows + enable + true + + +