Created
September 17, 2008 10:14
-
-
Save lantins/11222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// more error details please. | |
error_reporting(E_ALL|E_STRICT); | |
ini_set('display_errors', true); | |
//setup session | |
ini_set('session.use_only_cookies', true); | |
session_name('easy_maths_game'); | |
session_start(); | |
//generate sum, plus store sum so that user can guess again if correct gues was made | |
function create_or_get_sum() | |
{ | |
if (isset($_SESSION['sum']) == false) { | |
$number1 = rand(1,10); | |
$number2 = rand(1,10); | |
$sum = $number1 + $number2; | |
$_SESSION['sum'] = $sum; | |
$_SESSION['number1'] = $number1; | |
$_SESSION['number2'] = $number2; | |
} | |
$sum = $_SESSION['sum']; | |
$number1 = $_SESSION['number1']; | |
$number2 = $_SESSION['number2']; | |
return array($sum, $number1, $number2); | |
} | |
//check guess is same as sum answer | |
function check_guess($guess) | |
{ | |
$sum = create_or_get_sum(); | |
if ($guess == $sum[0]) { | |
// unset the sum for a new game | |
unset($_SESSION['sum']); | |
return true; | |
} | |
return false; | |
} | |
// we call this; encase its the first time the user has accessed the site. | |
list($sum, $number1, $number2) = create_or_get_sum(); | |
$winner = false; | |
$guess = false; | |
// see if the user has guessed at the number. | |
if (isset($_POST['guess'])) { | |
$guess = $_POST['guess']; | |
$winner = check_guess($guess); | |
} | |
?> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
<head> | |
<title>Easy Maths Game</title> | |
</head> | |
<body> | |
<h1>Easy Maths Game</h1> | |
<?php if ($winner == false): ?> | |
<p> | |
Answer this easy maths question:<br /> | |
<strong><?php echo $number1 ?> + <?php echo $number2 ?> = ????</strong> | |
</p> | |
<?php if ($guess != false): ?> | |
<p> | |
<strong>You guessed:</strong> <?php echo $guess ?><br /> | |
Unfortunately for you loser thats the wrong answer. | |
</p> | |
<?php endif; ?> | |
<form method="post" action="/easy_maths_game.php"> | |
<p>Your Guess: <input type="text" name="guess" /></p> | |
<p><input type="submit" /></p> | |
</form> | |
<?php else: ?> | |
<p><strong>Your a winner!</strong> You successfully answered correctly!</p> | |
<p><a href="/easy_maths_game.php">Click Here</a> to play again.</p> | |
<?php endif; ?> | |
<hr /> | |
<h5>Debug Data</h5> | |
<pre><?php var_dump($_SESSION) ?></pre> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment