Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
yehonatan604 committed May 11, 2022
1 parent 86f69d1 commit 2d964ef
Show file tree
Hide file tree
Showing 10 changed files with 559 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 8Queens.sln
@@ -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
19 changes: 19 additions & 0 deletions 8Queens/8Queens.csproj
@@ -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>
8 changes: 8 additions & 0 deletions 8Queens/App.xaml
@@ -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>
17 changes: 17 additions & 0 deletions 8Queens/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 _8Queens
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions 8Queens/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)
)]
88 changes: 88 additions & 0 deletions 8Queens/Board.cs
@@ -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;
}
}
}

13 changes: 13 additions & 0 deletions 8Queens/Cell.cs
@@ -0,0 +1,13 @@
namespace _8Queens
{
public class Cell
{
private int _value;

public int Value
{
get => _value;
set => _value = value;
}
}
}
Binary file added 8Queens/ChessQueen.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2d964ef

Please sign in to comment.