Reputation: 783
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;
}
Upvotes: 1
Views: 78
Reputation: 6922
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:
Upvotes: 2