public function moveAction($hash) { $player = $this->findPlayer($hash); if (!$player->isMyTurn()) { throw new \LogicException('Not my turn'); } $opponent = $player->getOpponent(); $game = $player->getGame(); $move = $this->getRequest()->get('from') . ' ' . $this->getRequest()->get('to'); $stack = new Stack(); $manipulator = new Manipulator($game->getBoard(), $stack); $opponentPossibleMoves = $manipulator->play($move, $this->getRequest()->get('options', array())); $data = array('time' => time(), 'possible_moves' => null, 'events' => $stack->getEvents()); if ($game->getIsFinished()) { $data['events'][] = array('type' => 'end', 'table_url' => $this->generateUrl('lichess_table', array('hash' => $player->getFullHash()))); } $response = $this->createResponse(json_encode($data)); $response->headers->set('Content-Type', 'application/json'); if ($opponent->getIsAi()) { if (empty($opponentPossibleMoves)) { $this->container->getLichessPersistenceService()->save($game); } else { $ai = $this->container->getLichessAiService(); $stack->reset(); $possibleMoves = $manipulator->play($ai->move($game, $opponent->getAiLevel())); $this->container->getLichessPersistenceService()->save($game); $data = array('possible_moves' => $possibleMoves, 'events' => $stack->getEvents()); if ($game->getIsFinished()) { $data['events'][] = array('type' => 'end', 'table_url' => $this->generateUrl('lichess_table', array('hash' => $player->getFullHash()))); } $this->container->getLichessSocketService()->write($player, $data); } } else { $this->container->getLichessPersistenceService()->save($game); $data = array('time' => time(), 'possible_moves' => $opponentPossibleMoves, 'events' => $stack->getEvents()); if ($game->getIsFinished()) { $data['events'][] = array('type' => 'end', 'table_url' => $this->generateUrl('lichess_table', array('hash' => $opponent->getFullHash()))); } $this->container->getLichessSocketService()->write($opponent, $data); } return $response; }
/** * 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())); } }
public function testCheck() { $data = <<<EOF Q k K EOF; $game = $this->createGame($data); $this->game->getBoard()->getPieceByKey('b7')->setFirstMove(1); $stack = new Stack(); $manipulator = new Manipulator($this->game->getBoard(), $stack); $manipulator->play('b7 b6', array('promotion' => 'Knight')); $this->assertEquals(array(array('type' => 'move', 'from' => 'b7', 'to' => 'b6'), array('type' => 'check', 'key' => 'd6')), $stack->getEvents()); }