Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86f69d1
commit 2d964ef
Showing
10 changed files
with
559 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.1.32407.343 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8Queens", "8Queens\8Queens.csproj", "{9DBA6583-0244-44C7-974C-DBD0F46D3F03}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9DBA6583-0244-44C7-974C-DBD0F46D3F03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{9DBA6583-0244-44C7-974C-DBD0F46D3F03}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{9DBA6583-0244-44C7-974C-DBD0F46D3F03}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{9DBA6583-0244-44C7-974C-DBD0F46D3F03}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {E3A02A7B-D4EC-4081-B993-BA904AE54F36} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net6.0-windows</TargetFramework> | ||
<RootNamespace>_8Queens</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="ChessQueen.png" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Resource Include="ChessQueen.png" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Application x:Class="_8Queens.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:_8Queens" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 _8Queens | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace _8Queens | ||
{ | ||
public class MainViewModel | ||
{ | ||
const int MAX = 8; | ||
|
||
public int[,] Board; | ||
public List<List<Cell>> CellBoard = new(); | ||
|
||
//ctor - builds the chessboard | ||
public MainViewModel() | ||
{ | ||
CellBoard = new List<List<Cell>>(MAX); | ||
|
||
for (int i = 0; i < MAX; i++) | ||
{ | ||
CellBoard.Add(new List<Cell>(MAX)); | ||
|
||
for (int j = 0; j < MAX; j++) | ||
{ | ||
CellBoard[i].Add(new Cell()); | ||
} | ||
} | ||
|
||
Board = new int[MAX, MAX]; | ||
} | ||
public bool Solve(int col) | ||
{ | ||
//if all queens are placed return true | ||
if (col >= MAX) | ||
{ | ||
return true; | ||
} | ||
|
||
for (int row = 0; row < MAX; row++) | ||
{ | ||
if (IsSafe(row, col)) | ||
{ | ||
Board[row, col] = 1; | ||
CellBoard[row][col].Value = 1; | ||
|
||
if (Solve(col + 1)) | ||
{ | ||
return true; | ||
} | ||
|
||
Board[row, col] = 0; | ||
CellBoard[row][col].Value = 0; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool IsSafe(int row, int col) | ||
{ | ||
//from the left until the current col | ||
for (int i = 0; i < col; i++) | ||
{ | ||
if (Board[row, i] == 1) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
//upper diagonal | ||
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) | ||
{ | ||
if (Board[i, j] == 1) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
//lower diagonal | ||
for (int i = row, j = col; i < MAX && j >= 0; i++, j--) | ||
{ | ||
if (Board[i, j] == 1) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace _8Queens | ||
{ | ||
public class Cell | ||
{ | ||
private int _value; | ||
|
||
public int Value | ||
{ | ||
get => _value; | ||
set => _value = value; | ||
} | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.