<? session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jellybean</title>
</head>
<body>
<?
if(!$_POST['step']){
?>
<form action="<?=$_SERVER['php_self']?>" method="post">
Select the number of beans in the jar. <input name="number" type="text">
<button type="submit" value="Play!">Play!</button>
<input type="hidden" name="step" value="1">
</form>
<?
}else{
if($_POST['step']==1){
//set random number
//ask user for their first guess
$_SESSION['r'] = rand(0,$_POST['number']);
?>
<form action="<?=$_SERVER['php_self']?>" method="post">
Make your first guess <input name="guess" type="text">
<button type="submit" value="Guess!">Guess!</button>
<input type="hidden" name="step" value="2">
</form>
<?
}else{
//check if guess is higher, lower or equal to the random number
if($_SESSION['r']<$_POST['guess']){
echo "guess too high";
guess();
}elseif($_SESSION['r']>$_POST['guess']){
echo "guess too low";
guess();
}else{
echo "correct guess";
}
}
}
function guess(){
?>
<form action="<?=$_SERVER['php_self']?>" method="post">
Guess again <input name="guess" type="text">
<button type="submit" value="Guess!">Guess!</button>
<input type="hidden" name="step" value="2">
</form>
<?
}
?>
</body>
</html>