Match sticks... simple php game.

Post all your tuts or request for tuts here.
Post Reply
User avatar
rockinliam
Posts: 466
Joined: Sun Jun 07, 2009 11:26 am

Match sticks... simple php game.

Post by rockinliam »

Hi, at my first day at college, we wrote a small game in my computing lesson using a visual flow chart, which was pretty cool. The game we made was a game called match sticks, where each player must keep removing either 1,2,3 matches until somebody picks up the last match, soo last night i got bored and decided to write it out in PHP, it manipulates simple sessions and uses simple calculations to run the game.

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>
game.php

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;
}
?>
The game is always set for the computer to win.
I hope you can learn something from this code, as it is a very simple game.
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Match sticks... simple php game.

Post by Jackolantern »

Pretty neat, but crazy pop-ups on that site and I bailed :P
The indelible lord of tl;dr
User avatar
rockinliam
Posts: 466
Joined: Sun Jun 07, 2009 11:26 am

Re: Match sticks... simple php game.

Post by rockinliam »

Lol maybe i have surpassed my ad free hosting limit lol but it shudnt affect the little game :P
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Match sticks... simple php game.

Post by Jackolantern »

With the numerous page reloads with the game, it was becoming a pop-up party :( Well, pop-under actually.

As far as the game goes, looks good :)
The indelible lord of tl;dr
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: Match sticks... simple php game.

Post by Rastan »

The AI in that game > me lol
Post Reply

Return to “Tutorials”