26
A deck of cards
- import { defclass, sendmsg, supersend } from 'So-o';
- import 'Hand';
- defclass('Deck', null, 1,
- null,
- ['cards', 'top', 'shuffleWhenEmpty'],
- null,
The Deck class adds the instance properties cards, top and shuffleWhenEmpty, redefines the instance messages init and toString, adds the instance messages shuffle, check, deal and hand.
- { 'init':
- (self, swe = false) => {
- sendmsg(self, 'set', 'shuffleWhenEmpty', swe ? true : false);
- let cards = [];
- for (let s = 0; s < 4; s++)
- for (let 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;
- },
- 'shuffle':
- (self) => {
- let cards = sendmsg(self, 'get', 'cards');
- let len = cards.length;
- for (let i = cards.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- const c = cards[j];
- cards[j] = cards[i], cards[i] = c;
- }
- sendmsg(self, 'set', 'cards', cards);
- sendmsg(self, 'set', 'top', 0);
- return self;
- },
- 'check':
- (self) => {
- let 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;
- },
- 'deal':
- (self) => {
- let cards = sendmsg(self, 'get', 'cards');
- let top = sendmsg(self, 'get', 'top');
- let c = cards[top];
- sendmsg(self, 'check');
- return c;
- },
- 'hand':
- (self, hand = null) => {
- let cards = [];
- for (let n = 0; n < 5; n++)
- cards.push(sendmsg(self, 'deal'));
- return hand ? sendmsg(hand, 'init', cards) : sendmsg(Hand, 'new', cards);
- },
- 'toString':
- (self) => {
- let cards = sendmsg(self, 'get', 'cards');
- let top = sendmsg(self, 'get', 'top');
- let s = cards.map((c) => sendmsg(c, 'toString'));
- return s.join(',', s) + ' ' + top;
- }
- }
- );
- import { sendmsg } from 'So-o';
- import 'Deck';
- let deck = sendmsg(Deck, 'new', true);
- console.log(sendmsg(deck, 'toString'));
- sendmsg(deck, 'shuffle');
- console.log(sendmsg(deck, 'toString'));
- let card = sendmsg(deck, 'deal');
- console.log(sendmsg(card, 'toString'));
- for (let n = 0; n < 5; n++)
- console.log(sendmsg(sendmsg(deck, 'hand'), 'toString'));
- let ndeals = 100000;
- let stats = [];
- for (let i = 0; i < Hand.hand2s.length; i++)
- stats[i] = 0;
- let hand = sendmsg(deck, 'hand');
- let startTime = Date.now();
- console.log('Dealing ' + ndeals + ' hands...');
- for (let n = 0; n < ndeals; n++ )
- stats[ sendmsg(sendmsg(deck, 'hand', hand), 'evaluate') ]++;
- let endTime = Date.now();
- console.log( 'in ' + (endTime - startTime) / 1000 + ' s.');
- let maxw = 0;
- for (let s of Hand.hand2s) {
- const w = s.length;
- if (w > maxw)
- maxw = w;
- }
- let formatter = new Intl.NumberFormat('en-US', { style: 'percent', minimumFractionDigits: 2 });
- for (let i = 0; i < Hand.hand2s.length; i++)
- console.log(Hand.hand2s[i].padStart(maxw) + '->' + stats[i] + "\t" + formatter.format(stats[i]/ndeals).padStart(6));
$ ln poker/testDeck.js testDeck.mjs
$ nodejs --experimental-modules testDeck
2c,3c,4c,5c,6c,7c,8c,9c,Tc,Jc,Qc,Kc,Ac,2d,3d,4d,5d,6d,7d,8d,9d,Td,Jd,Qd,Kd,Ad,2h,3h,4h,5h,6h,7h,8h,9h,Th,Jh,Qh,Kh,Ah,2s,3s,4s,5s,6s,7s,8s,9s,Ts,Js,Qs,Ks,As 0
Qd,4d,7h,2h,Jc,2s,6c,8h,7s,3d,3c,Ac,Qh,2d,9d,8s,Kc,3h,6s,Js,5h,8c,Qs,9h,5d,7c,Kh,6d,Th,8d,4s,Jd,2c,As,Ad,5c,Ts,7d,4c,Kd,6h,9c,9s,4h,Ks,5s,3s,Qc,Jh,Td,Ah,Tc 0
Qd
4d,7h,2h,Jc,2s -> ONEPAIR
6c,8h,7s,3d,3c -> ONEPAIR
Ac,Qh,2d,9d,8s -> NOTHING
Kc,3h,6s,Js,5h -> NOTHING
8c,Qs,9h,5d,7c -> NOTHING
Dealing 100000 hands...
in 1.41 s.
NOTHING->49889 49.89%
ONEPAIR->42259 42.26%
TWOPAIRS->4891 4.89%
THREEOFKIND->2198 2.20%
STRAIGHT->372 0.37%
FLUSH->208 0.21%
FULLHOUSE->150 0.15%
FOUROFKIND->31 0.03%
STRAIGHTFLUSH->2 0.00%
Comments