public static function init() { require_once SN_ROOT_PHYSICAL . 'includes/constants.php'; require_once SN_ROOT_PHYSICAL . 'includes/trash.php'; // Пока простенькая локаль. Будет время - добавлю объект поумнее require_once SN_ROOT_PHYSICAL . 'locales/ru.mo.php'; self::$controller = new Controller(); // self::$config = new Config(); }
function win() { // Check the rows, columns and diagonals for a winning line. $lines = array(array($this->board[0], $this->board[4], $this->board[8]), array($this->board[2], $this->board[4], $this->board[6])); for ($i = 0; $i <= 2; $i++) { $lines[] = array($this->board[$i], $this->board[$i + 3], $this->board[$i + 6]); $lines[] = array($this->board[$i * 3], $this->board[$i * 3 + 1], $this->board[$i * 3 + 2]); } foreach ($lines as $line) { if (TicTacToe::checkline($line)) { if ($line[0] == "o") { return -1; } else { if ($line[0] == "x") { return 1; } } } } return 0; }
// Это нужно для работы phpBB template engine $phpbb_root_path = str_replace('\\', '/', realpath(__FILE__)); $phpbb_root_path = str_replace('includes/init.php', '', $phpbb_root_path); define('SN_ROOT_PHYSICAL', $phpbb_root_path); // Для работы автолоадера $phpbb_root_path .= 'design/'; $sn_root_relative = str_replace('\\', '/', getcwd()); $sn_root_relative .= $sn_root_relative[strlen($sn_root_relative) - 1] == '/' ? '' : '/'; $sn_root_relative = str_replace(SN_ROOT_PHYSICAL, '', $sn_root_relative); $sn_root_relative .= basename($_SERVER['SCRIPT_NAME']); $sn_root_relative = str_replace($sn_root_relative, '', $_SERVER['SCRIPT_NAME']); define('SN_ROOT_RELATIVE', $sn_root_relative); define('SN_ROOT_VIRTUAL', 'http' . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . SN_ROOT_RELATIVE); define('SN_ROOT_VIRTUAL_PARENT', str_replace('//google.', '//', SN_ROOT_VIRTUAL)); /** * Autoloader * * @param string $class * @return void */ spl_autoload_register(function ($class) { $file = SN_ROOT_PHYSICAL . 'includes/classes/' . str_replace('\\', '/', $class) . '.php'; if (file_exists($file)) { require_once $file; if (method_exists($class, 'init') && $class !== 'TicTacToe') { $class::init(); } } }); TicTacToe::init();
} else { if ($result == -1) { print "gl" . implode("", $g->board); } else { print "gd" . implode("", $g->board); } } } function validboard($b) { return strlen($b) == 9 && strlen(preg_replace('/[xo-]*/', "", $b)) == 0; } if (isset($square) && isset($board) && ctype_digit($square) && validboard($board)) { $board = str_split($board); $board[$square] = "x"; $game = new TicTacToe("o", $board); if ($game->terminal()) { endgame($game); } else { $moves = $game->next_states(); $min = 2; $next = $moves[0]; foreach ($moves as $g) { $curr = minimax($g); if ($curr <= $min) { $next = $g; $min = $curr; } } if ($next->terminal()) { endgame($next);
// Check each column foreach ($colDeck as $col) { $player = $col[0]; if ($player > 0 && $col[0] == $col[1] && $col[1] == $col[2]) { return $player; } } // Check 2 diagonal lines $player = $deck[1][1]; if ($player > 0 && ($deck[0][0] == $deck[1][1] && $deck[1][1] == $deck[2][2] || $deck[0][2] == $deck[1][1] && $deck[1][1] == $deck[2][0])) { return $player; } return 0; } public function printDeck() { foreach ($this->deck as $r => $row) { foreach ($row as $c => $p) { echo $p . " "; } echo "\n"; } echo "\n"; } public function debug($obj) { echo print_r($obj, true); } } $ticTacToe = new TicTacToe(); $ticTacToe->run();
<?php /** * Created by PhpStorm. * User: geol * Date: 6/14/15 * Time: 4:11 AM */ error_reporting(E_ALL); ini_set('display_errors', true); set_include_path('classes/'); spl_autoload_register(); $users = array('firstGamer', 'secondGamer'); $db = new DB(); $id = !empty($argv[1]) ? (int) $argv[1] : null; $game = new TicTacToe($db, $users, $id); while (1) { print '>>'; $input = str_replace("\n", "", fgets(STDIN)); if ($input) { if ($input == 'stop') { exit; } elseif ($input == 'save') { $game->saveGame(); } else { $stones = explode(" ", $input); $game->addStone((int) $stones[0], (int) $stones[1]); } } }