Skip to content

Commit

Permalink
Refactored PieceArray as PieceArrayFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Feb 3, 2025
1 parent ff81939 commit 640a449
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/Variant/Capablanca/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Capablanca\Board;
use Chess\Variant\Capablanca\CastlingRule;
use Chess\Variant\Capablanca\FEN\Str;
Expand All @@ -27,12 +27,12 @@ public function __construct(string $string)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/CapablancaFischer/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\CapablancaFischer\Board;
use Chess\Variant\CapablancaFischer\CastlingRule;
use Chess\Variant\Capablanca\FEN\Str;
Expand All @@ -30,12 +30,12 @@ public function __construct(string $string, array $startPos)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($this->startPos, $pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/Chess960/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Chess960\Board;
use Chess\Variant\Chess960\CastlingRule;
use Chess\Variant\Classical\FEN\Str;
Expand All @@ -30,12 +30,12 @@ public function __construct(string $string, array $startPos)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($this->startPos, $pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/Classical/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Classical\Board;
use Chess\Variant\Classical\CastlingRule;
use Chess\Variant\Classical\FEN\Str;
Expand Down Expand Up @@ -41,12 +41,12 @@ public function __construct(string $string)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/Dunsany/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Classical\CastlingRule;
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalFenStrToBoard;
use Chess\Variant\Classical\PGN\Square;
Expand All @@ -27,12 +27,12 @@ public function __construct(string $string)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
6 changes: 3 additions & 3 deletions src/Variant/Losing/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Classical\CastlingRule;
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalFenStrToBoard;
use Chess\Variant\Classical\PGN\Square;
Expand All @@ -27,12 +27,12 @@ public function __construct(string $string)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down
17 changes: 9 additions & 8 deletions src/Variant/PieceArray.php → src/Variant/PieceArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
use Chess\Variant\Classical\PGN\Square;
use Chess\Variant\Classical\CastlingRule;

class PieceArray
class PieceArrayFactory
{
public array $pieces;

public function __construct(array $array, Square $square, CastlingRule $castlingRule = null, string $namespace)
public static function create(array $array, Square $square, CastlingRule $castlingRule = null, string $namespace)
{
$pieces = [];
foreach ($array as $i => $rank) {
foreach ($rank as $j => $char) {
if ($char !== '.') {
Expand All @@ -27,18 +26,20 @@ public function __construct(array $array, Square $square, CastlingRule $castling
}
if ($char === Piece::R) {
if ($sq === $castlingRule?->rule[$color][Piece::R][Castle::LONG]['from']) {
$this->pieces[] = new R($color, $sq, $square, RType::CASTLE_LONG);
$pieces[] = new R($color, $sq, $square, RType::CASTLE_LONG);
} elseif ($sq === $castlingRule?->rule[$color][Piece::R][Castle::SHORT]['from']) {
$this->pieces[] = new R($color, $sq, $square, RType::CASTLE_SHORT);
$pieces[] = new R($color, $sq, $square, RType::CASTLE_SHORT);
} else {
$this->pieces[] = new R($color, $sq, $square, RType::R);
$pieces[] = new R($color, $sq, $square, RType::R);
}
} else {
$class = VariantType::getClass($char, $namespace);
$this->pieces[] = new $class($color, $sq, $square);
$pieces[] = new $class($color, $sq, $square);
}
}
}
}

return $pieces;
}
}
6 changes: 3 additions & 3 deletions src/Variant/RacingKings/FEN/StrToBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Chess\Exception\UnknownNotationException;
use Chess\Variant\AbstractBoard;
use Chess\Variant\PieceArray;
use Chess\Variant\PieceArrayFactory;
use Chess\Variant\Classical\FEN\StrToBoard as ClassicalFenStrToBoard;
use Chess\Variant\Classical\PGN\Square;
use Chess\Variant\RacingKings\Board;
Expand All @@ -24,12 +24,12 @@ public function __construct(string $string)
public function create(): AbstractBoard
{
try {
$pieces = (new PieceArray(
$pieces = PieceArrayFactory::create(
$this->fenStr->toArray($this->fields[0]),
$this->square,
$this->castlingRule,
$this->namespace
))->pieces;
);
$board = new Board($pieces, $this->castlingAbility);
$board->turn = $this->fields[1];
$board->startFen = $this->string;
Expand Down

0 comments on commit 640a449

Please sign in to comment.