Ejemplo n.º 1
0
require_once 'classes.inc.php';
$host = 'localhost';
$database = 'tictactoe';
$user = '******';
$pswd = '';
$link = mysql_connect($host, $user, $pswd) or die("Не удалось подключиться к MySQL.");
mysql_select_db($database) or die("Не удалось подключиться к БД");
$id = (int) $_GET['id'];
$currentPlayer = (int) $_GET['currentPlayer'];
if ($id == NULL || !isset($_GET['id'])) {
    if (isset($_GET['sizeOfField'])) {
        $sizeOfField = $_GET['sizeOfField'];
        $title = $_GET['title'];
        $sizeOfField += 0;
        $game = new Gamefield($sizeOfField);
        $query = "INSERT INTO `tictactoe`.`gamefield` \n    (`id`, `rowSize`, `fieldSize`, `cells`, `winnerCells`, `currentPlayer`,`winner`, `title`, `playersNumber`, `time`) \n    VALUES (NULL, '" . $game->getRowSize() . "', '" . $game->getFieldSize() . "', '', '', '1', '0', '" . $title . "', '1', '" . time() . "')";
        mysql_query($query);
        $id = mysql_insert_id();
    }
} else {
    $query = sprintf("SELECT * FROM `tictactoe`.`gamefield` \n        WHERE id='%s'", mysql_real_escape_string($id));
    $res = mysql_query($query);
    $row = mysql_fetch_assoc($res);
    if ($row['winner'] != 0) {
        if ($row['winner'] == $currentPlayer) {
            echo 'You winner<br>';
        } else {
            echo 'You loser<br>';
        }
    } else {
        if ($row['currentPlayer'] != $currentPlayer) {
Ejemplo n.º 2
0
$database = 'tictactoe';
$user = '******';
$pswd = '';
$link = mysql_connect($host, $user, $pswd) or die("Не удалось подключиться к MySQL.");
mysql_select_db($database) or die("Не удалось подключиться к БД");
$id = (int) $_GET['id'];
$currentPlayer = (int) $_GET['currentPlayer'];
$query = sprintf("SELECT * FROM `tictactoe`.`gamefield` WHERE id='%s'", mysql_real_escape_string($id));
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$game = new Gamefield();
$game->initGame($row);
$size = $game->getFieldSize();
$cells = $game->getCells();
$winnerCells = $game->getWinnerCells();
$params = $_GET + $_POST;
$action = $params['action'];
if ($action == 'move') {
    $game->makeMove((int) $params['x'], (int) $params['y']);
    $t = $game->getCells();
    $str_cells = base64_encode(serialize($t));
    $t = $game->getWinnerCells();
    $str_winnerCells = base64_encode(serialize($t));
    $number = (int) 1;
    if ($currentPlayer == 1) {
        $number = (int) 2;
    }
    $query = "UPDATE `tictactoe`.`gamefield`  \n                SET rowSize=" . $game->getRowSize() . ",\n                fieldSize=" . $game->getFieldSize() . ",\n                cells='" . $str_cells . "',\n                winnerCells='" . $str_winnerCells . "',\n                currentPlayer=" . $game->getCurrentPlayer() . ", \n                winner=" . $game->getWinner() . "  \n                 WHERE id=" . $id;
    mysql_query($query) or trigger_error(mysql_error() . " in " . $query);
    header("Location: game.php?id=" . $id . "&currentPlayer=" . $currentPlayer);
}