card game - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of card game.php
- <?php
- // Optionally only choose specific types of cards
- $card_types = array(1,2,5); // $card_types = array();
- // Maximum cards in the deck
- $deckSize = 100;
- // Number of hands
- $hands = 2;
- // Number of cards in each hand
- $cardsInHand = 10;
- $cardTypes = array(
- array('qty' => 50, 'img' => 'https://img.scryfall.com/cards/large/front/9/3/9362f606-fafc-46b9-bf30-87b0799d885a.jpg'),
- array('qty' => 25, 'img' => 'https://img.scryfall.com/cards/large/front/1/9/19c2ce16-840b-4971-b4cc-89c8cdc5b093.jpg'),
- array('qty' => 25, 'img' => 'https://img.scryfall.com/cards/large/front/b/5/b5115083-0365-4a87-86be-c6d771a116e7.jpg')
- );
- // Build the entire deck
- $deck = array();
- foreach($cardTypes as $card){
- // Add all of each type of card to the deck
- $quantity = $card['qty'];
- // Get rid of the qty for the deck cards
- unset($card['qty']);
- // Create the deck
- for($x = 0; $x < $quantity; $x++){
- $deck[] = $card;
- }
- }
- // Randomize the deck
- shuffle($deck);
- // Cut the deck down to size
- $deck = array_slice($deck, 0, $deckSize);
- // Check if there are enough cards in the deck
- if( ($hands * $cardsInHand) > count($deck)) {
- throw new Exception('Not enough cards in deck');
- }
- // Deal out hands
- for($x = 0; $x < $hands; $x++) {
- for($y = 0; $y < $cardsInHand; $y++) {
- $hand[$x][] = array_shift($deck);
- }
- }
- // Dump it all out and see
- echo "<h2>Hand 1</h2>";
- foreach($hand[0] as $card) {
- echo "<img src=\"{$card['img']}\" width='50'>";
- }
- echo "<hr>";
- echo "<h2>Hand 2</h2>";
- foreach($hand[1] as $card) {
- echo "<img src=\"{$card['img']}\" width='50'>";
- }
- echo "<hr>";
- echo "<h2>Remaining Deck</h2>";
- foreach($deck as $card) {
- echo "<img src=\"{$card['img']}\" width='50'>";
- }