public function testRandomPointGenerator()
 {
     $count = 8;
     $safeRow = 2;
     $safeColumn = 4;
     $length = 6;
     $width = 10;
     $points = RandomPointGenerator::generate($length, $width, $safeRow, $safeColumn, $count);
     $this->assertEquals($count, count($points));
     $pointStrings = [];
     foreach ($points as $point) {
         $this->assertFalse($point->getRow() == $safeRow && $point->getColumn() == $safeColumn);
         $pointString = (string) $point;
         $this->assertFalse(in_array($pointString, $pointStrings));
         $pointStrings[] = $pointString;
     }
 }
Esempio n. 2
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;
         }
     }
 }