/** * @param GameInterface $game * @param string $class * * @return PersistableGameInterface */ protected function createPersistable(GameInterface $game, $class) { $persistable = $this->createPersistableGameObject($class); $persistable->setUid($game->getId()); $persistable->setWhitePlayerName($game->getWhitePlayer()->getName()); $persistable->setWhitePlayerHuman($game->getWhitePlayer()->isHuman()); $persistable->setBlackPlayerName($game->getBlackPlayer()->getName()); $persistable->setBlackPlayerHuman($game->getBlackPlayer()->isHuman()); $persistable->setFinished($game->hasFinished()); $persistable->setFinishedReason($game->getFinishedReason()); return $persistable; }
/** * @param string $notation * @param GameInterface $game * @param int $currentColor * * @return array The parsed data (containing 'from', 'to', and 'piece_type') * * @throws InvalidNotationException */ public function parse($notation, GameInterface $game, $currentColor) { if ($game->hasFinished()) { throw new \InvalidArgumentException('Can\'t parse notations for a finished game'); } $notation = trim($notation); if (strlen($notation) < 2) { throw new InvalidNotationException(sprintf('Algebraic notations for moves always consist of at least 2 characters: %d given', strlen($notation))); } elseif (ctype_alpha($notation) === true) { throw new InvalidNotationException(sprintf('Move notation "%s" should not only consist of alphabetic characters', $notation)); } $parsed = []; $parsed['color'] = $currentColor; $parsed['annotation'] = $this->parseAndRemoveAnnotation($notation); $parsed['attribute'] = $this->parseAndRemoveAttribute($notation); $parsed['promotion'] = $this->parseAndRemovePromotion($notation); $parsed['type'] = $this->parseMoveType($notation); $parsed['piece_type'] = $this->parsePieceType($notation, $parsed['type']); $parsed['to'] = $this->parseTo($notation, $parsed['type'], $parsed['color']); $parsed['from'] = $this->parseFrom($notation, $game, $parsed); return $parsed; }
/** * @param MoveInterface $move * @param GameInterface $game * * @throws InvalidMoveException */ private static function validate(MoveInterface $move, GameInterface $game) { if ($game->hasFinished()) { throw new InvalidMoveException('Game has already finished'); } if (null === ($piece = $move->getPiece())) { if (null === ($piece = $game->getBoard()->getSquare($move->getFrom())->getPiece())) { var_export($game->getBoard()->getSquaresAsString()); throw new InvalidMoveException(sprintf('There must be a piece to move on that position: %s', $move->getFromLabel())); } } if ($game->getCurrentColor() !== $piece->getColor()) { throw new InvalidMoveException(sprintf('You can only move your own pieces: the %s on %s belongs to %s', $piece->getTypeLabel(), $move->getFromLabel(), $game->getOpposingPlayer()->getColor())); } }
/** * @param GameInterface $game * * @return array */ private function getExpectedGameArray(GameInterface $game) { return ['uid' => $game->getId(), 'white_player' => $this->getExpectedPlayerArray($game->getWhitePlayer()), 'black_player' => $this->getExpectedPlayerArray($game->getBlackPlayer()), 'finished' => $game->hasFinished(), 'finished_reason' => $game->getFinishedReason(), 'board' => $this->getExpectedBoardArray($game->getBoard()), 'current_color' => $game->getCurrentColor(), 'states' => $this->getExpectedGameStatesArray($game->getStates()->toArray())]; }