protected function execute(InputInterface $input, OutputInterface $output) { $this->humanName = $input->getArgument('name'); $hal = new HAL($output); $hal->sayHello(); $p1 = new FANNPlayer($this->ai, 'X'); $p2 = new FANNPlayer($this->ai, 'O'); $board = new Board($output, $this->getApplication()->getTerminalDimensions(), 3); $game = new TicTacToeGame($p1, $p2); $playerMove = 0; $this->logger->debug('BEGIN'); try { $games = 1000000; while (true) { if (!$playerMove) { $this->logger->debug('----- P1 MOVE -----'); $this->computerP1Move($p1, $game, $board); $this->logger->debug('----- P1 MOVE -----'); } else { $this->logger->debug('----- P2 MOVE -----'); $this->computerMove($p2, $game, $board); $this->logger->debug('----- P2 MOVE -----'); } if ($game->isFinished()) { switch ($game->getResult()) { case TicTacToeGame::RESULT_PLAYER_1_WON: $this->logger->debug('Result: Player 1 won.'); break; case TicTacToeGame::RESULT_PLAYER_2_WON: $this->logger->debug('Result: Player 2 won.'); break; case TicTacToeGame::RESULT_TIE: $this->logger->debug('Result: Tie.'); break; } $game = $this->handleEndOfMatch($game, $p1, $p2, $board); $games--; if (0 == $games) { throw new ExitGameException("End of games"); } $this->logger->debug('---------------- END GAME ------------------'); } else { $playerMove = !$playerMove; } } } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); } $this->ai->save(); }
protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->humanName = $input->getArgument('name'); $hal = new HAL($output); $hal->sayHello(); $helper = $this->getHelper('question'); $helper->ask($this->input, $this->output, new ConfirmationQuestion('')); $p1 = new HumanPlayer($this->humanName, 'X'); $p2 = new FANNPlayer($this->ai, 'O'); $board = new Board($output, $this->getApplication()->getTerminalDimensions(), 3); $game = new TicTacToeGame($p1, $p2); $question = $this->createCoordinatesQuestion(); $playerMove = 0; $this->logger->info('--------------- START GAME ------------------'); try { while (true) { if (!$playerMove) { $this->handleHumanMove($question, $p1, $game, $board); } else { $this->computerMove($p2, $game, $board); } if ($game->isFinished()) { $this->logger->info('---------------- END GAME ------------------'); $game = $this->handleEndOfMatch($game, $p2, $p1, $board); } else { $playerMove = !$playerMove; } } } catch (ExitGameException $e) { $this->output->writeln("<question>I knew it!</question>"); } catch (\Exception $e) { $this->output->writeln('Unexpected <error>' . $e->getMessage() . '</error>'); } finally { $this->ai->save(); } }