Skip to content

Commit

Permalink
Refactored Square
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Nov 22, 2023
1 parent 05da2bc commit 751e5b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/Variant/Classical/PGN/AN/Square.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Variant/Classical/PGN/AN/SquareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 751e5b9

Please sign in to comment.