Ejemplo n.º 1
0
 private function calculate($move, $warPlace, $playerName)
 {
     $coordinates = $move->getCoordenates();
     $x = $coordinates['x'];
     $y = $coordinates['y'];
     $feedback = new Feedback();
     $result = array(isset($warPlace[$x - 1][$y + 1]) ? $warPlace[$x - 1][$y + 1] : null, isset($warPlace[$x][$y + 1]) ? $warPlace[$x][$y + 1] : null, isset($warPlace[$x + 1][$y + 1]) ? $warPlace[$x + 1][$y + 1] : null, isset($warPlace[$x - 1][$y]) ? $warPlace[$x - 1][$y] : null, isset($warPlace[$x + 1][$y]) ? $warPlace[$x + 1][$y] : null, isset($warPlace[$x - 1][$y - 1]) ? $warPlace[$x - 1][$y - 1] : null, isset($warPlace[$x][$y - 1]) ? $warPlace[$x][$y - 1] : null, isset($warPlace[$x + 1][$y - 1]) ? $warPlace[$x + 1][$y - 1] : null);
     $rEnd = array();
     foreach ($result as $idx => $r) {
         if (!is_null($r)) {
             $rEnd[] = $r;
         }
     }
     $count = array_count_values($rEnd);
     asort($count);
     unset($count['.']);
     if (count($count) == 0) {
         $feedback->add(Feedback::WIN, $move);
         return $feedback;
     }
     $dominanteId = key($count);
     $dominanteCount = array_shift($count);
     if ($dominanteId == $playerName || $dominanteId == '.') {
         $feedback->add(Feedback::WIN, $move);
         return $feedback;
     }
     if ($dominanteCount == 1) {
         $feedback->add(Feedback::WIN, $move);
         return $feedback;
     }
     $feedback->add(Feedback::LOSE, $move);
     return $feedback;
 }
Ejemplo n.º 2
0
 public function analyze($playerName, Move $move)
 {
     $feedback = new Feedback();
     $coordinates = $move->getCoordenates();
     $warPlace = $this->arena->getArena();
     if (isset($warPlace[$coordinates['x']][$coordinates['y']]) && ($warPlace[$coordinates['x']][$coordinates['y']] == '.' || $warPlace[$coordinates['x']][$coordinates['y']] == $playerName)) {
         $feedback->add(Feedback::WIN, $move);
     }
     if (isset($warPlace[$coordinates['x']][$coordinates['y']]) && ($warPlace[$coordinates['x']][$coordinates['y']] != '.' && $warPlace[$coordinates['x']][$coordinates['y']] != $playerName)) {
         $feedback->add(Feedback::NEUTRALIZED, $move);
     }
     return $feedback;
 }
Ejemplo n.º 3
0
 /**
  * @test
  */
 public function a_neutralized_feedback()
 {
     $feedback = new Feedback();
     $feedback->add(Feedback::NEUTRALIZED, new move(1, 1));
     $feedback->add(Feedback::NEUTRALIZED, new move(0, 1));
     $this->assertCount(0, $feedback->getWinners());
     $this->assertCount(0, $feedback->getLosers());
     $this->assertCount(2, $feedback->getNeutralizeds());
 }
Ejemplo n.º 4
0
 public function play($id, Move $move)
 {
     $feedback = new Feedback();
     foreach ($this->playAnalizes as $analize) {
         $result = $analize->analyze($id, $move);
         $feedback->merge(Feedback::WIN, $result->getWinners());
         $feedback->merge(Feedback::LOSE, $result->getLosers());
         $feedback->merge(Feedback::NEUTRALIZED, $result->getNeutralizeds());
     }
     foreach ($feedback->getWinners() as $winner) {
         $m = $winner->getCoordenates();
         $this->arena[$m['x']][$m['y']] = $id;
     }
     foreach ($feedback->getNeutralizeds() as $neutralized) {
         $m = $neutralized->getCoordenates();
         $this->arena[$m['x']][$m['y']] = '.';
     }
     return $feedback;
 }