Skip to content

Commit

Permalink
Refactored Chess\Movetext\FanMovetext
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Aug 20, 2024
1 parent 3d06def commit 383c5ce
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/Movetext/FanMovetext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@ public function __construct(Move $move, string $movetext)
$this->sanMovetext = new SanMovetext($move, $this->toSan($movetext));
$this->move = $this->sanMovetext->move;
$this->movetext = $this->sanMovetext->movetext;
$this->moves = array_map(function($move) {
$this->moves = array_map(function ($move) {
return $this->toFan($move);
}, $this->sanMovetext->moves);

$this->metadata = [
'firstMove' => $this->toFan($this->sanMovetext->metadata['firstMove']),
'lastMove' => $this->toFan($this->sanMovetext->metadata['lastMove']),
'turn' => $this->sanMovetext->metadata['turn'],
];
}

protected function beforeInsert(): FanMovetext
{
}

protected function insert(): void
{
}

public function validate(): string
{
$this->sanMovetext->validate();
Expand All @@ -51,27 +42,25 @@ public function filtered($comments = true, $nags = true): string

private function toFan(string &$movetext): string
{
$this->replace(Piece::R, '', $movetext);
$this->replace(Piece::N, '', $movetext);
$this->replace(Piece::B, '', $movetext);
$this->replace(Piece::Q, '', $movetext);
$this->replace(Piece::K, '', $movetext);
$this->replace(Piece::R, '', $movetext)
->replace(Piece::N, '', $movetext)
->replace(Piece::B, '', $movetext)
->replace(Piece::Q, '', $movetext)
->replace(Piece::K, '', $movetext);

return $movetext;
}

private function toSan(string $movetext): string
{
$movetext = str_replace('', Piece::R, $movetext);
$movetext = str_replace('', Piece::N, $movetext);
$movetext = str_replace('', Piece::B, $movetext);
$movetext = str_replace('', Piece::Q, $movetext);
$movetext = str_replace('', Piece::K, $movetext);

return $movetext;
return str_replace(
['', '', '', '', ''],
[Piece::R, Piece:: N, Piece::B, Piece::Q, Piece:: K],
$movetext
);
}

private function replace($letter, $unicode, &$movetext): void
private function replace($letter, $unicode, &$movetext): FanMovetext
{
if ($letter === Piece::K) {
preg_match_all('/' . Move::KING . '/', $movetext, $a);
Expand All @@ -83,15 +72,19 @@ private function replace($letter, $unicode, &$movetext): void
preg_match_all('/' . Move::PIECE . '/', $movetext, $a);
preg_match_all('/' . Move::PIECE_CAPTURES . '/', $movetext, $b);
}
$matches = [...$a[0], ...$b[0]];
$this->move($letter, $unicode, $matches, $movetext);
array_map(function ($match) use ($letter, $unicode, &$movetext) {
$replaced = str_replace($letter, $unicode, $match);
$movetext = str_replace($match, $replaced, $movetext);
}, [...$a[0], ...$b[0]]);

return $this;
}

private function move($letter, $unicode, $matches, &$movetext): void
protected function beforeInsert(): FanMovetext
{
}

protected function insert(): void
{
array_map(function($match) use ($letter, $unicode, &$movetext) {
$replaced = str_replace($letter, $unicode, $match);
$movetext = str_replace($match, $replaced, $movetext);
}, $matches);
}
}

0 comments on commit 383c5ce

Please sign in to comment.