/** * @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 test_it_returns_coordinates_fetched_from_board_by_random() { $randomCoords = ["random", "coordinates"]; $this->board->shouldReceive("getRandomCoordinates")->andReturn($randomCoords); $this->assertEquals($randomCoords, $this->game->getRandomBoardCoordinates()); }