Input:
scissor-paper paper-rock scissor-rock paper-paper rock-rock
Output:
player one wins player one wins player two wins draw draw
Solution 1:
$playerOne = $_GET['one']; // change this with your user input $playerTwo = $_GET['two']; // change this with your user input if($playerOne == 'rock') { if($playerTwo == 'rock') { echo 'draw'; } else if($playerTwo == 'paper') { echo 'player two wins'; } else if($playerTwo == 'scissor') { echo 'player one wins'; } else { echo 'wrong player two input'; } } else if($playerOne == 'paper') { if($playerTwo == 'rock') { echo 'player one wins'; } else if($playerTwo == 'paper') { echo 'draw'; } else if($playerTwo == 'scissor') { echo 'player two wins'; } else { echo 'wrong player two input'; } } else if($playerOne == 'scissor') { if($playerTwo == 'rock') { echo 'player two wins'; } else if($playerTwo == 'paper') { echo 'player one wins'; } else if($playerTwo == 'scissor') { echo 'draw'; } else { echo 'wrong player two input'; } } else { echo 'wrong player one input'; }
Solution 2:
$playerOne = numberRepresentation($_GET['one']); $playerTwo = numberRepresentation($_GET['two']); if($playerOne == 0) { echo 'wrong player one input'; exit; } else if($playerTwo == 0 ) { echo 'wrong player two input'; exit; } if($playerOne == 3 && $playerTwo == 1) { echo 'player two wins'; } else if($playerOne == 1 && $playerTwo == 3) { echo 'player one wins'; } else if($playerOne < $playerTwo) { echo 'player two wins'; } else if($playerOne > $playerTwo) { echo 'player one wins'; } else { echo 'draw'; } function numberRepresentation($c) { switch ($c) { case 'rock': return 1; break; case 'paper': return 2; break; case 'scissor': return 3; break; default: return 0; } }
No comments:
Post a Comment