Exemplo n.º 1
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()]);
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
 /**
  * @dataProvider getTestInjectPlayersData
  * @param string $class
  */
 public function testInjectPlayer($class)
 {
     $registry = PlayerRegistry::getDefaultPlayers();
     $classes = [];
     foreach ($registry->getNames() as $name) {
         $classes[] = get_class($registry->get($name));
     }
     $this->assertTrue(in_array($class, $classes), sprintf('Class %s not registered in player registry', $class));
 }
Exemplo n.º 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());
 }
Exemplo n.º 5
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use XO\Service\Game;
use XO\Controller\GameController;
use Silex\Provider\ServiceControllerServiceProvider;
class App extends Silex\Application
{
    use Silex\Application\TwigTrait;
}
$app = new App();
$app->register(new ServiceControllerServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../src/XO/Views'));
$gameService = new Game();
$registry = \XO\Service\PlayerRegistry::getDefaultPlayers();
$app['game.controller'] = $app->share(function () use($app, $gameService, $registry) {
    return new GameController($app, $gameService, $registry);
});
$app->get('/', "game.controller:indexAction");
$app->get('/turn.json', "game.controller:indexJsonAction");
$app->run();