diff --git a/src/Variant/Classical/PGN/AN/Square.php b/src/Variant/Classical/PGN/AN/Square.php index 1b369f29d..5cd15b24c 100644 --- a/src/Variant/Classical/PGN/AN/Square.php +++ b/src/Variant/Classical/PGN/AN/Square.php @@ -48,28 +48,22 @@ public static function validate(string $value): string */ public static function color(string $sq): string { - static::validate($sq); + static::validate($sq); - $b = []; - for ($i = 0; $i < static::SIZE['files']; $i++) { - for ($j = 0; $j < static::SIZE['ranks']; $j++) { - if ($i % 2 === 0) { - if ($j % 2 === 0) { - $b[] = AsciiArray::fromIndexToAlgebraic($i, $j); - } - } else { - if ($j % 2 !== 0) { - $b[] = AsciiArray::fromIndexToAlgebraic($i, $j); - } - } - } - } + $file = $sq[0]; + $rank = substr($sq, 1); - if (in_array($sq, $b)) { - return Color::B; - } + if ((ord($file) - 97) % 2 === 0) { + if ($rank % 2 !== 0) { + return Color::B; + } + } else { + if ($rank % 2 === 0) { + return Color::B; + } + } - return Color::W; + return Color::W; } /** diff --git a/tests/unit/Variant/Classical/PGN/AN/SquareTest.php b/tests/unit/Variant/Classical/PGN/AN/SquareTest.php index 950d83251..98de8814b 100644 --- a/tests/unit/Variant/Classical/PGN/AN/SquareTest.php +++ b/tests/unit/Variant/Classical/PGN/AN/SquareTest.php @@ -55,4 +55,36 @@ public function e4() { $this->assertSame(Square::validate('e4'), 'e4'); } + + /** + * @test + */ + public function color_a1() + { + $this->assertSame(Square::color('a1'), 'b'); + } + + /** + * @test + */ + public function color_a2() + { + $this->assertSame(Square::color('a2'), 'w'); + } + + /** + * @test + */ + public function color_b1() + { + $this->assertSame(Square::color('b1'), 'w'); + } + + /** + * @test + */ + public function color_b2() + { + $this->assertSame(Square::color('b2'), 'b'); + } }