我正在尝试编写一个在1到100之间引发随机($secretNumber
)数字的php游戏,将其打印到屏幕上,打印一个带有按钮的表单,供用户选择“更高& #39; '下部'或者'纠正'。按下按钮后,外部值会相应地改变,计算机会在最低值和最高值之间产生另一个随机数。这一直持续到用户选择正确并且游戏重置为止。
我正在使用会话来确保每次单击按钮时都不会重置值。
我的问题是$secretNumber
在$_SESSION
锁定它之前重置了几次。
此外,$randomGuess
似乎在更新之前无法锁定。
关于为什么这不起作用的任何建议?
<?php session_start() ?>
<?php
$low = 1;
$high = 100;
//create a function to hold a random number, between 1 and 100 and save it
//to a session variable.
secretNumber();
function secretNumber(){ //********** secretNumber() **********
global $randomGuess;
if(!isset($_SESSION["secretNumber"])){
$secretNumber = rand(1,100);
$_SESSION['secretNumber'] = $secretNumber;
} else {
$secretNumber = $_SESSION["secretNumber"];
echo($_SESSION['secretNumber']);
}
echo 'Secret number is '.$secretNumber;
echo ' <br/> ';
} //********** end secretNumber() ***********
//create a function to create a random guess as to the value of the
//first random number
randomGuess();
function randomGuess() { //********** randomGuess() **********
global $randomGuess;
global $low;
global $high;
if (isset($_SESSION['low'])){
$low = $_SESSION['low'];
}
if (isset($_SESSION['high'])){
$high = $_SESSION['high'];
}
$randomGuess = round($high-(($high-$low)/2));
echo 'Random guess is '.$randomGuess;
echo "<br/>";
$_SESSION['randomGuess'] = $randomGuess;
} // ********** end randomGuess() **********
//create a function to provide a response as to whether the guess is low,
//high or correct, and adjust the limits of the random guess accordingly.
checkGuess();
function checkGuess() { // ********** checkGuess() **********
global $low;
global $high;
global $randomGuess;
// form to show a 'high' button, a 'low' button and a 'correct' button
// Correct button needs to reset $_SESSION["secretNumber"], and start
// process over.
print <<<HERE
<div>
<br/><br/><br/>
<form method = "post" action = "" style="width:35%;
padding-left:35%">
<fieldset>
<button type = "submit" value="high" name ="high">Too High</button> <br/><br/>
<button type = "submit" value="low" name = "low">Too Low</button> <br/><br/>
<button type = "submit" value="correct" name = "correct">Correct</button> <br/><br/>
</fieldset>
</form>
</div>
HERE;
if (isset($_POST['low'])) {
echo "low <br/>";
echo "randomGuess = ".$randomGuess. "<br/>";
unset($_SESSION["low"]);
$low = $randomGuess;
$_SESSION['low'] = $low;
unset($_SESSION["randomNumber"]);
}
if (isset($_POST['high'])){
echo "high <br/>";
echo "randomGuess = ".$randomGuess. "<br/>";
unset($_SESSION["high"]);
$high = $randomGuess;
$_SESSION['high'] = $high;
unset($_SESSION["randomNumber"]);
}
} // ********** end checkGuess() **********
if (isset($_POST['correct'])){
print <<<HERE
<h2>Yes I guessed the correct secret number. Your secret is out</h2>
HERE;
unset($_SESSION["secretNumber"]);// unset the random number as the guess was correct.
unset($_SESSION["high"]);
unset($_SESSION["low"]);
}//end if
echo "low =".$low."<br/>";
echo "high =".$high." <br/>";
?>
答案 0 :(得分:0)
尝试检查$secretNumber
是否与会话相同。
var_dump($secretNumber);
if($secretNumber != $_SESSION["secretNumber"]) {
echo "Restart is here";
}
在每个功能之后测试它,然后你就可以看到什么是问题。