Exemple #1
0
 protected static function pieceToForsyth(Piece $piece)
 {
     static $reverseClasses = array('Pawn' => 'p', 'Rook' => 'r', 'Knight' => 'n', 'Bishop' => 'b', 'Queen' => 'q', 'King' => 'k');
     $notation = $reverseClasses[$piece->getClass()];
     if ('white' === $piece->getColor()) {
         $notation = strtoupper($notation);
     }
     return $notation;
 }
Exemple #2
0
 /**
  * Dumps a single move to PGN notation
  *
  * @return string
  **/
 public function dumpMove(Game $game, Piece $piece, Square $from, Square $to, array $playerPossibleMoves, $killed, $isCastling, $isPromotion, $isEnPassant, array $options)
 {
     $board = $game->getBoard();
     $pieceClass = $piece->getClass();
     $fromKey = $from->getKey();
     $toKey = $to->getKey();
     if ($isCastling) {
         if ($this->isCastleKingSide($piece, $to)) {
             return 'O-O';
         } else {
             return 'O-O-O';
         }
     }
     if ($isEnPassant) {
         return $from->getFile() . 'x' . $to->getKey();
     }
     if ($isPromotion) {
         return $to->getKey() . '=' . ('Knight' === $options['promotion'] ? 'N' : $options['promotion'][0]);
     }
     $pgnFromPiece = $pgnFromFile = $pgnFromRank = '';
     if ('Pawn' != $pieceClass) {
         $pgnFromPiece = $piece->getPgn();
         $candidates = array();
         foreach ($playerPossibleMoves as $_from => $_tos) {
             if ($_from !== $fromKey && in_array($toKey, $_tos)) {
                 $_piece = $board->getPieceByKey($_from);
                 if ($_piece->getClass() === $pieceClass) {
                     $candidates[] = $_piece;
                 }
             }
         }
         if (!empty($candidates)) {
             $isAmbiguous = false;
             foreach ($candidates as $candidate) {
                 if ($candidate->getSquare()->getFile() === $from->getFile()) {
                     $isAmbiguous = true;
                     break;
                 }
             }
             if (!$isAmbiguous) {
                 $pgnFromFile = $from->getFile();
             } else {
                 $isAmbiguous = false;
                 foreach ($candidates as $candidate) {
                     if ($candidate->getSquare()->getRank() === $from->getRank()) {
                         $isAmbiguous = true;
                         break;
                     }
                 }
                 if (!$isAmbiguous) {
                     $pgnFromRank = $from->getRank();
                 } else {
                     $pgnFromFile = $from->getFile();
                     $pgnFromRank = $from->getRank();
                 }
             }
         }
     }
     if ($killed) {
         $pgnCapture = 'x';
         if ('Pawn' === $pieceClass) {
             $pgnFromFile = $from->getFile();
         }
     } else {
         $pgnCapture = '';
     }
     $pgnTo = $to->getKey();
     $pgn = $pgnFromPiece . $pgnFromFile . $pgnFromRank . $pgnCapture . $pgnTo;
     return $pgn;
 }
Exemple #3
0
 public function extractPieces()
 {
     $pieces = array();
     if (!empty($this->ps)) {
         foreach (explode(' ', $this->ps) as $p) {
             $pos = Board::keyToPos(Board::piotrToKey($p[0]));
             $class = Piece::letterToClass(strtolower($p[1]));
             $piece = new Piece($pos[0], $pos[1], $class);
             if (ctype_upper($p[1])) {
                 $piece->setIsDead(true);
             }
             $pieces[] = $piece;
         }
     }
     $this->setPieces($pieces);
 }
Exemple #4
0
 public function add(Piece $piece)
 {
     $this->pieces[$piece->getSquareKey()] = $piece;
 }