Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/674 tdd the chess glossary open file term class #688

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implemented Chess\Glossary\OpenFileTerm
  • Loading branch information
programarivm committed Dec 17, 2024
commit 133eb7686e668d153e3357e97eef16deed1dc1c8
39 changes: 39 additions & 0 deletions src/Glossary/OpenFileTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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
{
$imploded = implode(', ', $this->toElaborate);

return [
"These are open files: $imploded.",
];
}
}
26 changes: 26 additions & 0 deletions tests/unit/Glossary/OpenFileTermTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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: c.",
];

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

$openFileTerm = new OpenFileTerm($board);

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