/** * @param MoveInterface $move * * @return string */ public static function toNotation(MoveInterface $move) { if (null === $move->getPiece()) { return ''; } $notation = ''; $piece = $move->getPiece(); $toLabel = strtolower($move->getToLabel()); $isPawn = $piece->getType() === PieceInterface::TYPE_PAWN; $pieceLetter = PieceDecorator::toLetter($piece); if (null !== ($capture = $move->getCapture())) { if ($isPawn) { $column = BoardHelper::getColumnFromPosition($move->getFrom()); $pieceLetter = BoardHelper::columnNumberToLetter($column); } $notation .= sprintf('%sx%s', $pieceLetter, $toLabel); if ($isPawn) { if ($move->getType() === MoveInterface::TYPE_CAPTURE_EN_PASSANT) { $notation .= 'e.p.'; } } return $notation; } if ($isPawn) { return $toLabel; } return sprintf('%s%s', $pieceLetter, $toLabel); }
/** * @param string $asciiBoard * * @return Board */ public static function fromAscii($asciiBoard) { $asciiBoard = trim($asciiBoard); $squares = []; $rows = array_map('trim', explode("\n", trim($asciiBoard))); unset($rows[8]); unset($rows[9]); foreach ($rows as $x => $row) { $rowNumber = 8 - $x; $columns = array_map('trim', explode(' ', substr($row, 5))); foreach ($columns as $y => $pieceAscii) { $columnNumber = $y + 1; if ($pieceAscii === '…') { $piece = null; } else { $piece = PieceDecorator::fromAscii($pieceAscii); } $position = intval($columnNumber . $rowNumber); $squares[] = new Square($position, $piece); } } $board = BoardFactory::create($squares); return $board; }
/** * @param string $notation * * @return string */ private function extractPieceType($notation) { if (strlen($notation) === 2) { return PieceInterface::TYPE_PAWN; } if (strlen($notation) === 4 && ctype_lower(substr($notation, 0, 1)) === true) { return PieceInterface::TYPE_PAWN; } $firstCharacter = substr($notation, 0, 1); return PieceDecorator::getTypeFromLetter($firstCharacter); }