I refined the last poster's code a little to use arrays (and some embellishments using CSS):
<?php
session_start();
$rps = array('Rock','Paper','Scissors');
$wins = array('Rock vs Scissors','Scissors vs Paper', 'Paper vs Rock');
$loses = array('Rock vs Paper','Paper vs Scissors','Scissors vs Rock');
?>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
< html>
<head>
<title>Rock, Paper, Scissors</title>
<style type="text/css" media="screen">
body, html {
font-size: 100%;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
}
#hdr {
display: block;
width: 95%;
margin-left: auto;
margin-right: auto;
padding-top: .5em;
padding-bottom: .5em;
border-bottom: 1px solid black;
}
#rop {
display: block;
width: 95%;
margin-left: auto;
margin-right: auto;
margin-top: 1em;
}
h1 {
text-align: center;
padding: 0;
margin: 0;
}
a {
text-decoration: none;
color: Red;
background-color: White;
font-weight: bold;
}
a:hover {
color: white;
background-color: Red;
}
.bold {
font-weight: bold;
}
.win {
color: Lime;
font-weight: bold;
}
.lost {
color: Red;
font-weight: bold;
}
.draw {
color: Black;
font-weight: bold;
}
</style>
</head>
<body>
<div id="hdr">
<h1>Rock, Paper, Scissors</h1>
</div>
<div id="rop">
<?php
//first check that our wins, loses and draws sessions aren't set so we dont reset them everytime we play agaion as we want to
//keep our total wins, loses and draws
if(!isset($_SESSION['wins']) || !isset($_SESSION['loses']) || !isset($_SESSION['draws']))
{
$_SESSION['wins'] = 0; // these should be set to zero, not 'no';
$_SESSION['loses'] = 0;
$_SESSION['draws'] = 0;
}
$computer = rand(0,2);
$choice = $rps[$computer];
//This here is a normal if/else statement but its just an inline if/else statment
$action = (isset($_GET['action']) ? $_GET['action'] : '');
if ($action != '') { // don't do the check if the user didn't supply an action
echo "You Choose <span style='bold'>$action</span> vs. Computer Choose <span style='bold'>$choice</span>";
if ($action == $choice) { // we have a draw
$_SESSION['draws']++;
$cls = 'draw';
$str = 'No one won';
}
if (in_array($action . ' vs ' . $choice, $wins)) { // User wins
$_SESSION['wins']++;
$cls = 'win';
$str = "You Won"; }
if (in_array($action . ' vs ' . $choice, $loses)) { // Computer wins
$_SESSION['loses']++;
$cls = 'lost';
$str = "You Lost"; }
echo '<br /><span class="' . $cls . '">' . $str . '</span>';
echo "<br /><hr><br />";
echo "You have won a total of <b>" . $_SESSION['wins'] . "</b> times<br />";
echo "You have lost a total of <b>" . $_SESSION['loses'] . "</b> times<br />";
echo "You have drew a total of <b>" . $_SESSION['draws'] . "</b> times<br /><br />";
}
?>
Pick one:
<a href="?action=Rock">Rock</a> <a href="?action=Paper">Paper</a> <a href="?action=Scissors">Scissors</a>
</div>
</body>
</html>
If you look at the basic code, it has been shortened considerably.
I tested this and it works. On the DOCTYPE and following line, remove the space after the "<" which I had to put in to get the code to post correctly. :-(
Ken