/** * @param Game $game * @return array */ public function getMove(Game $game) { $maximizingPlayerMark = $game->getCurrentPlayerMark(); $minimizingPlayerMark = $game->getOtherPlayerMark(); /* * Initial revenue has to be worse than in the worse case game scenario */ $currentRevenue = -self::MAX_REVENUE * 2; $currentColumn = null; $currentRow = null; $boardState = $game->getBoard(); $emptyFields = $boardState->getEmptyFields(); foreach ($emptyFields as $column => $rows) { foreach ($rows as $row => $emptyValue) { $boardState->markField($column, $row, $maximizingPlayerMark); $newRevenue = $this->calculateMiniMaxRevenue($boardState, $maximizingPlayerMark, true, $minimizingPlayerMark); $boardState->clearField($column, $row); if ($newRevenue > $currentRevenue) { $currentRevenue = $newRevenue; $currentColumn = $column; $currentRow = $row; } } } return [$currentColumn, $currentRow]; }
/** * @param Game $game * @return array (column, row) */ public function getMove(Game $game) { if ($game->hasEmptyBoard()) { /* * If the board is empty, instead of calculating the field to mark, * we select it by random. * * We can do this, because it never worsens this game's result for * the player: a computer player will still draw with another computer * player, and not lose with a human player. * * We want to do this, because it's quicker than calculating the move. */ list($column, $row) = $game->getRandomBoardCoordinates(); } else { list($column, $row) = $this->getMiniMaxMove($game); } $this->ioService->printMessage(sprintf("%s %s\n", $column, $row)); return [$column, $row]; }
public function getMove(Game $game) { return $game->getRandomBoardCoordinates(); }
/** * @param Game $game */ private function moveCurrentPlayer(Game $game) { list($randomCol, $randomRow) = $game->getRandomBoardCoordinates(); $msg = sprintf("%s's [%s] move (e.g. %s %s): ", $game->getCurrentPlayerName(), $game->getCurrentPlayerMark(), $randomCol, $randomRow); $this->ioService->printMessageLine($msg); while (true) { try { $game->moveCurrentPlayer(); break; } catch (GameException $e) { $msg = sprintf("%s, try again: ", $e->getMessage()); $this->ioService->printMessage($msg); } } }
public function __construct(Player $startingPlayer, Player $opposingPlayer, Board $board) { parent::__construct($startingPlayer, $opposingPlayer); $this->board = $board; }