26
Un jeu de cartes
- namespace Deck;
- require_once 'So-o.php';
- require_once 'Card.php';
- require_once 'Hand.php';
Place le code de la classe dans son espace de nommage. Charge So-o et les classes Card et Hand.
- defclass('Deck', null, 1, null, array('cards', 'top', 'shuffleWhenEmpty'), null, array('init', 'deal', 'hand', 'shuffle', 'check', 'toString'));
La classe Deck hérite de la classe Object.
Une instance a 3 propriétés : cards, le jeu complet de cartes, top qui contient l'index dans cards de la carte en haut du jeu, i.e. la prochaine qui sera tirée, et shuffleWhenEmpty qui si à true
mélange automatiquement le jeu quand la dernière carte a été tirée.
Une instance redéfinit les messages init et toString.
Elle implémente les messages deal, hand, shuffle et 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';
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.
Charge So-o et la classe Deck.
- $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%
Commentaires