Skip to content

Commit

Permalink
Updated Chess\Variant\Classical\PGN\Square
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Feb 2, 2025
1 parent 41344af commit 717ef91
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 192 deletions.
80 changes: 0 additions & 80 deletions src/Variant/Classical/PGN/Square.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,86 +47,6 @@ public function __construct()
}
}

/**
* Returns true if the line of squares has consecutive files.
*
* @param array $line
* @return bool
*/
protected function hasConsecutiveFiles(array $line): bool
{
for ($i = 1; $i < count($line); $i++) {
if (ord($line[$i - 1][0]) !== ord($line[$i][0]) - 1) {
return false;
}
}

return true;
}

/**
* Returns true if the line of squares has consecutive ranks.
*
* @param array $line
* @return bool
*/
protected function hasConsecutiveRanks(array $line, int $diff): bool
{
$ranks = $this->ranks($line);
for ($i = 1; $i < count($ranks); $i++) {
if ($ranks[$i] - $ranks[$i - 1] !== $diff) {
return false;
}
}

return true;
}

/**
* Returns the ranks in a line.
*
* @param array $line
* @return array
*/
protected function ranks(array $line): array
{
$ranks = [];
for ($i = 0; $i < count($line); $i++) {
$ranks[] = (int) substr($line[$i], 1);
}

return $ranks;
}

/**
* Returns true if the line of squares is a diagonal line with the
* precondition that the array is sorted in alphabetical order.
*
* @param array $line
* @return bool
*/
public function isDiagonalLine(array $line): bool
{
return $this->hasConsecutiveFiles($line) &&
($this->hasConsecutiveRanks($line, -1) xor $this->hasConsecutiveRanks($line, 1));
}

/**
* Returns true if the line of squares is a straight line with the
* precondition that the array is sorted in alphabetical order.
*
* @param array $line
* @return bool
*/
public function isStraightLine(array $line): bool
{
if ($this->hasConsecutiveFiles($line)) {
return !$this->hasConsecutiveRanks($line, -1) && !$this->hasConsecutiveRanks($line, 1);
}

return $this->hasConsecutiveRanks($line, 1);
}

/**
* Validate a square in algebraic notation.
*
Expand Down
112 changes: 0 additions & 112 deletions tests/unit/Variant/Classical/PGN/SquareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,116 +118,4 @@ public function from_index_to_algebraic_0_8()
{
$this->assertSame('a9', self::$square->toAn(0, 8));
}

/**
* @test
*/
public function is_diagonal_line_b4_c5_d6()
{
$this->assertTrue(self::$square->isDiagonalLine(['b4', 'c5', 'd6']));
}

/**
* @test
*/
public function is_diagonal_line_b4_d6_c5()
{
$this->assertFalse(self::$square->isDiagonalLine(['b4', 'd6', 'c5']));
}

/**
* @test
*/
public function is_diagonal_line_b4_c3_d2()
{
$this->assertTrue(self::$square->isDiagonalLine(['b4', 'c3', 'd2']));
}

/**
* @test
*/
public function is_diagonal_line_b4_d2()
{
$this->assertFalse(self::$square->isDiagonalLine(['b4', 'd2']));
}

/**
* @test
*/
public function is_diagonal_line_b4_c4()
{
$this->assertFalse(self::$square->isDiagonalLine(['b4', 'c4']));
}

/**
* @test
*/
public function is_diagonal_line_b4_c3_d4()
{
$this->assertFalse(self::$square->isDiagonalLine(['b4', 'c3', 'd4']));
}

/**
* @test
*/
public function is_diagonal_line_b4_c3_d6()
{
$this->assertFalse(self::$square->isDiagonalLine(['b4', 'c3', 'd6']));
}

/**
* @test
*/
public function is_straight_line_b4_c3_d2()
{
$this->assertFalse(self::$square->isStraightLine(['b4', 'c3', 'd2']));
}

/**
* @test
*/
public function is_straight_line_c7_d7_e7()
{
$this->assertTrue(self::$square->isStraightLine(['c7', 'd7', 'e7']));
}

/**
* @test
*/
public function is_straight_line_c7_e7_d7()
{
$this->assertFalse(self::$square->isStraightLine(['c7', 'e7', 'd7']));
}

/**
* @test
*/
public function is_straight_line_c7_c6_c5()
{
$this->assertFalse(self::$square->isStraightLine(['c7', 'c6', 'c5']));
}

/**
* @test
*/
public function is_straight_line_c5_c6_c7()
{
$this->assertTrue(self::$square->isStraightLine(['c5', 'c6', 'c7']));
}

/**
* @test
*/
public function is_diagonal_line_c5_c6_c7()
{
$this->assertFalse(self::$square->isDiagonalLine(['c5', 'c6', 'c7']));
}

/**
* @test
*/
public function is_diagonal_line_c7_d7_e7()
{
$this->assertFalse(self::$square->isDiagonalLine(['c7', 'd7', 'e7']));
}
}

0 comments on commit 717ef91

Please sign in to comment.