Skip to content

Commit

Permalink
Issue/674 tdd the chess glossary open file term class (#688)
Browse files Browse the repository at this point in the history
* Implemented Chess\Glossary\OpenFileTerm

* TDDed Chess\Glossary\OpenFileTerm

* Added test

* Added test

* Added test

* Wording

* TDDed Chess\Glossary\OpenFileTerm
  • Loading branch information
programarivm authored Dec 17, 2024
1 parent f1f8e5a commit 4b843df
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Glossary/OpenFileTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Chess\Glossary;

use Chess\Variant\AbstractBoard;
use Chess\Variant\Classical\PGN\AN\Piece;

class OpenFileTerm extends AbstractTerm
{
use ElaborateTermTrait;

const NAME = 'Open file';

public function __construct(AbstractBoard $board)
{
$this->board = $board;

for ($i = 0; $i < $this->board->square::SIZE['files']; $i++) {
$this->toElaborate[] = chr(97 + $i);
for ($j = 0; $j < $this->board->square::SIZE['ranks']; $j++) {
if ($piece = $this->board->pieceBySq($this->board->square->toAlgebraic($i, $j))) {
if ($piece->id === Piece::P) {
array_pop($this->toElaborate);
break;
}
}
}
}
}

public function elaborate(): array
{
$count = count($this->toElaborate);
if ($count > 0 && $count < 4) {
$imploded = implode(', ', $this->toElaborate);
$this->elaboration[] = "These are open files with no pawns of either color on it: $imploded.";
}

return $this->elaboration;
}
}
104 changes: 104 additions & 0 deletions tests/unit/Glossary/OpenFileTermTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Chess\Tests\Unit\Eval;

use Chess\FenToBoardFactory;
use Chess\Glossary\OpenFileTerm;
use Chess\Tests\AbstractUnitTestCase;

class OpenFileTermTest extends AbstractUnitTestCase
{
/**
* @test
*/
public function kaufman_06()
{
$expectedElaboration = [
"These are open files with no pawns of either color on it: c.",
];

$board = FenToBoardFactory::create('r5k1/3n1ppp/1p6/3p1p2/3P1B2/r3P2P/PR3PP1/2R3K1 b - -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}

/**
* @test
*/
public function kaufman_07()
{
$expectedElaboration = [
];

$board = FenToBoardFactory::create('2r2rk1/1bqnbpp1/1p1ppn1p/pP6/N1P1P3/P2B1N1P/1B2QPP1/R2R2K1 b - -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}

/**
* @test
*/
public function kaufman_09()
{
$expectedElaboration = [
"These are open files with no pawns of either color on it: c.",
];

$board = FenToBoardFactory::create('r3k2r/pbn2ppp/8/1P1pP3/P1qP4/5B2/3Q1PPP/R3K2R w KQkq -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}

/**
* @test
*/
public function kaufman_14()
{
$expectedElaboration = [
"These are open files with no pawns of either color on it: b, d, e.",
];

$board = FenToBoardFactory::create('1r2r1k1/p4p1p/6pB/q7/8/3Q2P1/PbP2PKP/1R3R2 w - -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}

/**
* @test
*/
public function kaufman_15()
{
$expectedElaboration = [
"These are open files with no pawns of either color on it: c, d.",
];

$board = FenToBoardFactory::create('r2q1r1k/pb3p1p/2n1p2Q/5p2/8/3B2N1/PP3PPP/R3R1K1 w - -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}

/**
* @test
*/
public function kaufman_20()
{
$expectedElaboration = [
];

$board = FenToBoardFactory::create('8/5p2/pk2p3/4P2p/2b1pP1P/P3P2B/8/7K w - -');

$openFileTerm = new OpenFileTerm($board);

$this->assertSame($expectedElaboration, $openFileTerm->elaborate());
}
}

0 comments on commit 4b843df

Please sign in to comment.