Initial commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
bin
|
||||
obj
|
||||
.vscode
|
||||
.vs
|
||||
102
EightQueens.cs
Normal file
102
EightQueens.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
namespace EightQueens;
|
||||
|
||||
public struct Coord(int x, int y)
|
||||
{
|
||||
public int X { get; set; } = x;
|
||||
public int Y { get; set; } = y;
|
||||
};
|
||||
|
||||
public class Board {
|
||||
|
||||
private List<Coord> Figures {get;set;} = [];
|
||||
|
||||
public bool IsFieldBusy(int x, int y)
|
||||
{
|
||||
return Figures.Any(f => x==f.X && y==f.Y);
|
||||
}
|
||||
|
||||
public void Add(int x, int y)
|
||||
{
|
||||
var pos = new Coord(x, y);
|
||||
|
||||
if (IsFieldBusy(x, y)) return;
|
||||
Figures.Add(pos);
|
||||
}
|
||||
|
||||
public void Remove(int x, int y)
|
||||
{
|
||||
var pos = new Coord(x, y);
|
||||
Figures.Remove(pos);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Figures.Clear();
|
||||
}
|
||||
|
||||
public List<Coord> GetFigures()
|
||||
{
|
||||
return Figures;
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
for (int x = 0; x < 9; x++)
|
||||
{
|
||||
string row = "";
|
||||
for (int y = 0; y < 9; y++)
|
||||
{
|
||||
if (x + y == 0)
|
||||
{
|
||||
row += " ";
|
||||
}
|
||||
else if (y == 0)
|
||||
{
|
||||
row += $"{x}";
|
||||
}
|
||||
else if (x == 0)
|
||||
{
|
||||
row += $"{y}";
|
||||
}
|
||||
else if (IsFieldBusy(x, y))
|
||||
{
|
||||
row += "O";
|
||||
}
|
||||
else
|
||||
{
|
||||
row += "#";
|
||||
}
|
||||
row+=" ";
|
||||
}
|
||||
Console.WriteLine(row);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return Figures.Count;
|
||||
}
|
||||
|
||||
public bool IsFieldSafe(int row, int col)
|
||||
{
|
||||
var figs = Figures.Where(f => !(row==f.X && col==f.Y)).ToList();
|
||||
|
||||
foreach (var fig in figs)
|
||||
{
|
||||
if (fig.X == row || fig.Y == col) return false;
|
||||
if (Math.Abs(fig.X - row) == Math.Abs(fig.Y - col)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsBoardSafe()
|
||||
{
|
||||
foreach (var fig in Figures)
|
||||
{
|
||||
if (!IsFieldSafe(fig.X, fig.Y)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
10
EightQueens.csproj
Normal file
10
EightQueens.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
42
Program.cs
Normal file
42
Program.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using EightQueens;
|
||||
|
||||
var board = new Board();
|
||||
|
||||
string[] cmd = [];
|
||||
bool quit = false;
|
||||
while (!quit)
|
||||
{
|
||||
board.Draw();
|
||||
Console.WriteLine(
|
||||
$"Действия:\n"
|
||||
+ $"add X Y - добавить ферзя\n"
|
||||
+ $"del X Y - удалить ферзя\n"
|
||||
+ $"clear - очистить доску\n"
|
||||
+ $"issolved - проверить решение задачи\n"
|
||||
+ $"exit - выход\n");
|
||||
|
||||
var cmdString = Console.ReadLine();
|
||||
if (cmdString == null) continue;
|
||||
cmd = cmdString.Trim().Split();
|
||||
|
||||
switch (cmd[0])
|
||||
{
|
||||
case "add":
|
||||
board.Add(int.Parse(cmd[1]), int.Parse(cmd[2]));
|
||||
break;
|
||||
case "del":
|
||||
board.Remove(int.Parse(cmd[1]), int.Parse(cmd[2]));
|
||||
break;
|
||||
case "clear":
|
||||
board.Clear();
|
||||
break;
|
||||
case "issolved":
|
||||
Console.WriteLine(board.IsBoardSafe() ? "Ферзи в безопасности!" : "Задача не выполняется");
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Неизвестная команда '{cmd[0]}'");
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine($"\n=======================\n");
|
||||
}
|
||||
Reference in New Issue
Block a user