Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created October 28, 2021 01:16
Show Gist options
  • Save susanBuck/e933c5382ac6dfb25fc029ce0ad3d58d to your computer and use it in GitHub Desktop.
Save susanBuck/e933c5382ac6dfb25fc029ce0ad3d58d to your computer and use it in GitHub Desktop.
Example
<?php
$player_move = $_POST['userPick'];
$computer_move = ['Rock', 'Paper', 'Scissors'][rand(0, 2)];
$win = check_moves($player_move, $computer_move);
function check_moves($player, $computer)
{
# Define variabel to track the winner if any, otherwise it will be a tie!
$win = '';
# Game Logic begins with check if their moves are equal, then it will be a 'tie'
if ($player == $computer) {
$win = 'Tied';
}
# player move is 'Rock' and Computer move is 'Scissors'
elseif (($player == 'Rock') && ($computer == 'Scissors')) {
$win = 'Player';
# player move is 'Rock' and Computer move is 'Paper'
} elseif (($player == 'Rock') && ($computer == 'Paper')) {
$win = 'Computer';
# Player move is 'Scissors' and Computer move is 'Rock'
} elseif (($player == 'Scissors') && ($computer == 'Rock')) {
$win = 'Computer';
# Player move is 'Scissors' and Computer move is 'Paper'
} elseif (($player == 'Scissors') && ($computer == 'Paper')) {
$win = 'Player';
# Player move is 'Paper' and Computer move is 'Rock'
} elseif (($player == 'Paper') && ($computer == 'Rock')) {
$win = 'Player';
# Player move is 'Paper' and Computer move is 'Scissors'
} elseif (($player == 'Paper') && ($computer == 'Scissors')) {
$win = 'Computer';
}
return $win;
}
require 'index-view.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment