我正在尝试构建一个简单的Rock,Paper,Scissors游戏。我正在使用XAMPP堆栈。我已成功检索到用户选择的岩石,纸张或剪刀,并将其插入表格中。我也成功地从对手那里选择了一个随机的选择"通过使用array_rand但我不能同时将此变量/整数插入表中。我曾经有能力插入对手的选择"但从来没有与用户选择,它总是一个或另一个。并且,我认为我会将两个选项转换为整数,然后使用if / else语句来确定每个游戏的赢家,但是在我对此进行测试时,它似乎并不总能产生正确的答案。我认为可能有一些不必要的代码,例如我在那里的var_dump,所以我可以看到我已经成功地将选项转换为整数,以便我可以比较选择以便在逻辑上确定胜利者,例如。谢谢你的帮助。
<html>
<body>
<p> Make your move! Choose:</p>
<p> Paper, Rock, or Scissors</p>
<form action="rps.php" method="post" id="playform">
<select name="playermove" id="quantity">
<option value="1" selected="selected">Rock</option>
<option value="2">Paper</option>
<option value="3">Scissors</option>
</select>
</form>
<button type="submit" form="playform" value="submit">1, 2, 3....GO!
</button>
</body>
</html>
<?php
require 'connection.php';
$conn = Connect();
$pmove = $conn->real_escape_string($_POST['playermove']);
$omove = $conn->(['oppmove']);
$query = "INSERT into rpstwo.tb_rps2 (playermove) VALUES ('" . $pmove .
"')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
$yourvar = intval($pmove);
echo "You chose: " . $yourvar . "<br>";
var_dump($yourvar);
echo "<br>";
$oppdecision = array('rock', 'paper', 'scissors');
$oppresult = array_rand($oppdecision, 1);
echo "Your opponent chose: " . $oppresult . "<br>";
$oppsmove = $oppdecision[$oppresult];
echo $oppsmove . "<br>";
$oppvar = intval($oppresult);
var_dump($oppvar);
$sql = "INSERT into rpstwo.tb_rps2 (oppmove) VALUES ('" . $omove .
"')";
$success = $conn->query($sql);
if ($yourvar = 1 & $oppvar = 0) {
echo '<br>' . "you tie";
} elseif ($yourvar = 1 & $oppvar = 1) {
echo '<br>' . "you lose";
} elseif ($yourvar = 1 & $oppvar = 2) {
echo '<br>' . "you win";
} else {
echo '<br>' . "Good game!";
}
$conn->close();
?>
答案 0 :(得分:0)
以下代码将您的发布内容修复为数据库。它不能解决WIN / LOSE / DRAW可能性的问题。您还应该知道,您的代码会为playermove和text(rock,paper,scissors)发布一个整数,供对手移动。您有两个Insert语句将导致每个游戏创建2个单独的行。
$pmove = $_POST['playermove'];
$yourvar = intval($pmove);
echo "You chose: " . $yourvar . "<br>";
echo "<br>";
$oppdecision = array('rock', 'paper', 'scissors');
$oppresult = array_rand($oppdecision, 1);
echo "Your opponent chose: " . $oppresult . "<br>";
$oppsmove = $oppdecision[$oppresult];
echo $oppsmove . "<br>";
$oppvar = intval($oppresult);
$sql = "INSERT into tb_rps2 (oppmove,playermove) VALUES ('$oppresult','$pmove')";
$success = $conn->query($sql);
if ($yourvar = 1 & $oppvar = 0) {
echo '<br>' . "you tie";
} elseif ($yourvar = 1 & $oppvar = 1) {
echo '<br>' . "you lose";
} elseif ($yourvar = 1 & $oppvar = 2) {
echo '<br>' . "you win";
} else {
echo '<br>' . "Good game!";
}
$conn->close();