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 | |
$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;
}
*/
?>