49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
|
|
namespace Chessistics.Engine.Model;
|
||
|
|
|
||
|
|
public readonly struct Coords : IEquatable<Coords>
|
||
|
|
{
|
||
|
|
public int Col { get; }
|
||
|
|
public int Row { get; }
|
||
|
|
|
||
|
|
public Coords(int col, int row)
|
||
|
|
{
|
||
|
|
Col = col;
|
||
|
|
Row = row;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsOnBoard(int width, int height)
|
||
|
|
=> Col >= 0 && Col < width && Row >= 0 && Row < height;
|
||
|
|
|
||
|
|
public int ManhattanDistance(Coords other)
|
||
|
|
=> Math.Abs(Col - other.Col) + Math.Abs(Row - other.Row);
|
||
|
|
|
||
|
|
public bool IsAdjacent4(Coords other)
|
||
|
|
=> ManhattanDistance(other) == 1;
|
||
|
|
|
||
|
|
public IReadOnlyList<Coords> GetAdjacent4(int width, int height)
|
||
|
|
{
|
||
|
|
var result = new List<Coords>(4);
|
||
|
|
Coords[] offsets = [new(0, 1), new(0, -1), new(1, 0), new(-1, 0)];
|
||
|
|
foreach (var offset in offsets)
|
||
|
|
{
|
||
|
|
var neighbor = new Coords(Col + offset.Col, Row + offset.Row);
|
||
|
|
if (neighbor.IsOnBoard(width, height))
|
||
|
|
result.Add(neighbor);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Checkerboard parity: true if (Col + Row) is even (light square).
|
||
|
|
/// </summary>
|
||
|
|
public bool IsLight => (Col + Row) % 2 == 0;
|
||
|
|
|
||
|
|
public bool Equals(Coords other) => Col == other.Col && Row == other.Row;
|
||
|
|
public override bool Equals(object? obj) => obj is Coords other && Equals(other);
|
||
|
|
public override int GetHashCode() => HashCode.Combine(Col, Row);
|
||
|
|
public override string ToString() => $"({Col},{Row})";
|
||
|
|
|
||
|
|
public static bool operator ==(Coords left, Coords right) => left.Equals(right);
|
||
|
|
public static bool operator !=(Coords left, Coords right) => !left.Equals(right);
|
||
|
|
}
|