102 lines
2.0 KiB
C#
102 lines
2.0 KiB
C#
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;
|
|
}
|
|
} |