I am trying to write a php game that raises an random ($secretNumber
) number between 1 and 100, prints it to the screen, prints a form with buttons for the user to select either 'higher' 'lower' or 'correct'. Upon a button press, the outside values are altered accordingly and the computer raises another random number between the lowest and highest values. This continues until the user select correct and the game resets.
I am using a session to ensure that the values don't reset each time a button is clicked.
My problem is that the $secretNumber
resets a couple of times before the $_SESSION
locks it in.
Also the $randomGuess
doesn't seem to lock in before updating.
Any suggestions as to why this isn't working?
<?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/>";
?>