示例#1
0
 public function testInitBoard()
 {
     $boardGame = new BoardGame();
     $boardGame->init();
     $boardProp = new \ReflectionProperty($boardGame, 'board');
     $boardProp->setAccessible(true);
     $board = $boardProp->getValue($boardGame);
     $this->assertCount(BoardGame::SIZE, $board);
     $countShipPieces = 0;
     foreach ($board as $columns) {
         $this->assertCount(BoardGame::SIZE, $columns);
         foreach ($columns as $value) {
             if ($value > 0) {
                 $countShipPieces++;
             }
         }
     }
     $shipsProp = new \ReflectionProperty($boardGame, 'ships');
     $shipsProp->setAccessible(true);
     $ships = $shipsProp->getValue($boardGame);
     $countAvailableShipPieces = 0;
     /** @var Ship $ship */
     foreach ($ships as $ship) {
         $sizeProp = new \ReflectionProperty($ship, 'size');
         $sizeProp->setAccessible(true);
         $countAvailableShipPieces += $sizeProp->getValue($ship);
     }
     $this->assertEquals($countAvailableShipPieces, $countShipPieces);
 }
示例#2
0
use Jfacchini\Battleship\BoardGame;
use Jfacchini\Battleship\Exception\HitException;
use Silex\Application;
use Silex\Provider\SessionServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Application();
$app->register(new SessionServiceProvider());
$app->get('/', function () use($app) {
    /** @var Session $session */
    $session = $app['session'];
    $game = $session->get('game');
    if (is_null($game)) {
        $game = new BoardGame();
        $game->init();
    }
    //TODO: use twig
    $template = <<<TEMPLATE
<!DOCTYPE html>
<html>
    <head>
    <title>Battleship game</title>
    </head>
    <body>
        {{msg}}
        <pre>{{render}}</pre>
        <form action="/hit" method="post">
            Enter coordinates (row, col), e.g. A5 <input type="text" name="coordinates" size="5" autofocus />
            <input type="submit" value="Hit" />
        </form>