This design is still under development!

Card Game

Step 1 : Setting variables

We start off by setting our variables.

First we need an image path, this is the path to the directory containing your images. Note that this is without the image name.

Then we create two arrays, which will contain suits and cards accordingly.


$image_dir = "/resources/images/playing_cards/";
$cards = array('Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King');
$suits = array('hearts', 'spades', 'clubs', 'diamonds');

        

Step 2 : Get familiar with functions

We then move on to creating a function, which will contain the code for assembling a deck of cards.

In most cases it is a good idea to contain your actions in a function, this will allow you to easily reuse this action with a single line of code.

The function takes two variables, suits and cards, not to be confused with the ones defined above, these variables will be used within the function only.


function assembledeck($cards, $suits){
    
};
        

Step 3 : Creating deck assembler

Now we write the code which will assemble the deck inside the function.

We need to create a variable to store the deck in, so we call it carddeck and defines that we want it to be an array.


function assembledeck($cards, $suits){
    $carddeck = array();
    //For each position in the $suits array we will run this action.
    foreach($suits as $suit){
        //For each position in the $cards array we will run this action.
        foreach($cards as $card){
            //Creates a new position in the $carddeck array containing the card.
            $carddeck[] = array('Card' => $card .' of '. $suit); 
        }
    }
    return $carddeck;
};
        

Step 4 : Getting the carddeck

To use this function simply call it and store it in a variable as the example shows.

We now have a neatly assembled carddeck, but we want to shuffle it. Php has it covered with the shuffle function, it takes all the positions in the array and shuffles them randomly.


$carddeck = assembledeck($cards, $suits);
shuffle($carddeck);
        

Step 5 : Pcicking a card

Let us take it a step further and pick a random card.

Create a variable called picked_card, then use the php function called array_rand to select a random position in the carddeck array.

Because we are working with a multidimensional array we need to dig into the result to get the string with the name of the card.

Last we replace the spaces in the string containing the cardname to match the filename of the image in our image file diretory.


//array_rand takes two variables first position is the array you want to use, second is the number of results you want.
$picked_card = array_rand($carddeck,1);
//Find the card on the posistion array_rand returned from your deck.
$picked_card = $carddeck[$picked_card];
foreach($picked_card as $card){
    $showcard = str_replace(' ', '_', strtolower($card));
};
        

Final result:


$image_dir = "/resources/images/playing_cards/";
$cards = array('Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King');
$suits = array('hearts', 'spades', 'clubs', 'diamonds');
//echo "under construction";
//die();
function assembledeck($cards, $suits){
    $carddeck = array();
    foreach($suits as $suit){
        foreach($cards as $card){
            $carddeck[] = array('Card' => $card .' of '. $suit); 
        }
    }
    return $carddeck;
};

$carddeck = assembledeck($cards, $suits);
shuffle($carddeck);
$picked_card = array_rand($carddeck,1);
$picked_card = $carddeck[$picked_card];
foreach($picked_card as $card){
    $showcard = str_replace(' ', '_', strtolower($card));
};
        

How to use:

To show your selected card as an image you will need to put this into the src attribute.


echo $image_dir . $showcard . ".png";