4

A deck of cards

  1. namespace Deck;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. require_once 'Card.php';
  6. require_once 'Hand.php';

Places the code of the class in its own namespace. Loads So-o and the Card class.

  1. defclass('Deck', null, 1, null, array('cards', 'top', 'shuffleWhenEmpty'), null, array('init', 'deal', 'hand', 'shuffle', 'check', 'toString'));

The Deck class inherits from the Object class. An instance has 3 properties: cards, the set of cards, top which holds the index in cards of the card on the top of the deck, i.e. the next which will be drawn, and shuffleWhenEmpty which if true automatically shuffles the deck when all the cards have been drawn. An instance redefines the messages init and toString. It implements the messages deal, hand, shuffle and check.

  1. function i_init($self, $swe=false) {
  2.     global $Card;
  3.  
  4.     supersend('init', func_get_args());
  5.  
  6.     sendmsg($self, 'set', 'shuffleWhenEmpty', $swe ? true : false);
  7.  
  8.     $cards=array();
  9.     for ($s = 0; $s < 4; $s++) {
  10.         for ($r = 0; $r < 13; $r++) {
  11.             $cards[ 13*$s+$r ] = sendmsg($Card, 'new', $r, $s);
  12.         }
  13.     }
  14.     sendmsg($self, 'set', 'cards', $cards);
  15.     sendmsg($self, 'set', 'top', 0);
  16.  
  17.     return $self;
  18. }
  1. function i_shuffle($self) {
  2.     $cards=sendmsg($self, 'get', 'cards');
  3.  
  4.     shuffle($cards);
  5.  
  6.     sendmsg($self, 'set', 'cards', $cards);
  7.     sendmsg($self, 'set', 'top', 0);
  8.  
  9.     return $self;
  10. }
  1. function i_check($self) {
  2.     $top=sendmsg($self, 'get', 'top');
  3.  
  4.     if (++$top >= 52) {
  5.         if (sendmsg($self, 'get', 'shuffleWhenEmpty')) {
  6.             sendmsg($self, 'shuffle');
  7.         }
  8.         $top=0;
  9.     }
  10.  
  11.     sendmsg($self, 'set', 'top', $top);
  12.  
  13.     return $self;
  14. }
  1. function i_deal($self) {
  2.     $cards=sendmsg($self, 'get', 'cards');
  3.     $top=sendmsg($self, 'get', 'top');
  4.  
  5.     $c = $cards[$top];
  6.  
  7.     sendmsg($self, 'check');
  8.  
  9.     return $c;
  10. }
  1. function i_hand($self, $hand=null) {
  2.     global $Hand;
  3.  
  4.     $cards=array();
  5.  
  6.     for ($n=0; $n < 5; $n++) {
  7.         $cards[]=sendmsg($self, 'deal');
  8.     }
  9.  
  10.     return $hand ? sendmsg($hand, 'init', $cards) : sendmsg($Hand, 'new', $cards);
  11. }
  1. function i_toString($self) {
  2.     $cards=sendmsg($self, 'get', 'cards');
  3.     $top=sendmsg($self, 'get', 'top');
  4.  
  5.     foreach ($cards as $c) {
  6.         $s[]=sendmsg($c, 'toString');
  7.     }
  8.  
  9.     return implode(',', $s) . ' ' . $top;
  10. }
  1. set_include_path(getcwd() . PATH_SEPARATOR . dirname(getcwd()));
  2.  
  3. require_once 'So-o.php';
  4.  
  5. require_once 'Deck.php';

Sets the PHP configuration parameter include_path to the current directory and the directory containing the So-o code. Loads So-o and the Deck class.

  1. $deck=sendmsg($Deck, 'new', true);
  2. sendmsg($deck, 'print', true);
  3. sendmsg($deck, 'shuffle');
  4. sendmsg($deck, 'print', true);
  5.  
  6. $card=sendmsg($deck, 'deal');
  7. sendmsg($card, 'print', true);
  8.  
  9. for ($n=0; $n < 5; $n++) {
  10.     sendmsg(sendmsg($deck, 'hand'), 'print', true);
  11. }
  12.  
  13. $ndeals=10000;
  14. $stats=array();
  15. for ($i=0; $i < 9; $i++ ) {
  16.     $stats[$i] = 0;
  17. }
  18. echo 'Dealing ', $ndeals, ' hands...';
  19. $starttime=time();
  20.  
  21. $hand=sendmsg($deck, 'hand');
  22. /*
  23. for ($n=0; $n < $ndeals; $n++ ) {
  24.     $stats[ sendmsg(sendmsg($deck, 'hand'), 'evaluate') ]++;
  25. }
  26. */
  27. for ($n=0; $n < $ndeals; $n++ ) {
  28.     $stats[ sendmsg(sendmsg($deck, 'hand', $hand), 'evaluate') ]++;
  29. }
  30. echo ' in ', time()-$starttime, ' seconds.', PHP_EOL;
  31.  
  32. static $handnames=array(
  33.     'NOTHING',
  34.     'ONEPAIR',
  35.     'TWOPAIRS',
  36.     'THREEOFKIND',
  37.     'STRAIGHT',
  38.     'FLUSH',
  39.     'FULLHOUSE',
  40.     'FOUROFKIND',
  41.     'STRAIGHTFLUSH',
  42. );
  43.  
  44. // find longest header
  45. $width = 0;
  46. foreach ($handnames as $s) {
  47.     $w = strlen($s);
  48.     if ($w > $width) {
  49.         $width=$w;
  50.     }
  51. }
  52.  
  53. for ($i=0; $i < 9; $i++ ) {
  54.     echo str_repeat(' ', $width - strlen($handnames[$i])), $handnames[$i], '->', $stats[$i], "\t", sprintf('%5.2f%%', $stats[$i]/($ndeals/100.0)), PHP_EOL;
  55. }
$ php -f testDeck.php
2c,3c,4c,5c,6c,7c,8c,9c,10c,Jc,Qc,Kc,Ac,2d,3d,4d,5d,6d,7d,8d,9d,10d,Jd,Qd,Kd,Ad,
2h,3h,4h,5h,6h,7h,8h,9h,10h,Jh,Qh,Kh,Ah,2s,3s,4s,5s,6s,7s,8s,9s,10s,Js,Qs,Ks,As
7c,2s,6h,7s,9s,4d,5d,10h,Kh,3h,2c,As,Js,Qh,5c,2h,6c,9h,5s,8d,4s,Ac,Qs,Qd,Jc,6d
10d,Ah,8c,Ks,8h,Kc,8s,7d,3s,Kd,9d,2d,10s,3c,5h,Qc,7h,Jh,Jd,4h,10c,3d,9c,4c,6s,Ad
7c
2s,6h,7s,9s,4d -> NOTHING
5d,10h,Kh,3h,2c -> NOTHING
As,Js,Qh,5c,2h -> NOTHING
6c,9h,5s,8d,4s -> NOTHING
Ac,Qs,Qd,Jc,6d -> ONEPAIR
Dealing 10000 hands... in 4 seconds.
      NOTHING->4973	49.73%
      ONEPAIR->4245	42.45%
     TWOPAIRS->461	 4.61%
  THREEOFKIND->242	 2.42%
     STRAIGHT->44	 0.44%
        FLUSH->15	 0.15%
    FULLHOUSE->18	 0.18%
   FOUROFKIND->1	 0.01%
STRAIGHTFLUSH->1	 0.01%

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].