3

A hand

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

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

  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);

Defines a constant for every hand value from the weakest to the strongest.

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

The Hand class inherits from the Object class. An instance has 1 property: cards, the cards it contains. An instance redefines the messages init and toString. It implements the messages reorder, card, setCard, isOnePair, isTwoPairs, isThreeOfKind, isStraight, isFlush, isFullHouse, isFourOfKind, isStraightFlush and 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 takes 1 argument: $cards, the 5 cards of the hand. It checks if $cards is an array of 5 elements and initializes the cards property of $self with $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 sorts the cards of $self from the weakest value to the strongest. 2 cards are compared by sending the compare message to a card with another card in 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()));

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

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

Loads So-o and the Hand class.

  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);

Creates a hand with a pair of aces, displays it, reorders it and displays the hand again. Displays the first card of the hand. Changes the second card of the hand. Displays the hand.

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

Displays all the different possible combinations of a hand.

$ 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

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