A hand
- namespace Hand;
- use \InvalidArgumentException as InvalidArgumentException;
- require_once 'So-o.php';
- require_once 'Card.php';
Places the code of the class in its own namespace. Loads So-o and the Card class.
- 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);
Defines a constant for every hand value from the weakest to the strongest.
- 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.
- 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
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
.
- 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
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.
- 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()));
Sets the PHP configuration parameter include_path
to the current directory and the directory containing the So-o code.
- require_once 'So-o.php';
- require_once 'Hand.php';
Loads So-o and the Hand class.
- $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);
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.
- $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);
- }
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