|
| 1 | +namespace Checkers; |
| 2 | + |
| 3 | +public class Board |
| 4 | +{ |
| 5 | + public List<Piece> Pieces { get; } |
| 6 | + |
| 7 | + public Piece? Aggressor { get; set; } |
| 8 | + |
| 9 | + public Piece? this[int x, int y] => |
| 10 | + Pieces.FirstOrDefault(piece => piece.X == x && piece.Y == y); |
| 11 | + |
| 12 | + public Board() |
| 13 | + { |
| 14 | + Aggressor = null; |
| 15 | + Pieces = new List<Piece> |
| 16 | + { |
| 17 | + new() { NotationPosition ="A3", Color = Black}, |
| 18 | + new() { NotationPosition ="A1", Color = Black}, |
| 19 | + new() { NotationPosition ="B2", Color = Black}, |
| 20 | + new() { NotationPosition ="C3", Color = Black}, |
| 21 | + new() { NotationPosition ="C1", Color = Black}, |
| 22 | + new() { NotationPosition ="D2", Color = Black}, |
| 23 | + new() { NotationPosition ="E3", Color = Black}, |
| 24 | + new() { NotationPosition ="E1", Color = Black}, |
| 25 | + new() { NotationPosition ="F2", Color = Black}, |
| 26 | + new() { NotationPosition ="G3", Color = Black}, |
| 27 | + new() { NotationPosition ="G1", Color = Black}, |
| 28 | + new() { NotationPosition ="H2", Color = Black}, |
| 29 | + |
| 30 | + new() { NotationPosition ="A7", Color = White}, |
| 31 | + new() { NotationPosition ="B8", Color = White}, |
| 32 | + new() { NotationPosition ="B6", Color = White}, |
| 33 | + new() { NotationPosition ="C7", Color = White}, |
| 34 | + new() { NotationPosition ="D8", Color = White}, |
| 35 | + new() { NotationPosition ="D6", Color = White}, |
| 36 | + new() { NotationPosition ="E7", Color = White}, |
| 37 | + new() { NotationPosition ="F8", Color = White}, |
| 38 | + new() { NotationPosition ="F6", Color = White}, |
| 39 | + new() { NotationPosition ="G7", Color = White}, |
| 40 | + new() { NotationPosition ="H8", Color = White}, |
| 41 | + new() { NotationPosition ="H6", Color = White} |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + public static string ToPositionNotationString(int x, int y) |
| 46 | + { |
| 47 | + if (!IsValidPosition(x, y)) throw new ArgumentException("Not a valid position!"); |
| 48 | + return $"{(char)('A' + x)}{y + 1}"; |
| 49 | + } |
| 50 | + |
| 51 | + public static (int X, int Y) ParsePositionNotation(string notation) |
| 52 | + { |
| 53 | + if (notation is null) throw new ArgumentNullException(nameof(notation)); |
| 54 | + notation = notation.Trim().ToUpper(); |
| 55 | + if (notation.Length is not 2 || |
| 56 | + notation[0] < 'A' || 'H' < notation[0] || |
| 57 | + notation[1] < '1' || '8' < notation[1]) |
| 58 | + throw new FormatException($@"{nameof(notation)} ""{notation}"" is not valid"); |
| 59 | + return (notation[0] - 'A', notation[1] - '1'); |
| 60 | + } |
| 61 | + |
| 62 | + public static bool IsValidPosition(int x, int y) => |
| 63 | + 0 <= x && x < 8 && |
| 64 | + 0 <= y && y < 8; |
| 65 | + |
| 66 | + public (Piece A, Piece B) GetClosestRivalPieces(PieceColor priorityColor) |
| 67 | + { |
| 68 | + double minDistanceSquared = double.MaxValue; |
| 69 | + (Piece A, Piece B) closestRivals = (null!, null!); |
| 70 | + foreach (Piece a in Pieces.Where(piece => piece.Color == priorityColor)) |
| 71 | + { |
| 72 | + foreach (Piece b in Pieces.Where(piece => piece.Color != priorityColor)) |
| 73 | + { |
| 74 | + (int X, int Y) vector = (a.X - b.X, a.Y - b.Y); |
| 75 | + double distanceSquared = vector.X * vector.X + vector.Y * vector.Y; |
| 76 | + if (distanceSquared < minDistanceSquared) |
| 77 | + { |
| 78 | + minDistanceSquared = distanceSquared; |
| 79 | + closestRivals = (a, b); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return closestRivals; |
| 84 | + } |
| 85 | + |
| 86 | + public List<Move> GetPossibleMoves(PieceColor color) |
| 87 | + { |
| 88 | + List<Move> moves = new(); |
| 89 | + if (Aggressor is not null) |
| 90 | + { |
| 91 | + if (Aggressor.Color != color) |
| 92 | + { |
| 93 | + throw new Exception($"{nameof(Aggressor)} is not null && {nameof(Aggressor)}.{nameof(Aggressor.Color)} != {nameof(color)}"); |
| 94 | + } |
| 95 | + moves.AddRange(GetPossibleMoves(Aggressor).Where(move => move.PieceToCapture is not null)); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + foreach (Piece piece in Pieces.Where(piece => piece.Color == color)) |
| 100 | + { |
| 101 | + moves.AddRange(GetPossibleMoves(piece)); |
| 102 | + } |
| 103 | + } |
| 104 | + return moves.Any(move => move.PieceToCapture is not null) |
| 105 | + ? moves.Where(move => move.PieceToCapture is not null).ToList() |
| 106 | + : moves; |
| 107 | + } |
| 108 | + |
| 109 | + public List<Move> GetPossibleMoves(Piece piece) |
| 110 | + { |
| 111 | + List<Move> moves = new(); |
| 112 | + ValidateDiagonalMove(-1, -1); |
| 113 | + ValidateDiagonalMove(-1, 1); |
| 114 | + ValidateDiagonalMove( 1, -1); |
| 115 | + ValidateDiagonalMove( 1, 1); |
| 116 | + return moves.Any(move => move.PieceToCapture is not null) |
| 117 | + ? moves.Where(move => move.PieceToCapture is not null).ToList() |
| 118 | + : moves; |
| 119 | + |
| 120 | + void ValidateDiagonalMove(int dx, int dy) |
| 121 | + { |
| 122 | + if (!piece.Promoted && piece.Color is Black && dy is -1) return; |
| 123 | + if (!piece.Promoted && piece.Color is White && dy is 1) return; |
| 124 | + (int X, int Y) target = (piece.X + dx, piece.Y + dy); |
| 125 | + if (!IsValidPosition(target.X, target.Y)) return; |
| 126 | + PieceColor? targetColor = this[target.X, target.Y]?.Color; |
| 127 | + if (targetColor is null) |
| 128 | + { |
| 129 | + if (!IsValidPosition(target.X, target.Y)) return; |
| 130 | + Move newMove = new(piece, target); |
| 131 | + moves.Add(newMove); |
| 132 | + } |
| 133 | + else if (targetColor != piece.Color) |
| 134 | + { |
| 135 | + (int X, int Y) jump = (piece.X + 2 * dx, piece.Y + 2 * dy); |
| 136 | + if (!IsValidPosition(jump.X, jump.Y)) return; |
| 137 | + PieceColor? jumpColor = this[jump.X, jump.Y]?.Color; |
| 138 | + if (jumpColor is not null) return; |
| 139 | + Move attack = new(piece, jump, this[target.X, target.Y]); |
| 140 | + moves.Add(attack); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary>Returns a <see cref="Move"/> if <paramref name="from"/>-><paramref name="to"/> is valid or null if not.</summary> |
| 146 | + public Move? ValidateMove(PieceColor color, (int X, int Y) from, (int X, int Y) to) |
| 147 | + { |
| 148 | + Piece? piece = this[from.X, from.Y]; |
| 149 | + if (piece is null) |
| 150 | + { |
| 151 | + return null; |
| 152 | + } |
| 153 | + foreach (Move move in GetPossibleMoves(color)) |
| 154 | + { |
| 155 | + if ((move.PieceToMove.X, move.PieceToMove.Y) == from && move.To == to) |
| 156 | + { |
| 157 | + return move; |
| 158 | + } |
| 159 | + } |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + public static bool IsTowards(Move move, Piece piece) |
| 164 | + { |
| 165 | + (int Dx, int Dy) a = (move.PieceToMove.X - piece.X, move.PieceToMove.Y - piece.Y); |
| 166 | + int a_distanceSquared = a.Dx * a.Dx + a.Dy * a.Dy; |
| 167 | + (int Dx, int Dy) b = (move.To.X - piece.X, move.To.Y - piece.Y); |
| 168 | + int b_distanceSquared = b.Dx * b.Dx + b.Dy * b.Dy; |
| 169 | + return b_distanceSquared < a_distanceSquared; |
| 170 | + } |
| 171 | +} |
0 commit comments