Jump to content

Counting Code


JackJack

Recommended Posts

[!--quoteo(post=360454:date=Mar 31 2006, 02:35 PM:name=JackJack)--][div class=\'quotetop\']QUOTE(JackJack @ Mar 31 2006, 02:35 PM) [snapback]360454[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What PHP code could I use to count something like wins and loses?
[/quote]

well where is the data?

and how is it getting there?


Link to comment
Share on other sites

Basically I would like to have it so that everytime you win lose or draw it adds to your total
and when you go off the webpage it resets your count or won, lost and drawn

Here is the code


<?php


$computer = rand(1,3);
{
if ($computer == 1) $choice = 'Rock' ;
if ($computer == 2) $choice = 'Paper' ;
if ($computer == 3) $choice = 'Scissors' ;
}

$action = $_GET['action'];



if ($action == 'Rock' && $choice == 'Rock') {

echo "
<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won</html>";
exit;
}
if ($action == 'Rock' && $choice == 'Paper') {

echo "
<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost</html>";
exit;
}
if ($action == 'Rock' && $choice == 'Scissors') {

echo "
<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose $action vs. Computer Choose <b>$choice</b> <br> You Won</html>";
exit;
}
if ($action == 'Paper' && $choice == 'Rock') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won";
exit;
}
if ($action == 'Paper' && $choice == 'Scissors') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";
exit;
}
if ($action == 'Paper' && $choice == 'Paper') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";
exit;
}
if ($action == 'Scissors' && $choice == 'Rock') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";
exit;
}
if ($action == 'Scissors' && $choice == 'Paper') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won";
exit;
}
if ($action == 'Scissors' && $choice == 'Scissors') {

echo "<html>
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>
<br>
You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";
exit;
}
?>
<html>
<body link="#000000" vlink="#000000" alink="#000000">
<center>
<a href='http://www.battlemachines.net/test/rps.php?action=Rock'><img src='http://www.battlemachines.net/test/Rock.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Paper'><img src='http://www.battlemachines.net/test/Paper.bmp'></a>
<a href='http://www.battlemachines.net/test/rps.php?action=Scissors'><img src='http://www.battlemachines.net/test/Scissors.bmp'></a>

</html>



Thank You
Link to comment
Share on other sites

Have a look at sessions. Just create three session win, lose and draw. Then just add a single line of code:[code]$_SESSION['win'] += 1;[/code] when the user wins or do [code]$_SESSION['lose'] += 1;[/code] or [code]$_SESSION['draw'] += 1;[/code] when the user draws.

I ahve implemented this into your script, aswell as recode it. You'll soon see the difference and your code is alot more slimmer than before! Heres you new code:[code]<?php
session_start();
?>
<html>
<body link="#000000" vlink="#000000" alink="#000000">
<center>
<?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'] = 'no';
    $_SESSION['loses'] = 'no';
    $_SESSION['draws'] = 'no';
}

$computer = rand(1,3);

if ($computer == 1) $choice = 'Rock';
if ($computer == 2) $choice = 'Paper';
if ($computer == 3) $choice = 'Scissors';

//This here is a normal if/else statement but its just an inline if/else statment
$action = (isset($_GET['action']) ? $_GET['action'] : '');

if ($action == 'Rock' && $choice == 'Rock') {
    echo "You Choose $action vs. Computer Choose $choice<br />No One Won";

    //we drew here so lets increse the draws sesion by one
    $_SESSION['draws'] += 1;
}
elseif ($action == 'Rock' && $choice == 'Paper') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";

    //we lost here so lets increse the loses sesion by one
    $_SESSION['loses'] += 1;
}
elseif ($action == 'Rock' && $choice == 'Scissors') {
    echo "You Choose $action vs. Computer Choose <b>$choice</b> <br> You Won</html>";

    //we won here so lets increse the wins sesion by one
    $_SESSION['wins'] += 1;
}
elseif ($action == 'Paper' && $choice == 'Rock') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b><br> You Won";

    $_SESSION['wins'] += 1;
}
elseif ($action == 'Paper' && $choice == 'Scissors') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";

    $_SESSION['loses'] += 1;
}
elseif ($action == 'Paper' && $choice == 'Paper') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";

    $_SESSION['draws'] += 1;
}
elseif ($action == 'Scissors' && $choice == 'Rock') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Lost";

    $_SESSION['loses'] += 1;
}
elseif ($action == 'Scissors' && $choice == 'Paper') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> You Won";

    //we won here so lets increse the wins sesion by one
    $_SESSION['wins'] += 1;
}
elseif ($action == 'Scissors' && $choice == 'Scissors') {
    echo "You Choose <b>$action</b> vs. Computer Choose <b>$choice</b> <br> No One Won";

     $_SESSION['loses'] += 1;
}

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 />";

?>

<a href="?action=Rock"><img src="Rock.bmp"></a>
<a href="?action=Paper"><img src="Paper.bmp"></a>
<a href="?action=Scissors"><img src="Scissors.bmp"></a>
</html>[/code]
Link to comment
Share on other sites

I refined the last poster's code a little to use arrays (and some embellishments using CSS):
[code]<?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>[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.