Example #1
0
 /**
  * Execute command
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $registry = PlayerRegistry::getDefaultPlayers();
     $player1 = $input->getArgument('player-x');
     $player2 = $input->getArgument('player-o');
     $count = (int) $input->getOption('games');
     $progress = new ProgressHelper();
     $progress->start($output, $count);
     $stats = [PlayerInterface::SYMBOL_X => 0, PlayerInterface::SYMBOL_O => 0, 'Draw' => 0];
     for ($i = 0; $i < $count; $i++) {
         $game = new Game();
         $game->addPlayer($registry->get($player1, PlayerInterface::SYMBOL_X));
         $game->addPlayer($registry->get($player2), PlayerInterface::SYMBOL_O);
         $winner = $game->autoPlay();
         $stats[$winner ? $winner : 'Draw']++;
         $progress->advance();
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln('Winning statistics');
     $table = new TableHelper();
     $table->setHeaders([$player1, $player2, "Draw"]);
     $table->addRow(array_values($stats));
     $table->render($output);
 }
Example #2
0
 /**
  * @expectedException \Exception
  * @expectedExceptionMessage Field is already filled
  */
 public function testDoTurnOnExistingField()
 {
     $table = array_fill(0, 3, array_fill(0, 3, null));
     $table[1][1] = PlayerInterface::SYMBOL_X;
     $game = new Game($table);
     $game->doTurn([1, 1], PlayerInterface::SYMBOL_X);
     $result = $game->getTable();
     $this->assertEquals($table, $result);
 }
Example #3
0
 public function testTurns()
 {
     $randomPlayer = new DrunkPlayer();
     $game = new Game();
     $game->addPlayer($randomPlayer, PlayerInterface::SYMBOL_X);
     $table = $game->getTurn();
     // First turn will be only in middle
     $this->assertEquals(1, $this->countSymbols($table));
     $this->assertEquals(PlayerInterface::SYMBOL_X, $table[1][1]);
     $table = $game->doTurn($this->findEmptySpot($table), PlayerInterface::SYMBOL_O);
     // we know that our turn definitely will be in corner
     $this->assertEquals(2, $this->countSymbols($table));
     $this->assertEquals(PlayerInterface::SYMBOL_O, $table[0][0]);
     $table = $game->getTurn();
     $this->assertEquals(3, $this->countSymbols($table));
     for ($x = 4; $x < 9; $x += 2) {
         if (!$game->getWinner()) {
             $table = $game->doTurn($this->findEmptySpot($table), PlayerInterface::SYMBOL_O);
             $this->assertEquals($x, $this->countSymbols($table));
         }
         if (!$game->getWinner()) {
             $table = $game->getTurn();
             $this->assertEquals($x + 1, $this->countSymbols($table));
         }
     }
 }
Example #4
0
 /**
  * We expect every player to use win possibility
  * @dataProvider getTestUseWinPossibilityData
  * @param string $name
  * @param $table
  */
 public function testUseWinPossibility($name, $table)
 {
     if ($name == 'drunk') {
         return;
         // we do not expect that from drunk player ;)
     }
     $player = PlayerRegistry::getDefaultPlayers()->get($name);
     $game = new Game($table);
     $game->addPlayer($player);
     $game->getTurn();
     $this->assertEquals(PlayerInterface::SYMBOL_X, $game->getWinner());
 }
Example #5
0
 /**
  * @param Request $request
  * @return JsonResponse
  */
 public function indexJsonAction(Request $request)
 {
     $table = json_decode($request->get('table'));
     $request->get(PlayerInterface::SYMBOL_X) && $this->gameService->addPlayer($this->registryService->get($request->get(PlayerInterface::SYMBOL_X)));
     $request->get(PlayerInterface::SYMBOL_O) && $this->gameService->addPlayer($this->registryService->get($request->get(PlayerInterface::SYMBOL_O)), PlayerInterface::SYMBOL_O);
     $this->gameService->setTable($table);
     $this->gameService->getTurn();
     return new JsonResponse(["status" => "ok", 'table' => $this->gameService->getTable(), 'winner' => $this->gameService->getWinner()]);
 }