php - How to remove duplicate playing cards from player's hand? -
i trying deal hand of 5 cards player , score them. scoring program seems working fine, running issue of duplicate cards getting dealt time time. tried using while loop check duplicate cards, seems kind of hackish. code below. please keep in mind neophyte, simpler solution better! much.
// create suits array $suits = array("996", "997", "998", "999"); // create faces array $faces = array(); $faces[1] = "1"; $faces[2] = "2"; $faces[3] = "3"; $faces[4] = "4"; $faces[5] = "5"; $faces[6] = "6"; $faces[7] = "7"; $faces[8] = "8"; $faces[9] = "9"; $faces[10] = "10"; $faces[11] = "11"; $faces[12] = "12"; $faces[13] = "13"; // create player's hand $card = array(); ($i = 0; $i < 5; $i++) { $face_value = shuffle($faces); $suit_value = shuffle($suits); $card[$i] = $faces[$face_value].$suits[$suit_value]; $counter = 0; while ($counter < 100) { if (in_array($card[$i], $card)) { $face_value = shuffle($faces); $suit_value = shuffle($suits); $card[$i] = $faces[$face_value].$suits[$suit_value]; } $counter++; } print ("<img src=\"../images/4/$card[$i].gif\">"); }
it might more efficient set array has 52 elements, 1 each of cards.
$cards = range(0,51); shuffle($cards); $hand = array(); ($i = 0; $i < 5; $i++) { $hand[$i] = $cards[$i]; }
note can extract suit , rank of card $i
simply, doing
$suit = $hand[$i] % 4; $rank = $hand[$i] / 4;
this prevent duplicates.
edit: suit , rank reversed. should correct now.
Comments
Post a Comment