Ejemplo n.º 1
0
 public function makeMove(Move $move)
 {
     if ($this->gameState !== self::GAME_STATE_PLAYING) {
         throw new InvalidArgumentException('game is already over');
     }
     $row = $move->getRow();
     $column = $move->getColumn();
     // wait until the user makes a first move to initialize the bomb locations
     // in order to ensure that the first move doesn't ever land on a mine
     if (!$this->bombsInitialized) {
         $points = RandomPointGenerator::generate($this->getLength(), $this->getWidth(), $row, $column, $this->getBombCount());
         $this->initBombs($points);
     }
     $cell = $this->getCell($row, $column);
     if (!$cell->isHidden()) {
         throw new InvalidArgumentException($row . ',' . $column . ' is already showing');
     }
     if ($move->isFlag()) {
         $cell->toggleFlagged();
         return;
     }
     if ($cell->isBomb()) {
         $this->gameState = self::GAME_STATE_LOST;
     } else {
         $cell->setHidden(false);
         $this->hiddenCount--;
         if ($cell->getSurroundingBombCount() === 0) {
             $this->revealSurroundingCells($row, $column);
         }
         if ($this->isGameWon()) {
             $this->gameState = self::GAME_STATE_WON;
         }
     }
 }