We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

fris on 09/20/08


Tagged

random shuffle cards


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

Scooter


shuffle a deck of cards


Published in: PHP 


  1. <?
  2. $cards = array("ah", "ac", "ad", "as",
  3. "2h", "2c", "2d", "2s",
  4. "3h", "3c", "3d", "3s",
  5. "4h", "4c", "4d", "4s",
  6. "5h", "5c", "5d", "5s",
  7. "6h", "6c", "6d", "6s",
  8. "7h", "7c", "7d", "7s",
  9. "8h", "8c", "8d", "8s",
  10. "9h", "9c", "9d", "9s",
  11. "th", "tc", "td", "ts",
  12. "jh", "jc", "jd", "js",
  13. "qh", "qc", "qd", "qs",
  14. "kh", "kc", "kd", "ks");
  15. for($i = 0; $i < 52; $i++)
  16. {
  17. $count = count($cards);
  18. $random = (rand()%$count);
  19. if($cards[$random] == "")
  20. {
  21. $i--;
  22. }
  23. else
  24. {
  25. $deck[] = $cards[$random];
  26. $cards[$random] = "";
  27. }
  28. }
  29.  
  30. $starting_point = (rand()%51);
  31. print("Starting point for cut cards is: $starting_point<p>");
  32.  
  33. // display shuffled cards
  34. for ($index = 0; $index < 52; $index++)
  35. {
  36. if ($starting_point == 52) { $starting_point = 0; }
  37. print("Uncut Point: <strong>$deck[$index]</strong> ");
  38. print("Starting Point: <strong>$deck[$starting_point]</strong><br>");
  39. $starting_point++;
  40. }
  41. ?>

Report this snippet 

You need to login to post a comment.