1

I made a cards game using PHP and all that's left now is that instead of echo'ing, 10, 3, Queen, King for example it should echo a random suited King when i Draw a King. I don't really know how this should be done.

Currently i have this function to show the hand of the player and the dealer (this one is for the dealer):

function list_dealer_hand() {
    foreach($_SESSION["dealer_hand"] as $hand_item) {
         echo $hand_item . ', ';
         echo '<img src="cardImages/h10.jpeg" border=0> ';
    }
}

The first echo will echo out what's in the dealer his hand using text, Like 10, Queen, Ace. For example. And the echo beneath that is an echo that will echo out the h10.jpeg, which in this case is the 10 of hearts. I have all cards from all suits in an folder named cardImages.

Is there a possibility to, for example, if the dealer has a 10 in his hand it would grab a random 10 from the images folder?

The array im currently using for the cards:

if(!isset($_SESSION["dealer_pile"])) $_SESSION["dealer_pile"] = array(
    'Jack', 'Queen', 'King', 'Ace', '10', '9', '8', '7', '6', '5', '4', '3', '2'
);

I appreciate any help or push in the right direction! Thanks in advance!

EDIT: Card cases:

// Case for each card, points
function get_card_value($card, $current_total) {
switch($card) {
    case "King":
    case "Queen":
    case "Jack":
    case "10":
        return 10;
    case "Ace":
       return ($current_total > 10) ? 1 : 11;
    case "9":
    case "8":
    case "7":
    case "6":
    case "5":
    case "4":
    case "3":
    case "2":
        return (int) $card;
   }
   return 0;
}
7
  • Can you clarify a bit - the dealer's hand in $_SESSION['dealer_pile'] the card (a 10) but does not specify the suit (10 of hearts)? You want to randomly choose from the images resulting in a random suit? Commented Feb 11, 2014 at 14:12
  • For example (the game is BlackJack btw) If i press start, it gives me 2 cards and the dealer 1, which is for example a 2, generated randomly from the above array. But instead of Echo 'Two'; it should echo a random suited Two. I hope this clears things up! Commented Feb 11, 2014 at 14:16
  • It does clear up what you're asking, but are you accounting for the removal of that card value + card suit from the deck? If you choose randomly, it is possible you both get the same card and suit (unless I don't know how Black Jack is played (multi-deck?)) Commented Feb 11, 2014 at 14:18
  • @MichaelBerkowski I honestly dont really mind if someone gets two 10s of hearts. The chance is so small, (random numbers + random suit, 1/14 and then 1/4) and even then its just a project i'm working on myself, So nothing that others are going to see. I'm really just experimenting with PHP, Sessions and what not. Commented Feb 11, 2014 at 14:22
  • @MichaelBerkowski Someone told me using the unset, shouldn't be done in my case. I don't really know why. But he said it was better to leave the array intact. Does it really matter that much? Commented Feb 11, 2014 at 14:41

1 Answer 1

2

I understood you'd like to create a pile of cetrtain number of cards with their corresponding "suit" letter. How about using this function to create a random pile:

function createRandomPile($limit) {
    $suits = array('h', 's', 'd', 'c');
    $cards = array(
        'Jack', 'Queen', 'King', 'Ace',
        '10', '9', '8', '7', '6',
        '5', '4', '3', '2'

    );
    $pile  = array();

    foreach (range(1, $limit) as $i) {
        $card = $cards[array_rand($cards)];
        $suit = $suits[array_rand($suits)];
        $pile[] = array($card, $suit);
    }

    return $pile;
}

$pile = createRandomPile(2);
/*
Returns something like:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "Queen"
    [1]=>
    string(1) "c"
  }
  [1]=>
  array(2) {
    [0]=>
    string(1) "9"
    [1]=>
    string(1) "s"
  }
}
*/

That function will create a pile of $limit cards, the cards being random in suit and number. You would use it like this:

foreach ($pile as $card) {
  $type = $card[0]; // King, 10, Ace, etc.
  $suit = $card[1]; // h, s, d or c.
  $image = $suit . $type; // hKing.
  // I don't know where $current_total comes from
  $value = get_card_value($type, $current_total);
}

I don't know if that's useful to you anymore D:

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer, but i also have a case system for points that go with each card, how would i change that so it would work together with those cases? Ill edit my question with the cases. Btw, every card has a 'suit' like jack of hearts, jack of spades, ace of diamons. Etc. These count up from 10, jack being 11, so h11, d11, etc
I see you pass $current_total to get_card_value function. You would have to pass that var to createRandomPile. Is that possible?
Is it? I have the cases in a function and as far as my php knowledge goes it's not very easy to switch vars between functions
Maybe i have something more easy for you to work with, it doesn't have to be a random suit anymore. So 2 or 3 10s of clubs is possible. And i named them literally 2,3,4,5,6, etc. (.jpeg) So all it has to do is <img src="cardImages/10.jpeg"> where 10 is $hand_item is this possible?
echo '<img src="cardImages/' . $hand_item . '.jpeg"> '; this seems to have done the trick! Thanks for all your effort you put into helping me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.