示例#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;
 }
示例#2
0
 protected function isCastleKingSide(King $king, Square $to)
 {
     if ($to->getPiece()) {
         return $to->getX() > $king->getX();
     } else {
         return 7 === $to->getX();
     }
 }
示例#3
0
 public function is(Square $square)
 {
     return $this->key === $square->getKey();
 }
示例#4
0
 /**
  * Handle castling
  **/
 protected function castling(King $king, Square $to)
 {
     if (7 === $to->getX()) {
         $rookSquare = $to->getSquareByRelativePos(1, 0);
         $newRookSquare = $to->getSquareByRelativePos(-1, 0);
     } else {
         $rookSquare = $to->getSquareByRelativePos(-2, 0);
         $newRookSquare = $to->getSquareByRelativePos(1, 0);
     }
     $rook = $rookSquare->getPiece();
     $this->board->move($rook, $newRookSquare->getX(), $newRookSquare->getY());
     $rook->setFirstMove($this->game->getTurns());
     if ($this->stack) {
         $this->stack->add(array('type' => 'castling', 'from' => $rookSquare->getKey(), 'to' => $newRookSquare->getKey()));
     }
 }