1

I'm working on a "card game" in which cards are randomly dealt from all four suites to up to 4 players. I created two separate arrays, one for the 13 cards, and one for the 4 suites. Below is the code I have for just 1 player.

Arrays:

<?php
  $suite=array("Clubs", "Diamonds", "Hearts", "Spades");
  $random_suite=array_rand($suite, 4);
?>

<?php
  $card=array("Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King");
  $random_card=array_rand($card,5);
?>

If Statements (Don't believe a problem exists here):

<?php  if (!is_numeric($_GET['players']))
{
echo "You did not enter a value for Number of Players; click the 'Return'
button and enter a number.";
}
elseif ($_GET['players'] < 1)
{
echo "You entered a number less than 1 which is the minimum allowed; click
the 'Return' button to enter another number.";
}
elseif ($_GET['players'] > 4)
{
echo "You entered a number greater than 4 which is the maximum allowed; click
the 'Return' button to enter another number.";
}
else {
  echo "<br>Dealing up Cards for 4 players
  <br>Hand for Player Number 1:

Combining Arrays:

  <br>{$card[$random_card[0]]} of {$suite[$random_suite[0]]}
  <br>{$card[$random_card[1]]} of {$suite[$random_suite[1]]}
  <br>{$card[$random_card[2]]} of {$suite[$random_suite[2]]}
  <br>{$card[$random_card[3]]} of {$suite[$random_suite[3]]}
  <br>{$card[$random_card[4]]} of {$suite[$random_suite[4]]}";
}
?>

Using <?php $suite=array("Clubs", "Diamonds", "Hearts", "Spades"); $random_suite=array_rand($suite, 4); ?> gives me this, which makes sense from the number being 4. However, if I change this to 5 which is how many cards need to be dealt, I get this, probably because there's only 4 suites.

So, what's wrong with my code?

Bonus: I also need to figure out how to make all cards that are dealt unique. I can't have two ace of spades. If someone could help me with this as well, I'd be grateful.

cfoster5
  • 1,696
  • 5
  • 28
  • 42
  • At the moment, your code for 4 cards will always give one from every suit. Is that intended? Which suit do you want for the 5th card? – Scopey Apr 19 '16 at 01:34
  • Please put your results inline as text rather than as links to images. Also [see questions like this](http://stackoverflow.com/a/24534266/1270789) for tips on how to shuffle. – Ken Y-N Apr 19 '16 at 01:38
  • @Scopey the intent is to deal five random cards from a full deck of 52 cards (all 4 suits) to up to 4 players. – cfoster5 Apr 19 '16 at 15:09

1 Answers1

0

If you want a given number of cards from a randomised deck of cards, it would be a better idea just to create an array with every card in a deck of cards.

<?php
$suits = array("Clubs", "Diamonds", "Hearts", "Spades");
$cards = array("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King");

$deck = array();
foreach($suits as $suit) {
    foreach($cards as $card) {
        $deck[] = $card . ' of ' . $suit;
    }
}

Now you can shuffle the array and get the specific amount of cards you need from it

$num = 5;
shuffle($deck);
$cards = array_slice($deck, 0, $num);

Here's an ideone example: https://ideone.com/wQE677

Or you can use array rand again

$cards = array_rand($deck, 5);
Scopey
  • 6,269
  • 1
  • 22
  • 34