Simple puzzle

main
syneffort 2 years ago
parent e10ff8bade
commit 67eae0d3f7
  1. 8
      PacticeSolution/PacticeSolution.sln
  2. 9
      PacticeSolution/SimplePuzzle/App.xaml
  3. 17
      PacticeSolution/SimplePuzzle/App.xaml.cs
  4. 10
      PacticeSolution/SimplePuzzle/AssemblyInfo.cs
  5. 21
      PacticeSolution/SimplePuzzle/MainWindow.xaml
  6. 167
      PacticeSolution/SimplePuzzle/MainWindow.xaml.cs
  7. 10
      PacticeSolution/SimplePuzzle/SimplePuzzle.csproj

@ -149,7 +149,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlurText", "BlurText\BlurTe
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelnetCommunicator", "TelnetCommunicator\TelnetCommunicator.csproj", "{06C7726F-CD7F-409A-97ED-343FFCD13BAA}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelnetCommunicator", "TelnetCommunicator\TelnetCommunicator.csproj", "{06C7726F-CD7F-409A-97ED-343FFCD13BAA}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,9 @@
<Application x:Class="SimplePuzzle.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimplePuzzle"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -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
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -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)
)]

@ -0,0 +1,21 @@
<Window x:Class="SimplePuzzle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SimplePuzzle"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid x:Name="PuzzleGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</Window>

@ -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
{
/// <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;
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Loading…
Cancel
Save