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

huangapple go评论65阅读模式
英文:

I need help building this tic-tac-toe game using PHP

问题

我需要使用PHP创建一个井字棋游戏。这是我的初始代码,然而,在表格中单击一个方格只会刷新页面,而不会在棋盘上注册我的输入。如何使这段代码工作?

<!DOCTYPE html>
<html>
<head>
	<title>Tic Tac Toe</title>
	<style>
		/* 游戏棋盘的样式 */
		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
		// 初始化游戏棋盘
		$board = array(
			array("", "", ""),
			array("", "", ""),
			array("", "", "")
		);

		// 检查玩家是否赢得游戏
		function checkWin($board, $player) {
			// 检查水平方向是否获胜
			for ($i = 0; $i < 3; $i++) {
				if ($board[$i][0] == $player && $board[$i][1] == $player && $board[$i][2] == $player) {
					return true;
				}
			}

			// 检查垂直方向是否获胜
			for ($i = 0; $i < 3; $i++) {
				if ($board[0][$i] == $player && $board[1][$i] == $player && $board[2][$i] == $player) {
					return true;
				}
			}

			// 检查对角线是否获胜
			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;
			}

			// 未找到获胜情况
			return false;
		}

		// 检查游戏是否平局
		function checkTie($board) {
			for ($i = 0; $i < 3; $i++) {
				for ($j = 0; $j < 3; $j++) {
					if ($board[$i][$j] == "") {
						return false;
					}
				}
			}
			return true;
		}

		// 开始游戏
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
			// 获取玩家的移动
			$row = $_POST["row"];
			$col = $_POST["col"];

			// 更新游戏棋盘
			$board[$row][$col] = "X";

			// 检查玩家是否赢得游戏
			if (checkWin($board, "X")) {
				echo "<h1>You win!</h1>";
				exit();
			}

			// 检查游戏是否平局
			if (checkTie($board)) {
				echo "<h1>Tie game!</h1>";
				exit();
			}

			// 电脑的移动
			do {
				$row = rand(0, 2);
				$col = rand(0, 2);
			} while ($board[$row][$col] != "");
			$board[$row][$col] = "O";

			// 检查电脑是否赢得游戏
			if (checkWin($board, "O")) {
				echo "<h1>You lose!</h1>";
				exit();
			}

			// 检查游戏是否平局
			if (checkTie($board)) {
				echo "<h1>Tie game!</h1>";
				exit();
			}
		}
	?>

	<h1>Tic Tac Toe</h1>

	<table>
		<?php
			// 显示游戏棋盘
			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'来注册我的输入,但在单击后没有显示任何内容。

英文:

I need to create a tic-tac-toe game using PHP. Here is my initial code, however, clicking a tile in the table only refreshes the page and it does not register my input in the board. How can I make this code work?

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Tic Tac Toe&lt;/title&gt;
&lt;style&gt;
/* 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;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
// Initialize the game board
$board = array(
array(&quot;&quot;, &quot;&quot;, &quot;&quot;),
array(&quot;&quot;, &quot;&quot;, &quot;&quot;),
array(&quot;&quot;, &quot;&quot;, &quot;&quot;)
);
// Check if a player has won the game
function checkWin($board, $player) {
// Check for horizontal win
for ($i = 0; $i &lt; 3; $i++) {
if ($board[$i][0] == $player &amp;&amp; $board[$i][1] == $player &amp;&amp; $board[$i][2] == $player) {
return true;
}
}
// Check for vertical win
for ($i = 0; $i &lt; 3; $i++) {
if ($board[0][$i] == $player &amp;&amp; $board[1][$i] == $player &amp;&amp; $board[2][$i] == $player) {
return true;
}
}
// Check for diagonal win
if ($board[0][0] == $player &amp;&amp; $board[1][1] == $player &amp;&amp; $board[2][2] == $player) {
return true;
}
if ($board[0][2] == $player &amp;&amp; $board[1][1] == $player &amp;&amp; $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 &lt; 3; $i++) {
for ($j = 0; $j &lt; 3; $j++) {
if ($board[$i][$j] == &quot;&quot;) {
return false;
}
}
}
return true;
}
// Start the game
if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
// Get the player&#39;s move
$row = $_POST[&quot;row&quot;];
$col = $_POST[&quot;col&quot;];
// Update the game board
$board[$row][$col] = &quot;X&quot;;
// Check if the player won the game
if (checkWin($board, &quot;X&quot;)) {
echo &quot;&lt;h1&gt;You win!&lt;/h1&gt;&quot;;
exit();
}
// Check if the game is a tie
if (checkTie($board)) {
echo &quot;&lt;h1&gt;Tie game!&lt;/h1&gt;&quot;;
exit();
}
// Computer&#39;s move
do {
$row = rand(0, 2);
$col = rand(0, 2);
} while ($board[$row][$col] != &quot;&quot;);
$board[$row][$col] = &quot;O&quot;;
// Check if the computer won the game
if (checkWin($board, &quot;O&quot;)) {
echo &quot;&lt;h1&gt;You lose!&lt;/h1&gt;&quot;;
exit();
}
// Check if the game is a tie
if (checkTie($board)) {
echo &quot;&lt;h1&gt;Tie game!&lt;/h1&gt;&quot;;
exit();
}
}
?&gt;
&lt;h1&gt;Tic Tac Toe&lt;/h1&gt;
&lt;table&gt;
&lt;?php
// Display the game board
for ($i = 0; $i &lt; 3; $i++) {
echo &quot;&lt;tr&gt;&quot;;
for ($j = 0; $j &lt; 3; $j++) {
echo &quot;&lt;td onclick=\&quot;location.href=&#39;?row=$i&amp;col=$j&#39;\&quot;&gt;&quot;;
echo $board[$i][$j];
echo &quot;&lt;/td&gt;&quot;;
}
echo &quot;&lt;/tr&gt;&quot;;
}
?&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

i EXPECTED THE PROGRAM TO REGISTER MY INPUT BY PLACING AN 'X' in the table but it does not show anything upon clicking.

答案1

得分: 1

以下是翻译的内容:

你正在寻找 POST 请求方法,然后查找全局的 $_POST 变量中的值,但是当你使用你编写的 JavaScript 重定向用户到位置时,实际上并没有进行任何提交 -- 你只是将数据附加到URL上,即使用查询字符串。

要从URL获取数据,你需要使用全局的 $_GET 变量。现在,由于初始页面加载也是一个 GET 请求,你的 if 条件不能简单地检查它是否是 GET 请求方法,但你可以检查 rowcol 是否被设置,这在初始页面加载时不会发生。

例如:

if (isset($_GET['row'], $_GET['col'])) {
    // 获取玩家的移动
    $row = $_GET["row"];
    $col = $_GET["col"];
    // ...
}

但是需要注意的是,你将遇到另一个问题:你的数据不会持久保存。一旦进行另一个移动,之前的移动将丢失。

你有几种解决方法。可以研究一下 sessionscookies,然后在那里读取和写入你的棋盘,或者你可以通过使用数组将东西附加到URL(链接这些值):

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

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

英文:

You're looking for the POST request method and then for the values in the global $_POST variable, however when you redirect the user to the location with the JavaScript you have written, it's not actually posting anything -- you're simply appending data to the URL, i.e. using a query string.

To get the data from the URL, you need to use the global $_GET variable. Now, since the initial page load is also a GET request, your if condition can't simply check if it's a GET request method, but you might check if row and col are set, which would not be the case on initial page load.

For example:

if (isset($_GET[&#39;row&#39;], $_GET[&#39;col&#39;])) {
    // Get the player&#39;s move
    $row = $_GET[&quot;row&quot;];
    $col = $_GET[&quot;col&quot;];
    // ...
}

That said, you will run into another problem: your data doesn't persist. The moment you make another move, the previous moves will be lost.

You have a few options to solve this. Either look into sessions or cookies, and then read and write your board there, or you could keep appending things to the URL (chaining the values) by using an array:

echo &quot;&lt;td onclick=\&quot;location.href=&#39;?moves[]=1,1&amp;moves[]=0,0&#39;\&quot;&gt;&quot;;

Keep in mind that you'll have to add the computer's move to the URL as well.

huangapple
  • 本文由 发表于 2023年3月23日 09:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定