我需要帮助使用 PHP 构建这个井字游戏

问题描述 投票:0回答:1

我需要使用 PHP 创建一个井字游戏。这是我的初始代码,但是,单击表格中的图块只会刷新页面,而不会在面板中注册我的输入。我怎样才能使这段代码工作?

<!DOCTYPE html>
<html>
<head>
    <title>Tic Tac Toe</title>
    <style>
        /* Styling for the game board */
        table {
            border-collapse: collapse;
            margin: auto;
            font-size: 25px;
            font-weight: bold;
        }
        td {
            width: 50px;
            height: 50px;
            border: 1px solid black;
            text-align: center;
            vertical-align: middle;
            cursor: pointer;
        }
        td:hover {
            background-color: #ccc;
        }
    </style>
</head>
<body>
    <?php
        // Initialize the game board
        $board = array(
            array("", "", ""),
            array("", "", ""),
            array("", "", "")
        );

        // Check if a player has won the game
        function checkWin($board, $player) {
            // Check for horizontal win
            for ($i = 0; $i < 3; $i++) {
                if ($board[$i][0] == $player && $board[$i][1] == $player && $board[$i][2] == $player) {
                    return true;
                }
            }

            // Check for vertical win
            for ($i = 0; $i < 3; $i++) {
                if ($board[0][$i] == $player && $board[1][$i] == $player && $board[2][$i] == $player) {
                    return true;
                }
            }

            // Check for diagonal win
            if ($board[0][0] == $player && $board[1][1] == $player && $board[2][2] == $player) {
                return true;
            }
            if ($board[0][2] == $player && $board[1][1] == $player && $board[2][0] == $player) {
                return true;
            }

            // No win found
            return false;
        }

        // Check if the game is a tie
        function checkTie($board) {
            for ($i = 0; $i < 3; $i++) {
                for ($j = 0; $j < 3; $j++) {
                    if ($board[$i][$j] == "") {
                        return false;
                    }
                }
            }
            return true;
        }

        // Start the game
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Get the player's move
            $row = $_POST["row"];
            $col = $_POST["col"];

            // Update the game board
            $board[$row][$col] = "X";

            // Check if the player won the game
            if (checkWin($board, "X")) {
                echo "<h1>You win!</h1>";
                exit();
            }

            // Check if the game is a tie
            if (checkTie($board)) {
                echo "<h1>Tie game!</h1>";
                exit();
            }

            // Computer's move
            do {
                $row = rand(0, 2);
                $col = rand(0, 2);
            } while ($board[$row][$col] != "");
            $board[$row][$col] = "O";

            // Check if the computer won the game
            if (checkWin($board, "O")) {
                echo "<h1>You lose!</h1>";
                exit();
            }

            // Check if the game is a tie
            if (checkTie($board)) {
                echo "<h1>Tie game!</h1>";
                exit();
            }
        }
    ?>

    <h1>Tic Tac Toe</h1>

    <table>
        <?php
            // Display the game board
            for ($i = 0; $i < 3; $i++) {
                echo "<tr>";
                for ($j = 0; $j < 3; $j++) {
                    echo "<td onclick=\"location.href='?row=$i&col=$j'\">";
                    echo $board[$i][$j];
                    echo "</td>";
                }
                echo "</tr>";
            }
        ?>
    </table>

</body>
</html>

我期望程序通过在表格中放置一个“X”来注册我的输入,但点击后它没有显示任何内容。

php html css web tic-tac-toe
1个回答
1
投票

您正在寻找

POST
请求方法,然后寻找全局
$_POST
变量中的值,但是当您使用您编写的 JavaScript 将用户重定向到该位置时,它实际上并没有发布任何内容——您只需将数据附加到 URL,即使用查询字符串。

要从URL中获取数据,需要使用全局

$_GET
变量。现在,由于初始页面加载也是一个
GET
请求,您的
if
条件不能简单地检查它是否是一个
GET
请求方法,但您可以检查是否设置了
row
col
,这初始页面加载时不会出现这种情况。

例如:

if (isset($_GET['row'], $_GET['col'])) {
    // Get the player's move
    $row = $_GET["row"];
    $col = $_GET["col"];
    // ...
}

也就是说,你会遇到另一个问题:你的数据不会持久化。当你再次移动的那一刻,之前的移动将会丢失。

你有几个选择来解决这个问题。要么查看 sessionscookies,然后在那里读写你的板,或者你可以通过使用数组将内容附加到 URL(链接值):

echo "<td onclick=\"location.href='?moves[]=1,1&moves[]=0,0'\">";

请记住,您还必须将计算机的移动添加到 URL。

© www.soinside.com 2019 - 2024. All rights reserved.