PHP code example of garak / card

1. Go to this page and download the library: Download garak/card library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

garak / card example snippets




arak\Card\Card;
use Garak\Card\Rank;
use Garak\Card\Suit;

$card = new Card(Rank::Ace, Suit::Diamonds);
echo $card; // will output "Ad"

$card = new Card(Rank::Seven, Suit::Spades);
echo $card->toText(); // will output "7♠"

$card = Card::fromRankSuit('Kh');
echo $card->toUnicode(); // will output "🂾"



arak\Card\Card;

$orderedCards = Card::getDeck();
$shuffledCards = Card::getDeck(shuffle: true);
$doubleDeckWithJokers = Card::getDeck(shuffle: true, num: 2, allowJokers: true);



arak\Card\Card;
use Garak\Card\CardBack;
use Garak\Card\Rank;
use Garak\Card\Suit;

// Single deck - cards have no back (backward compatible)
$singleDeck = Card::getDeck();
$card = $singleDeck[0];
$card->getBack(); // returns null

// Multiple decks - cards have different backs
$twoDecks = Card::getDeck(num: 2);
// First 52 cards have red back, next 52 have blue back

// Requesting more than two decks reuses the available backs in order
$threeDecks = Card::getDeck(num: 3);
// The third deck uses the red back again

// Cards with same face but different backs are not equal
$redAceOfSpades = new Card(Rank::Ace, Suit::Spades, CardBack::Red);
$blueAceOfSpades = new Card(Rank::Ace, Suit::Spades, CardBack::Blue);
$redAceOfSpades->isEqual($blueAceOfSpades); // false
$redAceOfSpades->isSameFace($blueAceOfSpades); // true

// Cards with/without back are different when one side has a back,
// but you can still compare just the face when needed.
$noBackAceOfSpades = new Card(Rank::Ace, Suit::Spades);
$noBackAceOfSpades->isEqual($redAceOfSpades); // false
$noBackAceOfSpades->isSameFace($redAceOfSpades); // true
$noBackAceOfSpades->getBack(); // returns null



arak\Card\Card;
use Garak\Card\Rank;
use Garak\Card\Suit;

$joker = new Card(Rank::Joker, Suit::BlackJoker);
echo $joker;              // will output "wb"
echo $joker->toText();    // will output "wb"
echo $joker->toUnicode(); // will output "🃏"



arak\Card\Card;

$card = Card::fromRankSuit('Asr');
$card->getBack(); // CardBack::Red

echo $card;                           // will output "As"
echo $card->toString(withBack: true); // will output "Asr"

// The same applies to hands
$hand = MyHand::createFromString('Asr,Kdb');
echo $hand;                           // will output "As,Kd"
echo $hand->toString(withBack: true); // will output "Asr,Kdb"



arak\Card\Card;
use Garak\Card\Pile;

$stock = new Pile(Card::getDeck());
$stock->shuffle();
$card = $stock->draw();   // removes and returns the top card
$stock->top();            // peeks at the new top card, or null when empty
count($stock);            // 51

$discard = Pile::createFromString('2c,Kd'); // bottom to top
$discard->add($card);     // the added card is now on top
echo $discard;            // will output "2c,Kd,As" (for an ace of spades)
$discard->has(Card::fromRankSuit('Kd')); // true

$cards = $discard->takeAll(); // returns all cards, bottom to top, and empties the pile



arak\Card\Card;
use Garak\Card\Pile;

$stock = Pile::createFromTop(Card::getDeck()); // the first card of the deck is on top
$hand = $stock->drawMany(13);                  // top card first
$next = $stock->peek(3);                       // the next 3 cards to be drawn, not removed



arak\Card\CardBack;
use Garak\Card\Deck;
use Garak\Card\Rank;

$canasta = new Deck(copies: 2, jokers: 4);
count($canasta); // 108
$cards = $canasta->getCards();  // unshuffled: copy by copy, suit by suit, rank by rank, jokers after each copy
$cards = $canasta->shuffle();
$stock = $canasta->createPile(shuffle: true); // a Pile ready to draw from

$piquet = new Deck(ranks: array_slice(Rank::regular(), 5)); // 32 cards, from seven to ace
$shoe = new Deck(copies: 6, backs: []);                    // 312 identical cards, no backs
$blue = new Deck(copies: 2, backs: [CardBack::Blue]);      // both copies with a blue back



arak\Card\Card;
use Random\Engine\Mt19937;
use Random\Randomizer;

$deck = Card::getDeck(shuffle: true, randomizer: new Randomizer(new Mt19937(42)));



arak\Card\Card;
use Garak\Card\CardComparator;
use Garak\Card\Cards;
use Garak\Card\CardValues;
use Garak\Card\Rank;
use Garak\Card\Suit;

$cards = Cards::fromString('As,Kd,Ad,wb,2c');
count($cards);                          // 5
$cards->has(Card::fromRankSuit('Kd'));  // true
$cards->hasSuit(Suit::Hearts);          // false
$cards->ofRank(Rank::Ace);              // As,Ad
$cards->regular();                      // As,Kd,Ad,2c (no jokers)
$cards->groupBySuit();                  // ['s' => As, 'd' => Kd,Ad, 'b' => wb, 'c' => 2c]
$cards->groupByRank()[Rank::Ace->value]; // As,Ad
$cards->with(Card::fromRankSuit('3c')); // a new list with the card appended
$cards->without(Card::fromRankSuit('wb'));
$cards->sort(new CardComparator());
$cards->regular()->sum(CardValues::aceHigh()); // 14 + 13 + 14 + 2



arak\Card\Card;
use Garak\Card\CardBag;
use Garak\Card\Cards;

$before = Cards::fromString('5h,5d,5s')->toBag();
$after = new CardBag(Cards::fromString('5h,5d,5s,6s'));

$before->isSubsetOf($after); // true
$after->diff($before);       // [6s]
$before->equals($after);     // false
$after->countOf(Card::fromRankSuit('5h')); // 1



arak\Card\Card;
use Garak\Card\CardValues;
use Garak\Card\Rank;
use Garak\Card\RankOrder;
use Garak\Card\SuitOrder;

$order = RankOrder::aceHigh();       // two to ace
$order = RankOrder::aceLow();        // ace to king
$order = new RankOrder([Rank::Nine, Rank::Jack, Rank::Queen, Rank::King, Rank::Ten, Rank::Ace]); // pinochle
$order->compare(Rank::Ten, Rank::King); // 1
$order->indexOf(Rank::Nine);            // 0
$order->next(Rank::Nine);               // Rank::Jack
$order->highest($cards);                // the highest card of a list

SuitOrder::standard(); // clubs, diamonds, hearts, spades

$values = CardValues::aceHigh();  // 2 to 14, the former Rank::getInt()
$values = CardValues::aceLow();   // 1 to 13
$values = new CardValues(['A' => 11, '3' => 10, 'K' => 4, 'Q' => 3, 'J' => 2], default: 0); // by rank
$values = (new CardValues(default: 0))->withCard(Card::fromRankSuit('Qs'), 13);          // a single card
$values->of(Card::fromRankSuit('Qs')); // 13
$values->sum($cards);



arak\Card\CardComparator;
use Garak\Card\RankOrder;
use Garak\Card\Suit;

$bySuit = new CardComparator();                                  // by suit, then by rank, ace high, ascending
$poker = new CardComparator(suitFirst: false, descending: true); // by rank, highest first
$bridge = (new CardComparator())->withTrump(Suit::Spades)->withDescending(); // trumps first
$rummy = new CardComparator(RankOrder::aceLow());

$sorted = $bridge->sort($cards); // a sorted copy
usort($cards, $poker);



arak\Card\Card;
use Garak\Card\CardsTrick;
use Garak\Card\Suit;

final class Trick extends CardsTrick
{
}

$trick = new Trick(['N' => Card::fromRankSuit('5h'), 'E' => Card::fromRankSuit('Kh'), 'S' => Card::fromRankSuit('2s')]);
$trick->getLedSuit();                 // Suit::Hearts
$trick->getWinningCard(null);         // Kh
$trick->getWinningKey(Suit::Spades);  // 'S'
$trick->canPlay($card, $hand);        // false when the hand could follow the led suit and the card does not



arak\Card\Card;

Card::fromRankSuit('Asr')->toHiddenString(); // "??r"
Card::fromRankSuit('As')->toHiddenString();  // "??"
$hand->toHiddenString();                     // "??r,??r,??r"

$rank = Rank::from('A');  // Rank::Ace
$suit = Suit::from('s');  // Suit::Spades

// Before
try {
    new Rank('X');
} catch (\InvalidArgumentException $e) { ... }

// After
try {
    Rank::from('X');
} catch (\ValueError $e) { ... }

// Before
foreach (Rank::$ranks as $symbol => $intValue) { ... }
foreach (Suit::$suits as $symbol => $unicodeChar) { ... }

// After
foreach (Rank::cases() as $rank) {
    $symbol   = $rank->value;    // e.g. 'A'
    $intValue = $rank->getInt(); // e.g. 14
}
foreach (Suit::cases() as $suit) {
    $symbol    = $suit->value;      // e.g. 's'
    $unicodeChar = $suit->toText(); // e.g. '♠'
}