I have a program which gives out 10 random cards from a normal deck (and doesn't put them back in).
Sometimes when I run the script in the shell I recieve the following error message:
PHP Notice: Undefined offset: ..... on line 15
My code looks as follows:
<?php
$deck = array(
array('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'), //club
array('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'), //spade
array('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'), //heart
array('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K') //diamond
);
$i = 0;
for ($i = 1; $i <= 10; $i++) {
$a = rand(0, 3); //card's suit
$nr = count($deck[$a]); //how many cards of the suit are available?
$b = rand(0, $nr--); //pick a random number out of the available ones
$card = $deck[$a][$b]; //pick a card [---LINE 15---]
switch ($a) {
case 0:
$c = "club";
break;
case 1:
$c = "spade";
break;
case 2:
$c = "heart";
break;
case 3:
$c = "diamond";
break;
}
echo $c . " " . $card . "\n" . "remaining: " . $nr . "\n";
unset($deck[$a][$b]); //remove the card you drew
array_values($deck[$a]); //rearrange the index of the suit you drew the card from
}
?>
Could someone help a newbie out?