Add the Coord extension type

This commit is contained in:
Vincent Velociter
2024-07-31 11:32:46 +02:00
parent 0eb1172004
commit bbcb028131
2 changed files with 171 additions and 0 deletions
+148
View File
@@ -223,6 +223,9 @@ extension type const Square._(int value) implements int {
/// The rank of the square on the board.
Rank get rank => Rank(value >> 3);
/// The Zero-based, numeric coordinates of the square on the board.
Coord get coord => Coord(value & 0x7, value >> 3);
/// The Algebraic Notation of the square, such as 'a1', 'b2', etc.
String get algebraicNotation =>
file.algebraicNotation + rank.algebraicNotation;
@@ -360,6 +363,151 @@ extension type const Square._(int value) implements int {
];
}
/// Zero-based numeric chessboard coordinate.
///
/// For instance a1 is (0, 0), a2 is (0, 1), etc.
extension type const Coord._((int x, int y) value) {
/// Constructs a [Coord] from a pair of integers ranging from 0 to 7.
const Coord(int x, int y) : this._new((x, y));
const Coord._new(this.value)
: assert(value == (0, 0) ||
value == (0, 1) ||
value == (0, 2) ||
value == (0, 3) ||
value == (0, 4) ||
value == (0, 5) ||
value == (0, 6) ||
value == (0, 7) ||
value == (1, 0) ||
value == (1, 1) ||
value == (1, 2) ||
value == (1, 3) ||
value == (1, 4) ||
value == (1, 5) ||
value == (1, 6) ||
value == (1, 7) ||
value == (2, 0) ||
value == (2, 1) ||
value == (2, 2) ||
value == (2, 3) ||
value == (2, 4) ||
value == (2, 5) ||
value == (2, 6) ||
value == (2, 7) ||
value == (3, 0) ||
value == (3, 1) ||
value == (3, 2) ||
value == (3, 3) ||
value == (3, 4) ||
value == (3, 5) ||
value == (3, 6) ||
value == (3, 7) ||
value == (4, 0) ||
value == (4, 1) ||
value == (4, 2) ||
value == (4, 3) ||
value == (4, 4) ||
value == (4, 5) ||
value == (4, 6) ||
value == (4, 7) ||
value == (5, 0) ||
value == (5, 1) ||
value == (5, 2) ||
value == (5, 3) ||
value == (5, 4) ||
value == (5, 5) ||
value == (5, 6) ||
value == (5, 7) ||
value == (6, 0) ||
value == (6, 1) ||
value == (6, 2) ||
value == (6, 3) ||
value == (6, 4) ||
value == (6, 5) ||
value == (6, 6) ||
value == (6, 7) ||
value == (7, 0) ||
value == (7, 1) ||
value == (7, 2) ||
value == (7, 3) ||
value == (7, 4) ||
value == (7, 5) ||
value == (7, 6) ||
value == (7, 7));
/// Gets the square from the coordinates.
Square get square => Square(value.$2 * 8 + value.$1);
/// All possible coordinates on the chessboard.
static const values = [
Coord(0, 0),
Coord(0, 1),
Coord(0, 2),
Coord(0, 3),
Coord(0, 4),
Coord(0, 5),
Coord(0, 6),
Coord(0, 7),
Coord(1, 0),
Coord(1, 1),
Coord(1, 2),
Coord(1, 3),
Coord(1, 4),
Coord(1, 5),
Coord(1, 6),
Coord(1, 7),
Coord(2, 0),
Coord(2, 1),
Coord(2, 2),
Coord(2, 3),
Coord(2, 4),
Coord(2, 5),
Coord(2, 6),
Coord(2, 7),
Coord(3, 0),
Coord(3, 1),
Coord(3, 2),
Coord(3, 3),
Coord(3, 4),
Coord(3, 5),
Coord(3, 6),
Coord(3, 7),
Coord(4, 0),
Coord(4, 1),
Coord(4, 2),
Coord(4, 3),
Coord(4, 4),
Coord(4, 5),
Coord(4, 6),
Coord(4, 7),
Coord(5, 0),
Coord(5, 1),
Coord(5, 2),
Coord(5, 3),
Coord(5, 4),
Coord(5, 5),
Coord(5, 6),
Coord(5, 7),
Coord(6, 0),
Coord(6, 1),
Coord(6, 2),
Coord(6, 3),
Coord(6, 4),
Coord(6, 5),
Coord(6, 6),
Coord(6, 7),
Coord(7, 0),
Coord(7, 1),
Coord(7, 2),
Coord(7, 3),
Coord(7, 4),
Coord(7, 5),
Coord(7, 6),
Coord(7, 7)
];
}
typedef BySide<T> = IMap<Side, T>;
typedef ByRole<T> = IMap<Role, T>;
typedef ByCastlingSide<T> = IMap<CastlingSide, T>;
+23
View File
@@ -55,6 +55,29 @@ void main() {
expect(Square.h8.offset(-8), Square.h7);
expect(Square.h8.offset(1), null);
});
test('algebraicNotation', () {
expect(Square.a1.algebraicNotation, 'a1');
expect(Square.h8.algebraicNotation, 'h8');
});
test('coord', () {
expect(Square.a1.coord, const Coord(0, 0));
expect(Square.c6.coord, const Coord(2, 5));
expect(Square.h8.coord, const Coord(7, 7));
});
});
group('Coord', () {
test('Coord.values', () {
expect(Coord.values.length, 64);
});
test('square', () {
expect(const Coord(0, 0).square, Square.a1);
expect(const Coord(2, 5).square, Square.c6);
expect(const Coord(7, 7).square, Square.h8);
});
});
group('Move', () {