Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created October 13, 2021 18:55
Show Gist options
  • Save susanBuck/4830bf38275b55c01ea2edce173e6137 to your computer and use it in GitHub Desktop.
Save susanBuck/4830bf38275b55c01ea2edce173e6137 to your computer and use it in GitHub Desktop.
Slap Jack example
<h2>Results</h2>
<p>The winner of the game was <?php echo $overallWinner; ?>!
Below is the results of each round.</p>
<?php foreach ($results as $roundNumber => $data) { ?>
<ul>
<li>Round #: <?php echo $roundNumber ?>
</li>
<li>Player A discarded: <?php echo $data['playerADiscard']; ?>
</li>
<li>Player B discarded: <?php echo $data['playerBDiscard']; ?>
</li>
<li>Player A has <?php echo $data['playerACardCount'] ?>
cards left.</li>
<li>Player B has <?php echo $data['playerBCardCount'] ?>
cards left.</li>
<?php if ($data['jack']) { ?>
<li>
<strong>
A Jack was discarded and Player <?php echo $data['winner'] ?> won the
slap so they get the pile
</strong>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php
// Create a deck of cards
$deck = [
"Ace of Spades",
"2 of Spades",
"3 of Spades",
"4 of Spades",
"5 of Spades",
"6 of Spades",
"7 of Spades",
"8 of Spades",
"9 of Spades",
"10 of Spades",
"Jack of Spades",
"Queen of Spades",
"King of Spades",
"Ace of Clubs",
"2 of Clubs",
"3 of Clubs",
"4 of Clubs",
"5 of Clubs",
"6 of Clubs",
"7 of Clubs",
"8 of Clubs",
"9 of Clubs",
"10 of Clubs",
"Jack of Clubs",
"Queen of Clubs",
"King of Clubs",
"Ace of Diamonds",
"2 of Diamonds",
"3 of Diamonds",
"4 of Diamonds",
"5 of Diamonds",
"6 of Diamonds",
"7 of Diamonds",
"8 of Diamonds",
"9 of Diamonds",
"10 of Diamonds",
"Jack of Diamonds",
"Queen of Diamonds",
"King of Diamonds",
"Ace of Hearts",
"2 of Hearts",
"3 of Hearts",
"4 of Hearts",
"5 of Hearts",
"6 of Hearts",
"7 of Hearts",
"8 of Hearts",
"9 of Hearts",
"10 of Hearts",
"Jack of Hearts",
"Queen of Hearts",
"King of Hearts",
];
// Shuffle cards
shuffle($deck);
// Initialize empty arrays for each player and the discard pile, as well as a results array where we'll accumulate
// results as the game is played
$paHand = [];
$pbHand = [];
$pile = [];
$results = [];
// Create an array of Jack cards
$jacks = ['Jack of Spades', 'Jack of Clubs', 'Jack of Hearts', 'Jack of Diamongs'];
// Deal cards one per player until entire deck is dealt
while (count($deck) > 0) {
if (count($deck) % 2 == 0) {
$paHand[] = array_pop($deck);
} else {
$pbHand[] = array_pop($deck);
}
}
// Game play
// Play until one of the players run out of cards
while (count($paHand) > 0 and count($pbHand) > 0) {
// Discard a card for player a and player b
$playerADiscard = array_pop($paHand);
$playerBDiscard = array_pop($pbHand);
// Add these cards to the pile
$pile = array_merge($pile, [$playerADiscard, $playerBDiscard]);
// If either of the cards discarded is a jack, randomly decide who slaps the jack
// The winner gets the whole pile added to their cards
if (in_array($playerADiscard, $jacks) or in_array($playerBDiscard, $jacks)) {
$jack = true;
if (rand(0, 1) == 0) {
$winner = 'A';
$paHand = array_merge($paHand, $pile);
} else {
$winner = 'B';
$pbHand = array_merge($pbHand, $pile);
}
$pile = [];
} else {
$jack = false;
$winner = null;
}
$results[] = [
'playerADiscard' => $playerADiscard,
'playerBDiscard' => $playerBDiscard,
'playerACardCount' => count($paHand),
'playerBCardCount' => count($pbHand),
'jack' => $jack,
'winner' => $winner
];
}
// Determine the winner
if (count($paHand) == 0) {
$overallWinner = 'Player B';
} else {
$overallWinner = 'Player A';
}
require 'index-view.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment