I Need debugging and validation help

The following is a simple game of Rock, Paper, Scissors, Lizard, Spock. The program now runs (mostly) but there are still problems. At the moment there are two sections commented out. The first section is intended to display the results of the hand and the second is to track the game stats. With those sections commented out the program will run but displays nothing in the browser when they are included. One other issue I need to resolve is making the for values required. It will not be good if the user can click submit(play) without selecting anything. I found something at w3 schools site, but was not able to make it work.

Please share your comments on this. There is still some house keeping and some finishing touches. But I would like to resolve the issues above before starting to wrap things up.

Thanks in advance!

[php]

<?php $human = human_play(); $computer = computer_play(); echo $human."
".$computer; $win = $lose = $draw = 0;

if ($human == $computer){
$draw = 1;
echo "Draw!
We both played ".$human;
}else{
switch($human){
case ‘Rock’:
switch ($computer){
case ‘Paper’: $outcome = “Lose”;$action = “Covers”;break;
case ‘Scissors’: $outcome = “Win”;$action = “Crushes”;break;
case ‘Lizard’: $outcome = “Win”;$action = “Crushes”;break;
case ‘Spock’: $outcome = “Lose”;$action = “Vaporizes”;break;
}
break;
case ‘Paper’:
switch ($computer){
case ‘Rock’: $outcome = “Win”;$action = “Covers”;break;
case ‘Lizard’: $outcome = “Lose”;$action = “Eats”;break;
case ‘Scissors’: $outcome = “Lose”;$action = “Cuts”;break;
case ‘Spock’: $outcome = “Win”;$action = “Disproves”;break;
}
break;
case ‘Scissors’:
switch ($computer){
case ‘Rock’: $outcome = “Lose”;$action = “Crushes”;break;
case ‘Paper’: $outcome = “Win”;$action = “Cuts”;break;
case ‘Lizard’: $outcome = “Win”;$action = “Decapitates”;break;
case ‘Spock’: $outcome = “Lose”;$action = “Smashes”;break;
}
break;
case ‘Lizard’:
switch ($computer){
case ‘Rock’: $outcome = “Lose”;$action = “Crushes”;break;
case ‘Paper’: $outcome = “Win”;$action = “Eats”;break;
case ‘Scissors’: $outcome = “Lose”;$action = “Decapitates”;break;
case ‘Spock’: $outcome = “Win”;$action = “Poisons”;break;
}
break;
case ‘Spock’:
switch ($computer){
case ‘Rock’: $outcome = “Win”;$action = “Vaporizes”;break;
case ‘Paper’: $outcome = “Lose”;$action = “Disproves”;break;
case ‘Scissors’: $outcome = “Win”;$action = “Smashes”;break;
case ‘Lizard’: $outcome = “Lose”;$action = “Poisons”;break;
}
break;

		}    
	}

echo “
Outcome: “.$outcome.” Action: “.$action.”
”;
/* commented out - contains trouble
if ($outcome === ‘Win’){
$win = 1;
echo "
You “.$outcome.”!!
Your “.$human.” “.$action.” my “.$computer”.
";
}else{
$lose = 1;
echo "
You “.$outcome.”!!
My “.$computer.” “.$action.” your “.$human”.
";
}
*/
if ($outcome ==‘Win’){
$win = 1;
}elseif ($outcome ==‘Lose’){
$lose = 1;
}else{
$draw = 1;
};

echo $win." “.$lose.” ".$draw;
//game_statistics($win, $lose, $draw);

function human_play(){
echo <<<_END

Let's Play Rock, Paper, Lizard, Spock


Rock Paper
Scissors Lizard
Spock

_END;

$human = $_POST[‘human’];
return $human;
}
function computer_play(){
$play = rand(1,5);
switch ($play) {
case 1:
$computer = “Rock”;
return $computer;
case 2:
$computer = “Paper”;
return $computer;
case 3:
$computer = “Scissors”;
return $computer;
case 4:
$computer = “Lizard”;
return $computer;
case 5:
$computer = “Spock”;
return $computer;
}
}
/*
function game_statistics($win, $lose, $draw){
static $games = $won = $loss = $ddraw = 0

$won = $won + $win;
$loss = $loss +  $lose;
$ddraw = $ddraw + $draw;
echo $won." ".$loss." ".$ddraw;
}

*/
?>

[/php]

I try to put the majority of the PHP above the Doctype (HTML) and I just created a simple version of the game of rock, paper, and scissors. Here it is:

[php]<?php
/* 1 for win, 2 for tie, -1 for loss /
/
Setup an array to that shows what each item will do against the others */
$rock = array(‘rock’ => 0, ‘paper’ => 1, ‘scissors’ => -1);
$paper = array(‘rock’ => -1, ‘paper’ => 0, ‘scissors’ => 1);
$scissors = array(‘rock’ => 1, ‘paper’ => -1, ‘scissors’ => 0);

function computer_pick() {
$computer_chose = array( 1 => ‘rock’, 2 => ‘paper’, 3 => ‘scissors’);
$pick = rand(1,3);
return $computer_chose[$pick];
}

$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_SPECIAL_CHARS);

if (isset($submit) && $submit == ‘Submit’) {
$computer = computer_pick();

/* Used Variable variables - http://php.net/manual/en/language.variables.variable.php */
$compare = $$computer;	

$pick = filter_input(INPUT_POST, 'human', FILTER_SANITIZE_SPECIAL_CHARS);

$message = human($pick, $computer, $compare);
}

function human($pick, $computer, $compare) {
/* See who wins */
$status = $compare[$pick];

/* Store the $result */
switch ($status) {
case 1:
$result = ‘Wins’;
break;
case 0:
$result = ‘ties’;
break;
case -1:
$result = ‘Loses’;
break;
}

/* Return the result in a message */
return 'Computer picked <span class="computer">' . $computer . '</span> and Player picked <span class="player">' . $pick . '</span>, the Player ' . $result;

}

?>

Rock, Paper and Scissors .computer { color: #C60; } .player { color: magenta; } .win { color: green; } .tie { color: blue; } .lose { color: red; } Rock
Paper
Scissors

<?php echo (isset($message)) ? $message : 'Waiting...'; ?>

[/php]

Maybe you can get some inspiration from it. :wink:

If you want to have a score for example then you will have to get into looking at $_SESSION to have the values retain from pick to pick. Force the player to select something by having one value checked, for example what I did in my simple game.

Thank you for your suggestion. There is still some cosmetic work to be done, but it is functional now.

Would anyone be able to recommend a a good book or books to learn php? I have only been working with it for about three weeks and want to learn much more. Regretfully, the book I was using was seriously out of date.

[php]

<?php

//initialize stats array
$stats = array();
$stats[‘Win’] = 0;
$stats[‘Lose’] = 0;
$stats[‘Draw’] = 0;
$stats[‘games’] = 0;

if(isset($_POST[‘stats’]))
{
if(isset($_POST[‘stats’][‘Win’])) $stats[‘Win’] = $_POST[‘stats’][‘Win’];
if(isset($_POST[‘stats’][‘Lose’])) $stats[‘Lose’] = $_POST[‘stats’][‘Lose’];
if(isset($_POST[‘stats’][‘Draw’])) $stats[‘Draw’] = $_POST[‘stats’][‘Draw’];
if(isset($_POST[‘stats’][‘games’])) $stats[‘games’] = $_POST[‘stats’][‘games’];
}
?>

<?php // if human has a value get computer play, compare results and echo results if(isset($_POST['human'])){ $human = $_POST['human']; $computer = computer_play(); $win = $lose = $draw = 0; if(isset($_POST['human'])){ if ($human == $computer){ $draw = 1; $outcome = 'Draw'; }else{ switch($human){ case 'Rock': switch ($computer){ case 'Paper': $outcome = "Lose";$action = "Covers";break; case 'Scissors': $outcome = "Win";$action = "Crushes";break; case 'Lizard': $outcome = "Win";$action = "Crushes";break; case 'Spock': $outcome = "Lose";$action = "Vaporizes";break; } break; case 'Paper': switch ($computer){ case 'Rock': $outcome = "Win";$action = "Covers";break; case 'Lizard': $outcome = "Lose";$action = "Eats";break; case 'Scissors': $outcome = "Lose";$action = "Cuts";break; case 'Spock': $outcome = "Win";$action = "Disproves";break; } break; case 'Scissors': switch ($computer){ case 'Rock': $outcome = "Lose";$action = "Crushes";break; case 'Paper': $outcome = "Win";$action = "Cuts";break; case 'Lizard': $outcome = "Win";$action = "Decapitates";break; case 'Spock': $outcome = "Lose";$action = "Smashes";break; } break; case 'Lizard': switch ($computer){ case 'Rock': $outcome = "Lose";$action = "Crushes";break; case 'Paper': $outcome = "Win";$action = "Eats";break; case 'Scissors': $outcome = "Lose";$action = "Decapitates";break; case 'Spock': $outcome = "Win";$action = "Poisons";break; } break; case 'Spock': switch ($computer){ case 'Rock': $outcome = "Win";$action = "Vaporizes";break; case 'Paper': $outcome = "Lose";$action = "Disproves";break; case 'Scissors': $outcome = "Win";$action = "Smashes";break; case 'Lizard': $outcome = "Lose";$action = "Poisons";break; } break; } } } // if there is an outcome, display results if(isset($outcome) ){ if ($outcome =='Win'){ echo "
You ".$outcome."!!
Your ".$human." ".$action." my ".$computer."
"; }elseif ($outcome =='Lose'){ echo "
You ".$outcome."!!
My ".$computer." ".$action." your ".$human."
"; }elseif ($outcome =='Draw'){ echo "Draw!
We both played ".$human."
"; } }; // update stats $stats = game_statistics($outcome, $stats); } else {echo "Please pick something"; } ?>
    <form method = "post" action = "rpsls_paul.php">
<?php foreach($stats as $key => $val) { echo ''; } ?>
<h2>Let's Play Rock, Paper, Lizard, Spock</h2>
<hr>
<table>
<tr>
    <td><input type = "radio" name = "human" value = "Rock" />Rock</td>
    <td><input type = "radio" name = "human" value = "Paper" />Paper</td>


</tr>
<tr>
    <td><input type = "radio" name = "human" value = "Scissors" />Scissors</td>
    <td><input type = "radio" name = "human" value = "Lizard" />Lizard</td>
</tr>
<tr>
    <td colspan = "2"><input type = "radio" name = "human" value = "Spock" />Spock</td>
</tr>
<tr>
    <td colspan = "2"><hr></td>
</tr>
<tr>
    <td colspan = "2"><input type = "submit" value = "Play!" /></td>
</tr>
</table>
</form>  
<?php // generate computer play and return results function computer_play(){ $play = rand(1,5); switch ($play) { case 1: $computer = "Rock"; return $computer; case 2: $computer = "Paper"; return $computer; case 3: $computer = "Scissors"; return $computer; case 4: $computer = "Lizard"; return $computer; case 5: $computer = "Spock"; return $computer; } } // update stats array function game_statistics($outcome, $stats ){ $stats['games']++; $stats[$outcome]++; echo "Games: ".$stats['games']; echo " Win: ".$stats['Win']; echo " Loss: ".$stats['Lose']; echo " Draw: ".$stats['Draw']; return $stats; } ?>
</body>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service