rps.php
1 <?php 2 do { 3 $user_choice = readline("Choose rock, paper, or scissors: "); 4 $user_choice = strtolower($user_choice); 5 6 $choices = array('rock', 'paper', 'scissors'); 7 while (!in_array($user_choice, $choices)) { 8 $user_choice = readline("Choose rock, paper, or scissors: "); 9 $user_choice = strtolower($user_choice); 10 } 11 $play_again = true; 12 $computer_choice = $choices[array_rand($choices)]; 13 14 if ($user_choice == $computer_choice) { 15 echo "It's a tie!\n"; 16 } elseif (($user_choice == 'rock' && $computer_choice == 'scissors') || 17 ($user_choice == 'paper' && $computer_choice == 'rock') || 18 ($user_choice == 'scissors' && $computer_choice == 'paper')) { 19 echo "Congratulations! You won! The computer chose $computer_choice.\n"; 20 $play_again = false; 21 } else { 22 echo "Sorry, you lost. The computer chose $computer_choice.\n"; 23 $play_again = false; 24 } 25 } while ($play_again == true); 26 ?>