9

Une main

  1. namespace Hand;
  2.  
  3. use \InvalidArgumentException as InvalidArgumentException;
  4.  
  5. require_once 'So-o.php';
  6.  
  7. require_once 'Card.php';

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

  1. define(__NAMESPACE__ . '\NOTHING',          0);
  2. define(__NAMESPACE__ . '\ONEPAIR',          1);
  3. define(__NAMESPACE__ . '\TWOPAIRS',         2);
  4. define(__NAMESPACE__ . '\THREEOFKIND',      3);
  5. define(__NAMESPACE__ . '\STRAIGHT',         4);
  6. define(__NAMESPACE__ . '\FLUSH',            5);
  7. define(__NAMESPACE__ . '\FULLHOUSE',        6);
  8. define(__NAMESPACE__ . '\FOUROFKIND',       7);
  9. define(__NAMESPACE__ . '\STRAIGHTFLUSH',    8);

Definit une constante pour chaque valeur d'une main de la plus faible à la plus forte.

  1. defclass('Hand', null, 1, null, array('cards'), null, array('init', 'reorder', 'card', 'setCard', 'isOnePair', 'isTwoPairs', 'isThreeOfKind', 'isStraight', 'isFlush', 'isFullHouse', 'isFourOfKind', 'isStraightFlush', 'evaluate', 'toString'));

La classe Hand hérite de la classe Object. Une instance a 1 propriété : cards, les cartes qu'elle contient. Une instance redéfinit les messages init et toString. Elle implémente les messages reorder, card, setCard, isOnePair, isTwoPairs, isThreeOfKind, isStraight, isFlush, isFullHouse, isFourOfKind, isStraightFlush et evaluate.

  1. function i_init($self, $cards) {
  2.     if (!is_array($cards) or count($cards) != 5) {
  3.         throw new InvalidArgumentException();
  4.     }
  5.  
  6.     supersend('init', func_get_args());
  7.  
  8.     sendmsg($self, 'set', 'cards', $cards);
  9.  
  10.     return $self;
  11. }

init prend 1 argument : $cards, les 5 cartes de la main. Elle vérifie que $cards est un tableau de 5 éléments et initialise la propriété cards de $self avec $cards.

  1. function i_reorder($self) {
  2.     $cards=sendmsg($self, 'get', 'cards');
  3.  
  4.     usort($cards, create_function('$c1, $c2', "return sendmsg(\$c1, 'compare', \$c2);"));
  5.  
  6.     sendmsg($self, 'set', 'cards', $cards);
  7.  
  8.     return $self;
  9. }

reorder ordonne les cartes de $self de la valeur la plus petite à la plus forte. 2 cartes sont comparées en envoyant le message compare à une carte avec une autre en argument.

  1. function i_card($self, $n) {
  2.     if (!(is_int($n) and $n >= 1 and $n <= 5)) {
  3.         throw new InvalidArgumentException();
  4.     }
  5.  
  6.     $cards=sendmsg($self, 'get', 'cards');
  7.  
  8.     return $cards[$n-1];
  9. }
  1. function i_setCard($self, $n, $card) {
  2.     if (!(is_int($n) and $n >= 1 and $n <= 5)) {
  3.         throw new InvalidArgumentException();
  4.     }
  5.  
  6.     $cards=sendmsg($self, 'get', 'cards');
  7.  
  8.     $cards[$n-1]=$card;
  9.  
  10.     return sendmsg($self, 'set', 'cards', $cards);
  11. }
  1. function i_toString($self) {
  2.     static $handnames=array(
  3.         'NOTHING',
  4.         'ONEPAIR',
  5.         'TWOPAIRS',
  6.         'THREEOFKIND',
  7.         'STRAIGHT',
  8.         'FLUSH',
  9.         'FULLHOUSE',
  10.         'FOUROFKIND',
  11.         'STRAIGHTFLUSH',
  12.     );
  13.  
  14.     $cards=sendmsg($self, 'get', 'cards');
  15.     $eval=sendmsg($self, 'evaluate');
  16.  
  17.     foreach ($cards as $c) {
  18.         $s[]=sendmsg($c, 'toString');
  19.     }
  20.  
  21.     return implode(',', $s) . ' -> ' . $handnames[$eval];
  22. }
  1. function i_isOnePair($self) {
  2.     // aabcd abbcd abccd abcdd
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r1 == $r2 && $r2 != $r3 && $r3 != $r4 && $r4 != $r5) {
  12.         return true;
  13.     }
  14.     if ($r1 != $r2 && $r2 == $r3 && $r3 != $r4 && $r4 != $r5) {
  15.         return true;
  16.     }
  17.     if ($r1 != $r2 && $r2 != $r3 && $r3 == $r4 && $r4 != $r5) {
  18.         return true;
  19.     }
  20.     if ($r1 != $r2 && $r2 != $r3 && $r3 != $r4 && $r4 == $r5) {
  21.         return true;
  22.     }
  23.     return false;
  24. }
  1. function i_isTwoPairs($self) {
  2.     // aabbc aabcc abbcc
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r1 == $r2 && $r2 != $r3 && $r3 == $r4 && $r4 != $r5) {
  12.         return true;
  13.     }
  14.     if ($r1 == $r2 && $r2 != $r3 && $r3 != $r4 && $r4 == $r5) {
  15.         return true;
  16.     }
  17.     if ($r1 != $r2 && $r2 == $r3 && $r3 != $r4 && $r4 == $r5) {
  18.         return true;
  19.     }
  20.  
  21.     return false;
  22. }
  1. function i_isThreeOfKind($self) {
  2.     // aaabc abbbc abccc
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r1 == $r2 && $r2 == $r3 && $r3 != $r4 && $r4 != $r5) {
  12.         return true;
  13.     }
  14.     if ($r1 != $r2 && $r2 == $r3 && $r3 == $r4 && $r4 != $r5) {
  15.         return true;
  16.     }
  17.     if ($r1 != $r2 && $r2 != $r3 && $r3 == $r4 && $r4 == $r5) {
  18.         return true;
  19.     }
  20.  
  21.     return false;
  22. }
  1. function i_isStraight($self) {
  2.     // a(a+1)(a+2)(a+3)(a+4)
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r5 == $r4+1 && $r4 == $r3+1 && $r3 == $r2+1 && $r2 == $r1+1) {
  12.         return true;    // could be a straight flush
  13.     }
  14.  
  15.     return false;
  16. }
  1. function i_isFlush($self) {
  2.     $cards=sendmsg($self, 'get', 'cards');
  3.  
  4.     $s1 = sendmsg($cards[0], 'suit');
  5.     $s2 = sendmsg($cards[1], 'suit');
  6.     $s3 = sendmsg($cards[2], 'suit');
  7.     $s4 = sendmsg($cards[3], 'suit');
  8.     $s5 = sendmsg($cards[4], 'suit');
  9.  
  10.     if ($s1 == $s2 && $s2 == $s3 && $s3 == $s4 && $s4 == $s5) {
  11.         return true;    // could be a straight flush
  12.     }
  13.  
  14.     return false;
  15. }
  1. function i_isFullHouse($self) {
  2.     // aaabb aabbb
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r1 == $r2 && $r2 == $r3 && $r3 != $r4 && $r4 == $r5) {
  12.         return true;
  13.     }
  14.     if ($r1 == $r2 && $r2 != $r3 && $r3 == $r4 && $r4 == $r5) {
  15.         return true;
  16.     }
  17.  
  18.     return false;
  19. }
  1. function i_isFourOfKind($self) {
  2.     // aaaab abbbb
  3.     $cards=sendmsg($self, 'get', 'cards');
  4.  
  5.     $r1 = sendmsg($cards[0], 'rank');
  6.     $r2 = sendmsg($cards[1], 'rank');
  7.     $r3 = sendmsg($cards[2], 'rank');
  8.     $r4 = sendmsg($cards[3], 'rank');
  9.     $r5 = sendmsg($cards[4], 'rank');
  10.  
  11.     if ($r1 == $r2 && $r2 == $r3 && $r3 == $r4) {
  12.         return true;
  13.     }
  14.     if ($r2 == $r3 && $r3 == $r4 && $r4 == $r5) {
  15.         return true;
  16.     }
  17.  
  18.     return false;
  19. }
  1. function i_isStraightFlush($self) {
  2.     // a(a+1)(a+2)(a+3)(a+4)
  3.     if (sendmsg($self, 'isStraight') and sendmsg($self, 'isFlush')) {
  4.         return true;
  5.     }
  6.  
  7.     return false;
  8. }
  1. function i_evaluate($self) {
  2.     // sort or nothing works!
  3.     $copy=sendmsg(sendmsg($self, 'copy'), 'reorder');
  4.     // DON'T change order
  5.     if (sendmsg($copy, 'isStraightFlush')) {
  6.         return STRAIGHTFLUSH;
  7.     }
  8.     if (sendmsg($copy, 'isFourOfKind')) {
  9.         return FOUROFKIND;
  10.     }
  11.     if (sendmsg($copy, 'isFullHouse')) {
  12.         return FULLHOUSE;
  13.     }
  14.     if (sendmsg($copy, 'isFlush')) {
  15.         return FLUSH;
  16.     }
  17.     if (sendmsg($copy, 'isStraight')) {
  18.         return STRAIGHT;
  19.     }
  20.     if (sendmsg($copy, 'isThreeOfKind')) {
  21.         return THREEOFKIND;
  22.     }
  23.     if (sendmsg($copy, 'isTwoPairs')) {
  24.         return TWOPAIRS;
  25.     }
  26.     if (sendmsg($copy, 'isOnePair')) {
  27.         return ONEPAIR;
  28.     }
  29.  
  30.     return NOTHING;
  31. }
  1. set_include_path(getcwd() . PATH_SEPARATOR . dirname(getcwd()));

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.

  1. require_once 'So-o.php';
  2.  
  3. require_once 'Hand.php';

Charge So-o et la classe Hand.

  1. $card1=sendmsg($Card, 'new', \Card\ACE, \Card\SPADES);
  2. $card2=sendmsg($Card, 'new', \Card\THREE, \Card\CLUBS);
  3. $card3=sendmsg($Card, 'new', \Card\ACE, \Card\DIAMONDS);
  4. $card4=sendmsg($Card, 'new', \Card\JACK, \Card\HEARTS);
  5. $card5=sendmsg($Card, 'new', \Card\SIX, \Card\SPADES);
  6.  
  7. $hand=sendmsg($Hand, 'new', array($card1, $card2, $card3, $card4, $card5));
  8.  
  9. sendmsg($hand, 'print', true);
  10. sendmsg($hand, 'reorder');
  11. sendmsg($hand, 'print', true);
  12. sendmsg(sendmsg($hand, 'card', 1), 'print', true);
  13. sendmsg($hand, 'setCard', 2, sendmsg($Card, 'new', \Card\ACE, \Card\HEARTS));
  14. sendmsg($hand, 'print', true);

Crée une main avec une paire d'as, l'affiche, l'ordonne et affiche de nouveau la main. Affiche la première carte de la main. Change la second carte de la main. Affiche la main.

  1. $testhands=array(
  2.     array(array(\Card\JACK, \Card\SPADES), array(\Card\KING, \Card\HEARTS), array(\Card\ACE, \Card\DIAMONDS), array(\Card\TWO, \Card\CLUBS), array(\Card\FIVE, \Card\SPADES)),
  3.     array(array(\Card\ACE, \Card\SPADES), array(\Card\THREE, \Card\CLUBS), array(\Card\FOUR, \Card\DIAMONDS), array(\Card\THREE, \Card\HEARTS), array(\Card\SIX, \Card\SPADES)),
  4.     array(array(\Card\SEVEN, \Card\SPADES), array(\Card\KING, \Card\HEARTS), array(\Card\SEVEN, \Card\DIAMONDS), array(\Card\JACK, \Card\CLUBS), array(\Card\JACK, \Card\SPADES)),
  5.     array(array(\Card\FOUR, \Card\SPADES), array(\Card\NINE, \Card\HEARTS), array(\Card\NINE, \Card\DIAMONDS), array(\Card\EIGHT, \Card\CLUBS), array(\Card\NINE, \Card\SPADES)),
  6.     array(array(\Card\KING, \Card\HEARTS), array(\Card\JACK, \Card\DIAMONDS), array(\Card\QUEEN, \Card\CLUBS), array(\Card\TEN, \Card\SPADES), array(\Card\ACE, \Card\DIAMONDS)),
  7.     array(array(\Card\FOUR, \Card\HEARTS), array(\Card\NINE, \Card\HEARTS), array(\Card\ACE, \Card\HEARTS), array(\Card\SEVEN, \Card\HEARTS), array(\Card\QUEEN, \Card\HEARTS)),
  8.     array(array(\Card\FOUR, \Card\SPADES), array(\Card\TEN, \Card\HEARTS), array(\Card\TEN, \Card\DIAMONDS), array(\Card\FOUR, \Card\CLUBS), array(\Card\TEN, \Card\SPADES)),
  9.     array(array(\Card\KING, \Card\DIAMONDS), array(\Card\JACK, \Card\DIAMONDS), array(\Card\QUEEN, \Card\DIAMONDS), array(\Card\TEN, \Card\DIAMONDS), array(\Card\ACE, \Card\DIAMONDS)),
  10. );
  11.  
  12. foreach ($testhands as $h) {
  13.     $cards=array();
  14.     foreach ($h as $c) {
  15.         $cards[]=sendmsg($Card, 'new', $c[0], $c[1]);
  16.     }
  17.     sendmsg(sendmsg($Hand, 'new', $cards), 'print', true);
  18. }

Affiche toutes les différentes combinaisons possibles d'une main.

$ php -f testHand.php 
As,3c,Ad,Jh,6s -> ONEPAIR
3c,6s,Jh,Ad,As -> ONEPAIR
3c
3c,Ah,Jh,Ad,As -> THREEOFKIND
Js,Kh,Ad,2c,5s -> NOTHING
As,3c,4d,3h,6s -> ONEPAIR
7s,Kh,7d,Jc,Js -> TWOPAIRS
4s,9h,9d,8c,9s -> THREEOFKIND
Kh,Jd,Qc,10s,Ad -> STRAIGHT
4h,9h,Ah,7h,Qh -> FLUSH
4s,10h,10d,4c,10s -> FULLHOUSE
Kd,Jd,Qd,10d,Ad -> STRAIGHTFLUSH

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].