http://radioboo.co.uk/matches/
Here is the code:
Index.php
Code: Select all
<?php
session_start();
if (isset ($_SESSION['total']))
{
$total=$_SESSION['total'];
}
else
{
$_SESSION['total']=21;
$total=$_SESSION['total'];
}
?>
<?php
echo "Matches remaining: $total";
?>
<html>
<body>
<form action="game.php" method="post">
Enter a number between 1 and 3: <input type="text" name="playerinput" size="1" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
</html>Code: Select all
<?php
session_start();
if (isset ($_SESSION['total']))
{
$total=$_SESSION['total'];
}
else
{
echo "A disaster has occured, run away, save yourself!";
exit;
}
$player=$_POST['playerinput'];
$player=strip_tags($player);
$computer=(4 - $player);
$remainder1 = $total - ($player + $computer);
if ($remainder1 > "1")
{
if (is_numeric ($player))
{
if ($player > "3")
{
echo "You picked up to many matches!<br>";
echo "<a href='index.php'>Return</a>";
exit;
}
if ($player < "1")
{
echo "You picked up to few matches!<br>";
echo "<a href='index.php'>Return</a>";
exit;
}
echo "You took: $player <br>";
echo "AI took: $computer <br>";
echo "Matches remaining: $remainder1 <br> ";
echo "<a href='index.php'>Return</a>";
unset ($_SESSION['total']);
$_SESSION['total']=$remainder1;
$total=$_SESSION['total'];
}
else
{
echo "You used letters.</a>";
echo "<a href='index.php'>Return</a>";
exit;
}
}
else
{
echo " You lose!";
unset ($_SESSION['total']);
$_SESSION['total']=21;
echo "<a href='index.php'>Replay?</a>";
exit;
}
?>I hope you can learn something from this code, as it is a very simple game.