/** * 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; }
/** * More variant * * @param Board $board * @param $type * * @return int */ protected function getPosition(Board $board, $type) { $countSide = $board->getSideCount(); for ($y = 1; $y <= $countSide; $y++) { $this->processPositions($board, 'Horizontal', $y); $this->processPositions($board, 'Vertical', $y); } $this->processPositions($board, 'LeftBisector'); $this->processPositions($board, 'RightBisector'); $line = $this->getWinLine($type); $free = $board->getFreeCell(); foreach ($line as $value) { if (in_array($value, $free)) { return $value; } } // no find best position return $free[rand(0, count($free) - 1)]; }