Пример #1
0
 /**
  * @param int $y
  * @param int $x
  * @param string $id
  * @param \TicTacToeGame $currentGame
  * @return bool
  */
 protected function isWonStrategyForId($y, $x, $id, \TicTacToeGame $currentGame)
 {
     $currentMatrix = $currentGame->getCurrentMatrix();
     //check straight line for x axis
     $numberOfMyCells = 0;
     for ($i = 0; $i < $currentGame->getYSize(); $i++) {
         if (!is_null($currentMatrix[$i][$x]) && $currentMatrix[$i][$x] === $id) {
             $numberOfMyCells++;
         }
     }
     if ($numberOfMyCells == $currentGame->getYSize() - 1) {
         return true;
     }
     //check straight line for y axis
     $numberOfMyCells = 0;
     for ($i = 0; $i < $currentGame->getXSize(); $i++) {
         if (!is_null($currentMatrix[$y][$i]) && $currentMatrix[$y][$i] === $id) {
             $numberOfMyCells++;
         }
     }
     if ($numberOfMyCells == $currentGame->getYSize() - 1) {
         return true;
     }
     $numberOfMyCells = 0;
     //check the diagonals
     //This algorithm is applicable for 3x3 matrix
     //@Todo change the algorithm for any matrix
     //Diagonals could be build only for extreme points
     if ($currentGame->getXSize() == 3) {
         //No diagonals possible
         if ($x != 0 && $x != $currentGame->getXSize() && $x != $y) {
             if ($y != 0 && $y != $currentGame->getYSize()) {
                 return false;
             }
         }
         for ($i = 1; $i < $currentGame->getXSize(); $i++) {
             $dX = $x == 0 ? $x + $i : $x - $i;
             $dY = $y == 0 ? $y + $i : $y - $i;
             if (isset($currentMatrix[$dY][$dX]) && !is_null($currentMatrix[$dY][$dX]) && $currentMatrix[$dY][$dX] === $id) {
                 $numberOfMyCells++;
             }
         }
         if ($numberOfMyCells == $currentGame->getYSize() - 1) {
             return true;
         }
     }
     return false;
 }