Jump to content


Photo

Counting Code


  • Please log in to reply
4 replies to this topic

#1 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 31 March 2006 - 07:35 PM

What PHP code could I use to count something like wins and loses?

#2 keithschm

keithschm
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 31 March 2006 - 07:41 PM

[!--quoteo(post=360454:date=Mar 31 2006, 02:35 PM:name=JackJack)--][div class=\'quotetop\']QUOTE(JackJack @ Mar 31 2006, 02:35 PM) View Post[/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?



NEED HELP

#3 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 31 March 2006 - 07:48 PM

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

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 April 2006 - 12:12 PM

Have a look at sessions. Just create three session win, lose and draw. Then just add a single line of code:
$_SESSION['win'] += 1;
when the user wins or do
$_SESSION['lose'] += 1;
or
$_SESSION['draw'] += 1;
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:
<?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>


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 April 2006 - 03:31 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users