Computer Science Canada

Rock paper scissors...

Author:  Squirter [ Wed Mar 24, 2004 7:52 am ]
Post subject:  Rock paper scissors...

Hey, I'm trying RPS with forms, here it is so far...

code:
<form METHOD="POST" ACTION="rpsform.php">

<INPUT TYPE="button" name="choice" value="rock">
<br>

<INPUT TYPE="button" name="choice" value="scissors"><br>


<INPUT TYPE="button" name="choice" value="paper">
</form>

Author:  Squirter [ Wed Mar 24, 2004 7:53 am ]
Post subject: 

And the script to compliment the first one...


The one below is rpsform.php.

code:

<?

$input = array("scissors", "paper", "rock");
$total_array = count($input);
$rand_keys = array_rand($input, $total_array);


if(isset($_POST['choice'])) {

        $choice = $_POST['choice'];




if ($input == 'rock' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> rock</b> <BR>
You lose!';
}
elseif ($input[$rand_keys[0]] == 'rock' && $choice == 'paper')
{
echo 'You chose <B> paper </b><br>
Computer chose <b>rock</b><BR>
You win!';
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> scissors</b> <BR>
You win!';
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'paper')
{
echo 'You chose <b>paper,</b><br>
Computer chose <B> scissors</b> <BR>
You lose!';
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> paper</b> <BR>
You lose!';
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> paper</b> <BR>
You win!';
} elseif($user == $input[$rand_keys[0]]) {
        echo 'It\'s a tie!';
} else {
        echo 'sorry, only <b>rock, paper and scissors</b> are allowed<br>its case-sensitive';
}


?>




Whenever I click the button though in the first script, nothing happens. Anyone know what's wrong?

Author:  Amailer [ Wed Mar 24, 2004 7:59 am ]
Post subject: 

ah, well you see for you to click a button and make it to submit stuff in the form, you have to make it's type 'SUBMIT'

code:

<form METHOD="POST" ACTION="rpsform.php">
<INPUT TYPE="submit" name="choice" value="rock"><br>
<INPUT TYPE="submit" name="choice" value="scissors"><br>
<INPUT TYPE="submit" name="choice" value="paper">
</form>

Author:  wtd [ Wed Mar 24, 2004 7:32 pm ]
Post subject: 

And just for fun, let's refactor!

Not tested.

code:
<html>
<head>
   <title>Rock, Paper, Scissors</title>
</head>
<body>
<?php
   $possible_choices = array(
      "rock" => array(
         "item" => "rock",
         "defeats" => "scissors",
         "defeated_by" => "paper";),
      "paper" => array(
         "item" => "paper",
         "defeats" => "rock",
         "defeated_by" => "scissors"),
      "scissors" => array(
         "item" => "scissors".
         "defeats" => "paper",
         "defeated_by" => "rock"));

   function form_submitted() {
      return isset($_REQUEST["choice"]);
   }

   function rps_computer_choice($possible_choices_array) {
      return array_rand($possible_choices_array);
   }

   function rps_user_wins($user_choice, $computer_choice) {
      $computers_choice = rps_computer_choice($possible_choices);
      if ($user_choice["defeats"] == $computers_choice["item"])
         return 1;
      elseif ($user_choice["item"] == $computers_choice["item"])
         return 0;
      elseif ($user_choice["defeated_by"] == $computers_choice["item"])
         return -1;
   }

   function rps_get_user_choice($possible_choices) {
      if (form_submitted()) {
         return $possible_choices[$_REQUEST["choice"]];
      } else {
         echo("I chose for you, since your input was wrong.<hr/>");
         return rps_computer_choice($possible_choices);
      }   
   }

   function rps_outcome_message($user_choice, $computer_choice) {
      switch (rps_user_wins($user_choice, $computer_choice)) {
         case 1:
            return "You win!";
         case 0:
            return "It's a tie!";
         case -1:
            return "Computer wins!";
      }
   }

   function rps_echo_choices($user_choice, $computer_choice) {
      echo("The computer chose " . $computers_choice["item"] . "<br/>");
      echo("You chose " . $user_choice["item"] . "<br/>");
   }

   // and here it is:

   if (form_submitted()) {
      $users_choice = rps_get_user_choice($possible_choices);
      $computer_choice = rps_computer_choice($possible_choices);

      rps_echo_choices($users_choice, $computer_choice);
      echo(rps_outcome_message($users_choice, $computer_choice));

      echo("<br/><hr/><br/>");
      echo("<b>Play again!</b>");
      echo("<br/><hr/><br/>");
   }
?>
   <FORM METHOD="POST" ACTION="<?= basename($PHP_SELF) ?>">
      <INPUT TYPE="button" name="choice" value="rock"/>
      <br/>
      <INPUT TYPE="button" name="choice" value="scissors"/>
      <br/>
      <INPUT TYPE="button" name="choice" value="paper"/>
   </FORM>
</body>
</html>

Author:  Squirter [ Fri Mar 26, 2004 8:34 pm ]
Post subject: 

Okay, now I'm using sessions on this as well.

This is rps1.php
code:

<form METHOD="POST" ACTION="rpsform.php">

<INPUT TYPE="submit" name="choice" value="rock">
<br>

<INPUT TYPE="submit" name="choice" value="scissors"><br>


<INPUT TYPE="submit" name="choice" value="paper">
<br>
<br>
<br>
</form>
<?

session_start();
if(!isset($_SESSION['wins'])) or
(!isset($_SESSION['losses'])) or
(!isset($_SESSION['ties']))
echo "Sorry, you have not started a session.  Play a game to start a session and keep your game record.";

else
echo "<center> Wins: <b> $_SESSION['wins']" </b> </center><br>

echo "<center> Losses: <b> $_SESSION['losses']" </b> </center><br>

echo "<center> ties: <b> $_SESSION['ties']" </b> </center><br>
?>

Author:  Squirter [ Fri Mar 26, 2004 8:36 pm ]
Post subject: 

And to compliment it, rpsform.php.

code:
<?
srand((float) microtime() * 10000000);
$input = array("scissors", "paper", "rock");
$total_array = count($input);
$rand_keys = array_rand($input, $total_array);


if(isset($_POST['choice'])) {

        $choice = $HTTP_POST_VARS['choice'];

session_start();
if(!isset($_SESSION['count'])) {
session_register('wins')
session_register('losses')
session_register('ties')
$HTTP_SESSION_VARS['wins'] = 0
$HTTP_SESSION_VARS['losses'] = 0
$HTTP_SESSION_VARS['ties'] = 0;



if ($input == 'rock' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> rock</b> <BR>
You lose!'
$HTTP_SESSION_VARS['losses'] ++;
}
elseif ($input[$rand_keys[0]] == 'rock' && $choice == 'paper')
{
echo 'You chose <B> paper </b><br>
Computer chose <b>rock</b><BR>
You win!'
$HTTP_SESSION_VARS['wins'] ++;
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> scissors</b> <BR>
You win!'
$HTTP_SESSION_VARS['wins'] ++;
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'paper')
{
echo 'You chose <b>paper,</b><br>
Computer chose <B> scissors</b> <BR>
You lose!'
$HTTP_SESSION_VARS['losses'] ++;
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> paper</b> <BR>
You lose!'
$HTTP_SESSION_VARS['losses'] ++;
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> paper</b> <BR>
You win!'
$HTTP_SESSION_VARS['win'] ++;
} elseif($user == $input[$rand_keys[0]]) {
        echo 'It\'s a tie!'
$HTTP_SESSION_VARS['ties'] ++;;
} else {
        echo 'sorry, only <b>rock, paper and scissors</b> are allowed<br>its case-sensitive';
}

}
<center> echo Wins: <b> $HTTP_SESSION_VARS['wins']</b> </center>;
<center> echo Losses: <b> $HTTP_SESSION_VARS['losses']</b> </center>;
<center> echo Ties: <b> $HTTP_SESSION_VARS['ties']</b> </center>;

<form METHOD="POST" ACTION="rps1.html">

<input type="submit" value="Play Again!"
</form>

?>


So this should display the wins, losses, and ties on both rps1.php and rpsform.php. But, it only shows the scores on rpsform.php. Anyone know what's wrong?

Author:  Amailer [ Sat Mar 27, 2004 4:23 pm ]
Post subject: 

for your rpsform.php
code:

<?
srand((float) microtime() * 10000000);
$input = array("scissors", "paper", "rock");
$total_array = count($input);
$rand_keys = array_rand($input, $total_array);

if(isset($_POST['choice'])) {

        $choice = $HTTP_POST_VARS['choice'];


if(!isset($_SESSION['count'])) {
session_start();

session_register('wins');
session_register('losses');
session_register('ties');


if(!isset($HTTP_SESSION_VARS['wins'])) $HTTP_SESSION_VARS['wins'] = 0;
if(!isset($HTTP_SESSION_VARS['ties'])) $HTTP_SESSION_VARS['ties'] = 0;
if(!isset($HTTP_SESSION_VARS['losses'])) $HTTP_SESSION_VARS['losses'] = 0;

if ($input[$rand_keys[0]] == 'rock' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> rock</b> <BR>
You lose!';
$HTTP_SESSION_VARS['losses']++;
}
elseif ($input[$rand_keys[0]] == 'rock' && $choice == 'paper')
{
echo 'You chose <B> paper </b><br>
Computer chose <b>rock</b><BR>
You win!';
$HTTP_SESSION_VARS['wins']++;
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> scissors</b> <BR>
You win!';
$HTTP_SESSION_VARS['wins']++;
}
elseif ($input[$rand_keys[0]] == 'scissors' && $choice == 'paper')
{
echo 'You chose <b>paper,</b><br>
Computer chose <B> scissors</b> <BR>
You lose!';
$HTTP_SESSION_VARS['losses']++;
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'rock')
{
echo 'You chose <b>rock,</b><br>
Computer chose <B> paper</b> <BR>
You lose!';
$HTTP_SESSION_VARS['losses']++;
}
elseif ($input[$rand_keys[0]] == 'paper' && $choice == 'scissors')
{
echo 'You chose <b>scissors,</b><br>
Computer chose <B> paper</b> <BR>
You win!';
$HTTP_SESSION_VARS['wins']++;
} elseif($choice == $input[$rand_keys[0]]) {
        echo 'It\'s a tie!';
$HTTP_SESSION_VARS['ties']++;;
} else {
        echo 'sorry, only <b>rock, paper and scissors</b> are allowed<br>its case-sensitive';
}

}


echo '<center> echo Wins: <b>'. $HTTP_SESSION_VARS['wins'] .'</b> </center>
<center> echo Losses: <b>'.  $HTTP_SESSION_VARS['losses'] .'</b> </center>
<center> echo Ties: <b>'. $HTTP_SESSION_VARS['ties'] .'</b> </center>';
} // if(!isset($_SESSION['count'])) {

echo '<form METHOD="POST" ACTION="rps1.php">
<input type="submit" value="Play Again!">
</form>';


?>


: