Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Generate, shuffle and deal custom card deck

Card deck class can generate deck of user defined cards.

Then card deck can be shuffled and dealt by specified amount of cards. As card types and properties can be specified, user can define card deck to suit any game

If you can't or didn't understand how to define card deck for any specific game (without involving game logic), post a comment down here or at PHP clases support forum with link to rules of game, and I'll try to provide an example for you

Contents

Download

Example codes

<?php
/*************************************************************
 * Here is an example of dealing cards for Texas holdem poker
 * for 4 people plus flop, turn and river
 * More information about the game:
 * http://en.wikipedia.org/wiki/Texas_hold_'em
 *************************************************************/
 //car strength
$strength = array("2","3","4","5","6","7","8","9","10","J","Q","K","A");
//suit of cards
$suit = array("C","D","H","S");

//inlcude class and create card deck
include("./card_deck.php");
$deck = new card_deck();

//add type with strength property and values from array
//and get id of type
$id = $deck->add_type("strength", $strength);

//add suit property to same type by providing id
$deck->add_type("suit", $suit, 1, $id);

//shuffle cards
$deck->shuffle();


//deal cards for 4 people, 2 cards for each
echo "<p>Player 1: ";
$arr = $deck->deal(2);
foreach($arr as $key => $val)
{
    $arr[$key] = implode("", $val);
}
echo implode(" ", $arr);
echo "</p>";

echo "<p>Player 2: ";
$arr = $deck->deal(2);
foreach($arr as $key => $val)
{
    $arr[$key] = implode("", $val);
}
echo implode(" ", $arr);
echo "</p>";

echo "<p>Player 3: ";
$arr = $deck->deal(2);
foreach($arr as $key => $val)
{
    $arr[$key] = implode("", $val);
}
echo implode(" ", $arr);
echo "</p>";

echo "<p>Player 4: ";
$arr = $deck->deal(2);
foreach($arr as $key => $val)
{
    $arr[$key] = implode("", $val);
}
echo implode(" ", $arr);
echo "<p>";

//deal flop, turn and river
echo "<p>Flop: ";
$arr = $deck->deal(3);
foreach($arr as $key => $val)
{
    $arr[$key] = implode("", $val);
}
echo implode(" ", $arr);
echo "<p>";

echo "<p>Turn: ";
$arr = $deck->deal(1);
echo implode("", current($arr));
echo "</p>";

echo "<p>River: ";
$arr = $deck->deal(1);
echo implode("", current($arr));
echo "</p>";

?>

Examples in action

Example scripts provided with package in action:

Method list

Add type and property of card

Back to method list

Method nameadd_type($property, $prop_names, $count = 1, $id = -1)
Description

Ads new card type and property or ads property to existing card type.

Card deck is generated by multiplying each property of card type and then summing all generated type cards

User may provide how many of each card should be in deck

For example if card one type has property strength, from 2 to Ace (13 values) and property suit - Clubs, Diamonds, Hearts and Spades (4 values) and other type are jokers (3 cards). Then all possible values will be generated by multiplying first card type properties and adding second type cards: (13*4)+3 = 55 cards

This function returns identification of card type, if you want to add more properties to this card type.

Input parameters

string $property - name of card property, for example suit or strength"

array $prop_names - array with all possible property values

int $count - how many of each generated type (type not property) card should be in deck. For example if you have type with two properties, and you passed count 2 while adding both properties, you will end up having 4 card with same generated type.

int $id - identification of previously created card type. You need to pass it to add property to existing card type

Example inputadd_type("suit", array("Clubs", "Diamonds", "Hearts", "Spades"))

Shuffle card deck

Back to method list

Method nameshuffle($reset = true)
DescriptionShuffles generated card deck and resets it, means generates all card types from scratch. If you have dealt cards and want to shuffle deck, without reseting it, pass argument false to shuffle function
Input parameters

bool $reset - if card deck should be reset, generate all cards again. If passed false, then only cards left in deck will be shuffled

Deal cards

Back to method list

Method namedeal($count)
Description

Returns array with specified amount of cards

Array contains property name as key and property value as value

If more cards are requested then is actually left in card deck, then function will return all cards left in deck or empty array, if there are no cards left at all

Example output
Array
(
    [0] => Array
        (
            [strength] => A
            [suit] => D
        )

    [1] => Array
        (
            [strength] => 9
            [suit] => C
        )

    [2] => Array
        (
            [strength] => 8
            [suit] => C
        )
)

Latest changes

None for now

Rate us

Like our script? Rate it at PHP > Hot Scripts

Scripts.com
RATE ME!

Try it out and Rate on PHPclasses.org

Support

PHP classes support forum or comments below

Awards

Card deck class was nominated to PHP Programming Innovation Award of March of 2011, please support ir by voting


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus