/** * Find winner and store win lines * * @param Board $board * @param $category * @param null $line * */ protected function processPositions(Board $board, $category, $line = null) { $method = 'get' . $category . 'Positions'; $positions = $board->{$method}($line); $winCount = $board->getWinCountByPositions($positions); if ($this->mergeWinCount($winCount)) { $this->winLines[$winCount->getWinner()] = $positions; } }
/** * Simple draw board * * @param Board $board * * @return string */ public static function draw(Board $board) { $out = $line = ''; $set = $board->getKitCells(); /** @var \Ticki\Core\Model\Cell $value */ foreach ($set as $pos => $value) { $template = $pos <= 9 || $value !== null ? " %s " : " %s "; $line .= sprintf($template, $value ? self::formatType($value->getType()) : $pos); if ($pos % $board->getSideCount() == 0) { $out .= $line . "\r\n"; $line = ''; } } return $out; }
/** * Just put first free cell on board */ public function getCell(Board $board, $type) { $free = $board->getFreeCell(); if (empty($free)) { ExceptionFactory::runtime("Not have free cells"); } $position = $this->getPosition($board, $type); switch ($type) { case Cell::TIC: $cell = Cell::createTic($position); break; default: $cell = Cell::createTac($position); } return $cell; }
/** * Check for game finish * * @return int */ protected function checkFinish() { if (!$this->board->isFinish()) { return self::STAGE_GAME; } if ($this->board->getWinnerType() === $this->myType) { return self::STAGE_YOU_WIN; } elseif ($this->board->getWinnerType() === $this->oppositeType) { return self::STAGE_YOU_LOST; } return self::STAGE_DEAD_HEAT; }
/** * o o 3 4 5 6 7 8 9 10 11 12 13 14 15 16 x x x 20 21 22 23 24 25 * */ public function testNotFullLineByWin() { $type = Cell::TAC; $board = new Board(5, 3); $this->assertTrue(!$board->isFinish()); $board->addCell(new Cell(17, $type)); $board->addCell(new Cell(18, $type)); $board->addCell(new Cell(19, $type)); $this->assertTrue($board->isFinish()); $this->assertEquals($type, $board->getWinnerType()); }
/** * Get new random position for free cells * * @param Board $board * @param $type * * @return int */ protected function getPosition(Board $board, $type) { $free = $board->getFreeCell(); return $free[0]; }