12
A deck of cards
- namespace Deck;
- require_once 'So-o.php';
- require_once 'Card.php';
- require_once 'Hand.php';
Places the code of the class in its own namespace. Loads So-o and the Card class.
- defclass('Deck', null, 1, null, array('cards', 'top', 'shuffleWhenEmpty'), null, array('init', 'deal', 'hand', 'shuffle', 'check', 'toString'));
The Deck class inherits from the Object class.
An instance has 3 properties: cards, the set of cards, top which holds the index in cards of the card on the top of the deck, i.e. the next which will be drawn, and shuffleWhenEmpty which if true
automatically shuffles the deck when all the cards have been drawn.
An instance redefines the messages init and toString.
It implements the messages deal, hand, shuffle and check.
- function i_init($self, $swe=false) {
- global $Card;
- supersend('init', func_get_args());
- sendmsg($self, 'set', 'shuffleWhenEmpty', $swe ? true : false);
- $cards=array();
- for ($s = 0; $s < 4; $s++) {
- for ($r = 0; $r < 13; $r++) {
- $cards[ 13*$s+$r ] = sendmsg($Card, 'new', $r, $s);
- }
- }
- sendmsg($self, 'set', 'cards', $cards);
- sendmsg($self, 'set', 'top', 0);
- return $self;
- }
- function i_shuffle($self) {
- $cards=sendmsg($self, 'get', 'cards');
- shuffle($cards);
- sendmsg($self, 'set', 'cards', $cards);
- sendmsg($self, 'set', 'top', 0);
- return $self;
- }
- function i_check($self) {
- $top=sendmsg($self, 'get', 'top');
- if (++$top >= 52) {
- if (sendmsg($self, 'get', 'shuffleWhenEmpty')) {
- sendmsg($self, 'shuffle');
- }
- $top=0;
- }
- sendmsg($self, 'set', 'top', $top);
- return $self;
- }
- function i_deal($self) {
- $cards=sendmsg($self, 'get', 'cards');
- $top=sendmsg($self, 'get', 'top');
- $c = $cards[$top];
- sendmsg($self, 'check');
- return $c;
- }
- function i_hand($self, $hand=null) {
- global $Hand;
- $cards=array();
- for ($n=0; $n < 5; $n++) {
- $cards[]=sendmsg($self, 'deal');
- }
- return $hand ? sendmsg($hand, 'init', $cards) : sendmsg($Hand, 'new', $cards);
- }
- function i_toString($self) {
- $cards=sendmsg($self, 'get', 'cards');
- $top=sendmsg($self, 'get', 'top');
- foreach ($cards as $c) {
- $s[]=sendmsg($c, 'toString');
- }
- return implode(',', $s) . ' ' . $top;
- }
- set_include_path(getcwd() . PATH_SEPARATOR . dirname(getcwd()));
- require_once 'So-o.php';
- require_once 'Deck.php';
Sets the PHP configuration parameter include_path
to the current directory and the directory containing the So-o code.
Loads So-o and the Deck class.
- $deck=sendmsg($Deck, 'new', true);
- sendmsg($deck, 'print', true);
- sendmsg($deck, 'shuffle');
- sendmsg($deck, 'print', true);
- $card=sendmsg($deck, 'deal');
- sendmsg($card, 'print', true);
- for ($n=0; $n < 5; $n++) {
- sendmsg(sendmsg($deck, 'hand'), 'print', true);
- }
- $ndeals=10000;
- $stats=array();
- for ($i=0; $i < 9; $i++ ) {
- $stats[$i] = 0;
- }
- echo 'Dealing ', $ndeals, ' hands...';
- $starttime=time();
- $hand=sendmsg($deck, 'hand');
- /*
- for ($n=0; $n < $ndeals; $n++ ) {
- $stats[ sendmsg(sendmsg($deck, 'hand'), 'evaluate') ]++;
- }
- */
- for ($n=0; $n < $ndeals; $n++ ) {
- $stats[ sendmsg(sendmsg($deck, 'hand', $hand), 'evaluate') ]++;
- }
- echo ' in ', time()-$starttime, ' seconds.', PHP_EOL;
- static $handnames=array(
- 'NOTHING',
- 'ONEPAIR',
- 'TWOPAIRS',
- 'THREEOFKIND',
- 'STRAIGHT',
- 'FLUSH',
- 'FULLHOUSE',
- 'FOUROFKIND',
- 'STRAIGHTFLUSH',
- );
- // find longest header
- $width = 0;
- foreach ($handnames as $s) {
- $w = strlen($s);
- if ($w > $width) {
- $width=$w;
- }
- }
- for ($i=0; $i < 9; $i++ ) {
- echo str_repeat(' ', $width - strlen($handnames[$i])), $handnames[$i], '->', $stats[$i], "\t", sprintf('%5.2f%%', $stats[$i]/($ndeals/100.0)), PHP_EOL;
- }
$ 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%
Comments