$pHands = array(); foreach ($players as $player) { $pCards = array(); foreach ($shuffledDeck as $k => $card) { $pCards[] = $card; unset($shuffledDeck[$k]); if (count($pCards) == $numCards) { break; } } $pHands[$player] = $pCards; } return $pHands; } echo '<pre>'; // ----------- USAGE ----------------- // Crack open a brand new deck of cards $deck = getDeck(); // Shuffle the deck shuffleDeck($deck); echo "<br>"; echo '<h2>' . 'New deck before dealing: <br/>' . '</h2>'; print_r($deck); $players = array('Joe', 'Mary', 'Zim'); $numCards = 3; $playerHands = deal($players, $numCards, $deck); echo '<h2>' . 'Hands each player has: <br/>' . '</h2>'; print_r($playerHands); echo '<h2>' . 'Deck after dealing: <br/>' . '</h2>'; print_r($deck); echo '<pre>';
function processCommand($clientInfo, $data) { if ($this->State != "lobby") { switch ($data["command"]) { case "bid": if ($this->State->NextAction === "Team1Player1Bid" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Bid" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Bid" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Bid" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { if ((string) $data["arguments"] === "pass") { if ($clientInfo["teamNumber"] === 1) { if ($clientInfo["playerNumber"] === 1) { $this->Team1->Player1->HasPassed = true; } else { $this->Team1->Player2->HasPassed = true; } } else { if ($clientInfo["playerNumber"] === 1) { $this->Team2->Player1->HasPassed = true; } else { $this->Team2->Player2->HasPassed = true; } } if (getNumberOfPassedPlayers($clientInfo["game"]) == 3) { $round = end($this->Rounds); array_push($round->Tricks, new Trick()); if ($this->Team1->Player1->HasPassed === false) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player1; $this->State->NextAction = "Team1Player1Kitty"; $waitingOn = 0; } if ($this->Team1->Player2->HasPassed === false) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player2; $this->State->NextAction = "Team1Player2Kitty"; $waitingOn = 2; } if ($this->Team2->Player1->HasPassed === false) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player1; $this->State->NextAction = "Team2Player1Kitty"; $waitingOn = 1; } if ($this->Team2->Player2->HasPassed === false) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player2; $this->State->NextAction = "Team2Player2Kitty"; $waitingOn = 3; } $allClients = getAllClientIdsInGame($clientInfo["game"]); foreach ($allClients as $id) { if ($id != $round->PlayerBidWinner->ClientId) { $response = array("action" => "command", "message" => "waitforkitty", "data" => array("bid" => $round->Bid, "bidwinner" => $round->PlayerBidWinner->Name)); sendJson($id, $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } else { $kittyCards = array(); foreach ($round->Kitty as $card) { array_push($kittyCards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } $response = array("action" => "command", "message" => "kitty", "data" => $kittyCards); sendJson($id, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "waitingon", "data" => $waitingOn); sendJson($id, $response); } } else { $round = end($this->Rounds); $allClients = getAllClientIdsInGame($clientInfo["game"]); $nextClientId = getNextBidder($clientInfo); foreach ($allClients as $thisClient) { $response = array("action" => "command", "message" => "newsfeed", "data" => $clientInfo["player"]->Name . " has passed"); sendJson($thisClient, $response); if ($thisClient === $nextClientId) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($nextClientId, $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($nextClientId, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($thisClient, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisClient, $response); } } } } elseif ($data["arguments"] % 5 === 0) { $round = end($this->Rounds); if ((int) $data["arguments"] > $round->Bid && (int) $data["arguments"] < 180) { $round->Bid = (int) $data["arguments"]; $round->CurrentHighestBidder = $clientInfo["player"]; $allClients = getAllClientIdsInGame($clientInfo["game"]); $nextClientId = getNextBidder($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "newsfeed", "data" => $clientInfo["player"]->Name . " just bid " . (string) $round->Bid); sendJson($id, $response); if ($nextClientId === $id) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($id, $response); } } } elseif ((int) $data["arguments"] === 180) { $round = end($this->Rounds); array_push($round->Tricks, new Trick()); $round->Bid = 180; if ($this->Team1->Player1->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player1; $this->State->NextAction = "Team1Player1Kitty"; $waitingOn = 0; } if ($this->Team1->Player2->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team1; $round->PlayerBidWinner = $this->Team1->Player2; $this->State->NextAction = "Team1Player2Kitty"; $waitingOn = 2; } if ($this->Team2->Player1->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player1; $this->State->NextAction = "Team2Player1Kitty"; $waitingOn = 1; } if ($this->Team2->Player2->ClientId === $clientInfo["clientId"]) { $round->TeamBidWinner = $this->Team2; $round->PlayerBidWinner = $this->Team2->Player2; $this->State->NextAction = "Team2Player2Kitty"; $waitingOn = 3; } $allClients = getAllClientIdsInGame($clientInfo["game"]); foreach ($allClients as $id) { if ($id != $round->PlayerBidWinner->ClientId) { $response = array("action" => "command", "message" => "waitforkitty", "data" => array("bid" => $round->Bid, "bidwinner" => $round->PlayerBidWinner->Name)); sendJson($id, $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } else { $kittyCards = array(); foreach ($round->Kitty as $card) { array_push($kittyCards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } $response = array("action" => "command", "message" => "kitty", "data" => $kittyCards); sendJson($id, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "waitingon", "data" => $waitingOn); sendJson($id, $response); } } else { $response = array("action" => "alert", "message" => "Sorry, your bid must be higher than the current bid of " . (string) $round->Bid . " and no greater than 180."); sendJson($clientInfo["clientId"], $response); } } else { $response = array("action" => "alert", "message" => "Sorry, your bid must be a multiple of 5."); sendJson($clientInfo["clientId"], $response); } } break; case "kitty": if ($this->State->NextAction === "Team1Player1Kitty" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Kitty" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Kitty" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Kitty" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { $allCardsAreValid = true; $chosenHandCards = array(); $chosenKittyCards = array(); foreach ($data["arguments"] as $card) { $round = end($this->Rounds); $kitty = $round->Kitty; $handCard = playerHasCard($clientInfo, $card); $kittyCard = kittyHasCard($clientInfo, $card); if (!$handCard && !$kittyCard) { $allCardsAreValid = false; break; } if ($handCard) { array_push($chosenHandCards, $handCard); } elseif ($kittyCard) { array_push($chosenKittyCards, $kittyCard); } } $round = end($this->Rounds); if ($allCardsAreValid) { //set the trump for the round if ((string) $data["trumpcolor"] === "green") { $round->Trump = Suit::Green; } elseif ((string) $data["trumpcolor"] === "red") { $round->Trump = Suit::Red; } elseif ((string) $data["trumpcolor"] === "black") { $round->Trump = Suit::Black; } elseif ((string) $data["trumpcolor"] === "yellow") { $round->Trump = Suit::Yellow; } else { $response = array("action" => "alert", "message" => "Invalid color choice"); sendJson($clientInfo["clientId"], $response); break; } moveCardsToKitty($clientInfo, $chosenHandCards, $chosenKittyCards); $allClients = getAllClientIdsInGame($clientInfo["game"]); $round = end($this->Rounds); $trumpColor = getSuitAsString($round->Trump, true); if (checkIfPointsInKitty($clientInfo)) { $messageString = "There are points in the kitty."; } else { $messageString = "There are no points in the kitty."; } $firstlayplayer = getAbsolutePlayerNumber($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "newsfeed", "data" => (string) $clientInfo["player"]->Name . " is finished with the kitty, and trump is " . $trumpColor . ". " . $messageString); sendJson($id, $response); $response = array("action" => "command", "message" => "beginlay", "playernumber" => (string) $firstlayplayer, "data" => cardsToJsonArray($clientInfo["player"]->Hand)); sendJson($id, $response); } $teamNumber = $clientInfo["teamNumber"]; $playerNumber = $clientInfo["playerNumber"]; if ($teamNumber === 1 && $playerNumber === 1) { $this->State->NextAction = "Team1Player1Lay"; } if ($teamNumber === 1 && $playerNumber === 2) { $this->State->NextAction = "Team1Player2Lay"; } if ($teamNumber === 2 && $playerNumber === 1) { $this->State->NextAction = "Team2Player1Lay"; } if ($teamNumber === 2 && $playerNumber === 2) { $this->State->NextAction = "Team2Player2Lay"; } } else { $response = array("action" => "alert", "message" => "Invalid card selection."); sendJson($clientInfo["clientId"], $response); } } break; case "lay": if ($this->State->NextAction === "Team1Player1Lay" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team1Player2Lay" && $clientInfo["teamNumber"] === 1 && $clientInfo["playerNumber"] === 2 || $this->State->NextAction === "Team2Player1Lay" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 1 || $this->State->NextAction === "Team2Player2Lay" && $clientInfo["teamNumber"] === 2 && $clientInfo["playerNumber"] === 2) { $card = playerHasCard($clientInfo, $data["arguments"]); if ($card !== null) { $round = end($this->Rounds); $trick = end($round->Tricks); if (isLegalMove($clientInfo, $trick, $card)) { if ($card->Suit === Suit::Rook && $this->Rules->NoRookOnFirstTrick && count($round->Tricks) === 1) { $response = array("action" => "alert", "message" => "You cannot play the Rook on the first trick"); sendJson($clientInfo["clientId"], $response); return; } array_push($trick->CardSet, $card); array_push($trick->PlayerOrder, $clientInfo); removeCardFromHand($clientInfo["clientId"], $clientInfo["player"], $card); $allClients = getAllClientIdsInGame($clientInfo["game"]); $thisPlayerNumber = getAbsolutePlayerNumber($clientInfo); foreach ($allClients as $id) { $response = array("action" => "command", "message" => "cardlaid", "data" => array("player" => $thisPlayerNumber, "suit" => $card->getSuitAsString(), "number" => $card->Number, "numberofcardsintrick" => count($trick->CardSet))); sendJson($id, $response); } if (count($trick->CardSet) === 4) { $leadSuit = $trick->CardSet[0]->Suit; if ($leadSuit === Suit::Rook) { $leadSuit === $round->Trump; } $highestValue = 0; $highestCard; $trumpIsInRound = false; foreach ($trick->CardSet as $card) { $cardSuit = $card->Suit; if ($cardSuit === Suit::Rook) { $cardSuit = $round->Trump; } if ($cardSuit === $round->Trump && !$trumpIsInRound) { $highestValue = 0; $trumpIsInRound = true; } if ($cardSuit === $leadSuit && !$trumpIsInRound) { $cardNumber = $card->Number; if ($cardNumber === 1) { $cardNumber = 15; } if ($cardNumber > $highestValue) { $highestValue = $cardNumber; $highestCard = $card; } } elseif ($cardSuit === $round->Trump && $trumpIsInRound) { $cardNumber = $card->Number; if ($cardNumber === 1) { $cardNumber = 15; } if ($cardNumber > $highestValue) { $highestValue = $cardNumber; $highestCard = $card; } } } $key = array_search($highestCard, $trick->CardSet); $winnerInfo = $trick->PlayerOrder[$key]; $trick->WinningTeam = $winnerInfo["teamNumber"]; $waitingOn; if ($winnerInfo["teamNumber"] === 1 && $winnerInfo["playerNumber"] === 1) { $this->State->NextAction = "Team1Player1Lay"; $waitingOn = 0; } if ($winnerInfo["teamNumber"] === 1 && $winnerInfo["playerNumber"] === 2) { $this->State->NextAction = "Team1Player2Lay"; $waitingOn = 2; } if ($winnerInfo["teamNumber"] === 2 && $winnerInfo["playerNumber"] === 1) { $this->State->NextAction = "Team2Player1Lay"; $waitingOn = 1; } if ($winnerInfo["teamNumber"] === 2 && $winnerInfo["playerNumber"] === 2) { $this->State->NextAction = "Team2Player2Lay"; $waitingOn = 3; } $endOfGameInfo = null; $isLastRound = false; if (count($round->Tricks) === 10) { $isLastRound = true; $endOfGameInfo = computeEndOfGameInfo($clientInfo, $winnerInfo); $this->State->NextAction = "ConfirmNextGame"; } else { array_push($round->Tricks, new Trick()); } $allClients = getAllClientIdsInGame($clientInfo["game"]); if (!$isLastRound) { foreach ($allClients as $id) { if ($id === $winnerInfo["clientId"]) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "setallowedsuits", "numberofcardsintrick" => "0", "data" => array("black", "yellow", "red", "green", "rook")); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } $response = array("action" => "command", "message" => "newsfeed", "data" => "The trick was won by " . $winnerInfo["player"]->Name . " with the " . $highestCard->toString()); sendJson($id, $response); $response = array("action" => "command", "message" => "trickdone", "data" => $waitingOn); sendJson($id, $response); } } else { foreach ($allClients as $id) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "newsfeed", "data" => "The trick was won by " . $winnerInfo["player"]->Name . " with the " . $highestCard->toString()); sendJson($id, $response); $response = array("action" => "command", "message" => "trickdone", "data" => $waitingOn); sendJson($id, $response); $response = array("action" => "command", "message" => "endgame", "data" => $endOfGameInfo); sendJson($id, $response); } if (!is_null($endOfGameInfo) && $endOfGameInfo["gameIsDone"] === "yes") { $this->DeleteMe = true; } } } else { setNextGameState($clientInfo); $allClients = getAllClientIdsInGame($clientInfo["game"]); $allowedSuits = getAllowedSuitsForNextPlayer($clientInfo); foreach ($allClients as $id) { if ($id === getNextClientId($clientInfo)) { $response = array("action" => "command", "message" => "gainpermission"); sendJson($id, $response); $response = array("action" => "command", "message" => "setallowedsuits", "data" => $allowedSuits, "numberofcardsintrick" => count($trick->CardSet)); sendJson($id, $response); } else { $response = array("action" => "command", "message" => "losepermission"); sendJson($id, $response); } } } } else { $response = array("action" => "alert", "message" => "Sorry, you must follow suit."); sendJson($clientInfo["clientId"], $response); } } else { $response = array("action" => "alert", "message" => "Sorry, you do not have that card in your hand."); sendJson($clientInfo["clientId"], $response); } } break; case "nextgame": if ($this->State->NextAction === "ConfirmNextGame") { $thisGame = $clientInfo["game"]; $clientInfo["player"]->NextGameConfirmed = true; if ($thisGame->Team1->Player1->NextGameConfirmed && $thisGame->Team1->Player2->NextGameConfirmed && $thisGame->Team2->Player1->NextGameConfirmed && $thisGame->Team2->Player2->NextGameConfirmed) { $thisGame->Team1->Player1->HasPassed = false; $thisGame->Team1->Player2->HasPassed = false; $thisGame->Team2->Player1->HasPassed = false; $thisGame->Team2->Player2->HasPassed = false; $thisGame->Team1->Player1->NextGameConfirmed = false; $thisGame->Team1->Player2->NextGameConfirmed = false; $thisGame->Team2->Player1->NextGameConfirmed = false; $thisGame->Team2->Player2->NextGameConfirmed = false; $bidStarter = $thisGame->BidStarter + 1; if ($bidStarter === 5) { $bidStarter = 1; } if ($bidStarter === 1) { $thisGame->State->NextAction = "Team1Player1Bid"; } else { if ($bidStarter === 2) { $thisGame->State->NextAction = "Team2Player1Bid"; } else { if ($bidStarter === 3) { $thisGame->State->NextAction = "Team1Player2Bid"; } else { if ($bidStarter === 4) { $thisGame->State->NextAction = "Team2Player2Bid"; } } } } $thisGame->BidStarter = $bidStarter; array_push($thisGame->Rounds, new Round()); $gamePlayers = array($thisGame->Team1->Player1->ClientId, $thisGame->Team2->Player1->ClientId, $thisGame->Team1->Player2->ClientId, $thisGame->Team2->Player2->ClientId); for ($i = 0; $i < 4; $i++) { $response = array("action" => "command", "message" => "resetfornextgame"); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "waitingon", "data" => $bidStarter - 1); sendJson($gamePlayers[$i], $response); } deal($thisGame); $p1Cards = array(); $p2Cards = array(); $p3Cards = array(); $p4Cards = array(); $kittyCards = ""; $round = end($thisGame->Rounds); if ($bidStarter === 1) { $round->CurrentHighestBidder = $thisGame->Team2->Player2; } if ($bidStarter === 2) { $round->CurrentHighestBidder = $thisGame->Team1->Player1; } if ($bidStarter === 3) { $round->CurrentHighestBidder = $thisGame->Team2->Player1; } if ($bidStarter === 4) { $round->CurrentHighestBidder = $thisGame->Team1->Player2; } foreach ($thisGame->Team1->Player1->Hand as $card) { array_push($p1Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team1->Player2->Hand as $card) { array_push($p2Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player1->Hand as $card) { array_push($p3Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player2->Hand as $card) { array_push($p4Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($round->Kitty as $card) { $kittyCards = $kittyCards . $card->toString() . ", "; } $response = array("action" => "command", "message" => "initializecards", "data" => $p1Cards); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p2Cards); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p3Cards); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p4Cards); sendJson($thisGame->Team2->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player2->ClientId, $response); $response = array("action" => "command", "message" => "gainpermission"); sendJson($gamePlayers[$bidStarter - 1], $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($gamePlayers[$bidStarter - 1], $response); } } } } }
/** *Plays a single round/hand by shuffling deck, dealing cards, calculating score, and printing to screen * @param array $players an array containing the players and their scores (passed by ref) *@param int $numCards the number of cards to deal this round *@param array $deck the array of cards passed by ref *@param int $roundNum the number of the round in the game * @return void */ function gameRound(&$players, $numCards, &$deck, $roundNum) { //shuffle the deck shuffleDeck($deck); //deal cards out to players deal($players, $numCards, $deck); //echo new round title to screen echo "<div class = 'Round'>"; echo "<h3>Round Number {$roundNum} </h3>"; //loop through players foreach ($players as $player => $hand) { //return score of player's hand $roundScore = scoreHand($players[$player]['hand']); //add hand score to total score $players[$player]['score'] += $roundScore; //Print hand and score to screen displayHand($players, $player, $roundScore, $players[$player]['score']); //Clear hand for next round $players[$player]['hand'] = []; } //Print count of remaining cards in deck to screen echo "<br><p>Remaining cards: " . count($deck) . "</p><br></div>"; }
<?php /** * Created by PhpStorm. * User: Nomad_Mystic * Date: 11/4/2015 * Time: 1:36 AM */ require_once 'includes/poker_constants.php'; require_once 'includes/poker_code.php'; $deck = make_deck(); $hand = deal($deck); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Video Poker</title> <link rel="stylesheet" type="text/css" href="includes/poker.css.php"> <script src="includes/poker.js.php"></script> </head> <body onload="javascript:init();"> <div id="spacer"></div> <?php show_content($hand); ?> <?php output_form($hand, $deck);
function new_game($bot1, $bot2) { // Shuffle the deck and deal two hands. $hands = deal(); $hand1 = hand_to_array($hands[0]); $hand2 = hand_to_array($hands[1]); // Create the game. $game = create_game($bot1, $bot2, $hand1, $hand2); // Assign the game to both players. assign_game($game, $bot1, $hand1); assign_game($game, $bot2, $hand2); }
if ($cardValue == $value) { return TRUE; } } return FALSE; } /* * Find all the winners in this round of Silverjack and set the * boolean value to true for the UI. There can be more than one * winner */ function findWinners($players) { //Hold the highest winning hand here $highestHand = 0; //Find the hightest hand at the table foreach ($players as $hand) { if ($highestHand < $hand["handTotal"] && $hand["handTotal"] <= 42) { $highestHand = $hand["handTotal"]; } } //Set the boolean value of each winner to true foreach ($players as $player => $hand) { $players[$player]["win"] = $hand["handTotal"] == $highestHand ? "TRUE" : "FALSE"; } //Return the array for players with the Win boolean set return $players; } //Lets deal a round of cards $players = deal($players);
<?php include 'admin/common.func.php'; deal(); function deal() { global $global; if (check_admin_login() > 0) { if (isset($global['dir'])) { include 'admin/module/' . $global['dir'] . '/deal.php'; } $cmd = post('cmd'); $cmd(); } exit; } function get_upl_file_name() { $dir = post('dir'); $file = post('file'); if ($file != rawurlencode($file)) { $file = date('Ymdhis') . substr($file, -4); } $result = $file; for ($i = 1; $i < 1000; $i++) { if (file_exists($dir . $result)) { $result = $i . '_' . $file; } else { break; } }
{ shuffle($deck); } /** * Deal a hand to each player * @param array $players Flat array of player names * @param int $numCards How many cards to deal to each player * @param array $deck A shuffled deck of cards * @return array */ function deal($players, $numCards, &$deck) { $playerHands = []; foreach ($players as $player) { $playerHand = []; for ($i = 0; $i < $numCards; $i++) { $playerHand[] = array_shift($deck); } $playerHands[$player] = $playerHand; } return $playerHands; } // Usage echo '<pre>'; $players = array('Jack', 'Jill', 'Justin'); $deck = getDeck(); shuffleDeck($deck); $dealtPlayers = deal($players, $numCards = 3, $deck); print_r($dealtPlayers); echo '<hr/>'; print_r($deck);
function beginGame($thisGame) { $thisGame->State->NextAction = "Team1Player1Bid"; $thisGame->BidStarter = 1; array_push($thisGame->Rounds, new Round()); $gamePlayers = array($thisGame->Team1->Player1->ClientId, $thisGame->Team2->Player1->ClientId, $thisGame->Team1->Player2->ClientId, $thisGame->Team2->Player2->ClientId); for ($i = 0; $i < 4; $i++) { $response = array("action" => "log", "message" => "Beginning game " . (string) $thisGame->Id); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "losepermission"); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "setplayernumber", "data" => (string) $i); sendJson($gamePlayers[$i], $response); $response = array("action" => "command", "message" => "waitingon", "data" => "0"); sendJson($gamePlayers[$i], $response); } deal($thisGame); $p1Cards = array(); $p2Cards = array(); $p3Cards = array(); $p4Cards = array(); $kittyCards = ""; $round = end($thisGame->Rounds); $round->CurrentHighestBidder = $thisGame->Team2->Player2; foreach ($thisGame->Team1->Player1->Hand as $card) { array_push($p1Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team1->Player2->Hand as $card) { array_push($p2Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player1->Hand as $card) { array_push($p3Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($thisGame->Team2->Player2->Hand as $card) { array_push($p4Cards, array("suit" => $card->getSuitAsString(), "number" => $card->Number)); } foreach ($round->Kitty as $card) { $kittyCards = $kittyCards . $card->toString() . ", "; } $response = array("action" => "command", "message" => "initializecards", "data" => $p1Cards); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p2Cards); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team1->Player2->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p3Cards); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player1->ClientId, $response); $response = array("action" => "command", "message" => "initializecards", "data" => $p4Cards); sendJson($thisGame->Team2->Player2->ClientId, $response); $response = array("action" => "command", "message" => "notyourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($thisGame->Team2->Player2->ClientId, $response); foreach ($gamePlayers as $id) { $response = array("action" => "log", "message" => "Cards in the Kitty: " . $kittyCards); sendJson($id, $response); } $response = array("action" => "command", "message" => "gainpermission"); sendJson($gamePlayers[0], $response); $response = array("action" => "command", "message" => "yourbid", "data" => array("bid" => $round->Bid, "highestbidder" => $round->CurrentHighestBidder->Name)); sendJson($gamePlayers[0], $response); }