public function addBattlefield(Battlefield $battlefield) : self
 {
     $this->battlefields[] = $battlefield;
     if (PlayerModel::isAIControlled($battlefield->getPlayer())) {
         foreach ($battlefield->getCells() as $cell) {
             $cell->setFlags(CellModel::FLAG_NONE);
         }
     }
     return $this;
 }
 /**
  * @see GameInitiationResponse::addBattlefield()
  * @test
  */
 public function addBattlefield()
 {
     $game = MockFactory::getGameMock();
     $game->getBattlefields()[0]->setPlayer(MockFactory::getAIPlayerMock(''))->getCellByCoordinate('A1')->setFlags(CellModel::FLAG_SHIP);
     $request = new GameInitiationResponse($game->getBattlefields());
     foreach ($request->getBattlefields() as $battlefield) {
         foreach ($battlefield->getCells() as $cell) {
             if (PlayerModel::isAIControlled($battlefield->getPlayer())) {
                 $this->assertEquals(CellModel::FLAG_NONE, $cell->getFlags());
             }
         }
     }
 }
 /**
  * should:
  *      generate X battlefields of Y size
  *      assign AI controlled player to the generated battlefield
  *      initiate ship cells for the generated battlefield
  *      attach generated battlefield to the Game
  *
  * @see GameBuilder::attachAIBattlefields
  * @test
  */
 public function attachAIBattlefields()
 {
     $game = MockFactory::getGameMock(0, 0);
     $this->invokeMethod(static::$gameBuilder, 'attachAIBattlefields', [$game, 2, 7]);
     $this->assertCount(2, $game->getBattlefields());
     foreach ($game->getBattlefields() as $battlefield) {
         $this->assertCount(49, $battlefield->getCells());
         $this->assertTrue(PlayerModel::isAIControlled($battlefield->getPlayer()));
         $this->assertTrue(BattlefieldModel::hasUnfinishedShips($battlefield));
         foreach ($battlefield->getCells() as $coordinate => $cell) {
             /** all battlefields associated with AI players currently have hardcoded ship into B2 cell */
             $expectedFlag = 'B2' === $coordinate ? CellModel::FLAG_SHIP : CellModel::FLAG_NONE;
             $this->assertEquals($expectedFlag, $cell->getFlags());
         }
     }
 }
 /**
  * @param Battlefield $battlefield
  * @param Cell        $cell - this cell will be attacked if attacker is human
  *
  * @return Cell
  */
 protected function processPlayerTurn(Battlefield $battlefield, Cell $cell) : Cell
 {
     return PlayerModel::isAIControlled($battlefield->getPlayer()) ? CellModel::switchPhase($cell) : $this->ai->processCPUTurn($battlefield);
 }
 /**
  * should return new player controlled by Human, as it didn't exist before
  *
  * @see      PlayerModel::createOnRequestHumanControlled
  * @test
  *
  * @depends  isAIControlledOnFlagNone
  * @requires isAIControlledOnFlagAIControlled
  */
 public function createOnRequestHumanControlledOnNonExistingPlayer()
 {
     $player = static::$playerModel->createOnRequestHumanControlled('NON-EXISTING-HUMAN-PLAYER');
     $this->assertEquals('NON-EXISTING-HUMAN-PLAYER', $player->getName());
     $this->assertFalse(PlayerModel::isAIControlled($player));
     /** because player is not persisted yet */
     $this->assertNull($player->getId());
 }