Une main
- namespace Hand;
- use \InvalidArgumentException as InvalidArgumentException;
- require_once 'So-o.php';
- require_once 'Card.php';
Place le code de la classe dans son espace de nommage. Charge So-o et la classe Card.
- define(__NAMESPACE__ . '\NOTHING', 0);
- define(__NAMESPACE__ . '\ONEPAIR', 1);
- define(__NAMESPACE__ . '\TWOPAIRS', 2);
- define(__NAMESPACE__ . '\THREEOFKIND', 3);
- define(__NAMESPACE__ . '\STRAIGHT', 4);
- define(__NAMESPACE__ . '\FLUSH', 5);
- define(__NAMESPACE__ . '\FULLHOUSE', 6);
- define(__NAMESPACE__ . '\FOUROFKIND', 7);
- define(__NAMESPACE__ . '\STRAIGHTFLUSH', 8);
Definit une constante pour chaque valeur d'une main de la plus faible à la plus forte.
- 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.
- function i_init($self, $cards) {
- if (!is_array($cards) or count($cards) != 5) {
- throw new InvalidArgumentException();
- }
- supersend('init', func_get_args());
- sendmsg($self, 'set', 'cards', $cards);
- return $self;
- }
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
.
- function i_reorder($self) {
- $cards=sendmsg($self, 'get', 'cards');
- usort($cards, create_function('$c1, $c2', "return sendmsg(\$c1, 'compare', \$c2);"));
- sendmsg($self, 'set', 'cards', $cards);
- return $self;
- }
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.
- function i_card($self, $n) {
- if (!(is_int($n) and $n >= 1 and $n <= 5)) {
- throw new InvalidArgumentException();
- }
- $cards=sendmsg($self, 'get', 'cards');
- return $cards[$n-1];
- }
- function i_setCard($self, $n, $card) {
- if (!(is_int($n) and $n >= 1 and $n <= 5)) {
- throw new InvalidArgumentException();
- }
- $cards=sendmsg($self, 'get', 'cards');
- $cards[$n-1]=$card;
- return sendmsg($self, 'set', 'cards', $cards);
- }
- function i_toString($self) {
- static $handnames=array(
- 'NOTHING',
- 'ONEPAIR',
- 'TWOPAIRS',
- 'THREEOFKIND',
- 'STRAIGHT',
- 'FLUSH',
- 'FULLHOUSE',
- 'FOUROFKIND',
- 'STRAIGHTFLUSH',
- );
- $cards=sendmsg($self, 'get', 'cards');
- $eval=sendmsg($self, 'evaluate');
- foreach ($cards as $c) {
- $s[]=sendmsg($c, 'toString');
- }
- return implode(',', $s) . ' -> ' . $handnames[$eval];
- }
- function i_isOnePair($self) {
- // aabcd abbcd abccd abcdd
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r1 == $r2 && $r2 != $r3 && $r3 != $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 == $r3 && $r3 != $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 != $r3 && $r3 == $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 != $r3 && $r3 != $r4 && $r4 == $r5) {
- return true;
- }
- return false;
- }
- function i_isTwoPairs($self) {
- // aabbc aabcc abbcc
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r1 == $r2 && $r2 != $r3 && $r3 == $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 == $r2 && $r2 != $r3 && $r3 != $r4 && $r4 == $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 == $r3 && $r3 != $r4 && $r4 == $r5) {
- return true;
- }
- return false;
- }
- function i_isThreeOfKind($self) {
- // aaabc abbbc abccc
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r1 == $r2 && $r2 == $r3 && $r3 != $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 == $r3 && $r3 == $r4 && $r4 != $r5) {
- return true;
- }
- if ($r1 != $r2 && $r2 != $r3 && $r3 == $r4 && $r4 == $r5) {
- return true;
- }
- return false;
- }
- function i_isStraight($self) {
- // a(a+1)(a+2)(a+3)(a+4)
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r5 == $r4+1 && $r4 == $r3+1 && $r3 == $r2+1 && $r2 == $r1+1) {
- return true; // could be a straight flush
- }
- return false;
- }
- function i_isFlush($self) {
- $cards=sendmsg($self, 'get', 'cards');
- $s1 = sendmsg($cards[0], 'suit');
- $s2 = sendmsg($cards[1], 'suit');
- $s3 = sendmsg($cards[2], 'suit');
- $s4 = sendmsg($cards[3], 'suit');
- $s5 = sendmsg($cards[4], 'suit');
- if ($s1 == $s2 && $s2 == $s3 && $s3 == $s4 && $s4 == $s5) {
- return true; // could be a straight flush
- }
- return false;
- }
- function i_isFullHouse($self) {
- // aaabb aabbb
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r1 == $r2 && $r2 == $r3 && $r3 != $r4 && $r4 == $r5) {
- return true;
- }
- if ($r1 == $r2 && $r2 != $r3 && $r3 == $r4 && $r4 == $r5) {
- return true;
- }
- return false;
- }
- function i_isFourOfKind($self) {
- // aaaab abbbb
- $cards=sendmsg($self, 'get', 'cards');
- $r1 = sendmsg($cards[0], 'rank');
- $r2 = sendmsg($cards[1], 'rank');
- $r3 = sendmsg($cards[2], 'rank');
- $r4 = sendmsg($cards[3], 'rank');
- $r5 = sendmsg($cards[4], 'rank');
- if ($r1 == $r2 && $r2 == $r3 && $r3 == $r4) {
- return true;
- }
- if ($r2 == $r3 && $r3 == $r4 && $r4 == $r5) {
- return true;
- }
- return false;
- }
- function i_isStraightFlush($self) {
- // a(a+1)(a+2)(a+3)(a+4)
- if (sendmsg($self, 'isStraight') and sendmsg($self, 'isFlush')) {
- return true;
- }
- return false;
- }
- function i_evaluate($self) {
- // sort or nothing works!
- $copy=sendmsg(sendmsg($self, 'copy'), 'reorder');
- // DON'T change order
- if (sendmsg($copy, 'isStraightFlush')) {
- return STRAIGHTFLUSH;
- }
- if (sendmsg($copy, 'isFourOfKind')) {
- return FOUROFKIND;
- }
- if (sendmsg($copy, 'isFullHouse')) {
- return FULLHOUSE;
- }
- if (sendmsg($copy, 'isFlush')) {
- return FLUSH;
- }
- if (sendmsg($copy, 'isStraight')) {
- return STRAIGHT;
- }
- if (sendmsg($copy, 'isThreeOfKind')) {
- return THREEOFKIND;
- }
- if (sendmsg($copy, 'isTwoPairs')) {
- return TWOPAIRS;
- }
- if (sendmsg($copy, 'isOnePair')) {
- return ONEPAIR;
- }
- return NOTHING;
- }
- 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.
- require_once 'So-o.php';
- require_once 'Hand.php';
Charge So-o et la classe Hand.
- $card1=sendmsg($Card, 'new', \Card\ACE, \Card\SPADES);
- $card2=sendmsg($Card, 'new', \Card\THREE, \Card\CLUBS);
- $card3=sendmsg($Card, 'new', \Card\ACE, \Card\DIAMONDS);
- $card4=sendmsg($Card, 'new', \Card\JACK, \Card\HEARTS);
- $card5=sendmsg($Card, 'new', \Card\SIX, \Card\SPADES);
- $hand=sendmsg($Hand, 'new', array($card1, $card2, $card3, $card4, $card5));
- sendmsg($hand, 'print', true);
- sendmsg($hand, 'reorder');
- sendmsg($hand, 'print', true);
- sendmsg(sendmsg($hand, 'card', 1), 'print', true);
- sendmsg($hand, 'setCard', 2, sendmsg($Card, 'new', \Card\ACE, \Card\HEARTS));
- 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.
- $testhands=array(
- 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)),
- 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)),
- 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)),
- 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)),
- 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)),
- 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)),
- 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)),
- 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)),
- );
- foreach ($testhands as $h) {
- $cards=array();
- foreach ($h as $c) {
- $cards[]=sendmsg($Card, 'new', $c[0], $c[1]);
- }
- sendmsg(sendmsg($Hand, 'new', $cards), 'print', true);
- }
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