Exemple #1
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;
 }