25

Un jeu de cartes

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

Place le code de la classe dans son espace de nommage. Charge So-o et les classes Card et Hand.

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

La classe Deck hérite de la classe Object. Une instance a 3 propriétés : cards, le jeu complet de cartes, top qui contient l'index dans cards de la carte en haut du jeu, i.e. la prochaine qui sera tirée, et shuffleWhenEmpty qui si à true mélange automatiquement le jeu quand la dernière carte a été tirée. Une instance redéfinit les messages init et toString. Elle implémente les messages deal, hand, shuffle et 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';

Met le paramètre de configuration include_path de PHP au répertoire courant et au répertoire qui contient le code de So-o. Charge So-o et la classe Deck.

  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%

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[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]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].