Created
October 2, 2023 01:36
-
-
Save susanBuck/3a92feae1badfcb98247ed95d8dfa4bc to your computer and use it in GitHub Desktop.
card-deal.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
shuffle($cards); | |
$playerCards = []; | |
$computerCards = []; | |
# VARIATION 1 | |
$dealTo = 'player'; | |
foreach ($cards as $key => $card) { | |
if ($dealTo == 'player') { | |
$playerCards[] = array_pop($cards); | |
$dealTo = 'computer'; | |
} else { | |
$computerCards[] = array_pop($cards); | |
$dealTo = 'player'; | |
} | |
} | |
# VARIATION 2 - modulo operator | |
# Ref: https://www.php.net/manual/en/language.operators.arithmetic.php | |
// foreach($cards as $key => $card) { | |
// if($key % 0 == 0) { | |
// $playerCards[] = array_pop($cards); | |
// } else { | |
// $computerCards[] = array_pop($cards); | |
// } | |
// } | |
# VARIATION 3 - Popping the dealt card before the loop | |
// foreach ($cards as $key => $card) { | |
// $cardToDeal = array_pop($cards); | |
// if ($key % 2 == 0) { | |
// $playerCards[] = $cardToDeal; | |
// } else { | |
// $computerCards[] = $cardToDeal; | |
// } | |
// } | |
# VARIATION 4 - Dynamic variables | |
// foreach ($cards as $key => $card) { | |
// if ($key % 2 == 0) { | |
// $dealTo = 'playerCards'; | |
// } else { | |
// $dealTo = 'computerCards'; | |
// } | |
// # Dynamic variables : https://www.php.net/manual/en/language.variables.variable.php | |
// $$dealTo[] = array_pop($cards); | |
// } | |
# VARIATION 5 - Using a while loop | |
// $dealTo = 'player'; | |
// while (count($cards) > 0) { | |
// if ($dealTo == 'player') { | |
// $playerCards[] = array_pop($cards); | |
// $dealTo = 'computer'; | |
// } else { | |
// $computerCards[] = array_pop($cards); | |
// $dealTo = 'player'; | |
// } | |
// } | |
# Output results | |
var_dump($playerCards); # Should yield 5 random cards | |
var_dump($computerCards); # Should yield 5 different random cards |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment