A deck of cards

  1. import { defclass, sendmsg, supersend } from 'So-o';
  2.  
  3. import 'Hand';
  4.  
  5. defclass('Deck', null, 1,
  6.     null,
  7.     ['cards', 'top', 'shuffleWhenEmpty'],
  8.     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.

  1.     {   'init':
  2.             (self, swe = false) => {
  3.                 sendmsg(self, 'set', 'shuffleWhenEmpty', swe ? true : false);
  4.  
  5.                 let cards = [];
  6.  
  7.                 for (let s = 0; s < 4; s++)
  8.                     for (let r = 0; r < 13; r++)
  9.                         cards[ 13*s+r ] = sendmsg(Card, 'new', r, s);
  10.  
  11.                 sendmsg(self, 'set', 'cards', cards);
  12.                 sendmsg(self, 'set', 'top', 0);
  13.  
  14.                 return self;
  15.             },
  1.         'shuffle':
  2.             (self) => {
  3.                     let cards = sendmsg(self, 'get', 'cards');
  4.                     let len = cards.length;
  5.  
  6.                     for (let i = cards.length - 1; i > 0; i--) {
  7.                         const j = Math.floor(Math.random() * (i + 1));
  8.                         const c = cards[j];
  9.                        
  10.                         cards[j] = cards[i], cards[i] = c;
  11.                     }
  12.  
  13.                     sendmsg(self, 'set', 'cards', cards);
  14.                     sendmsg(self, 'set', 'top', 0);
  15.  
  16.                     return self;
  17.                 },
  1.         'check':
  2.             (self) => {
  3.                 let top = sendmsg(self, 'get', 'top');
  4.  
  5.                 if (++top >= 52) {
  6.                     if (sendmsg(self, 'get', 'shuffleWhenEmpty'))
  7.                         sendmsg(self, 'shuffle');
  8.  
  9.                     top = 0;
  10.                 }
  11.  
  12.                 sendmsg(self, 'set', 'top', top);
  13.  
  14.                 return self;
  15.             },
  1.         'deal':
  2.             (self) => {
  3.                 let cards = sendmsg(self, 'get', 'cards');
  4.                 let top = sendmsg(self, 'get', 'top');
  5.  
  6.                 let c = cards[top];
  7.  
  8.                 sendmsg(self, 'check');
  9.  
  10.                 return c;
  11.             },
  1.         'hand':
  2.             (self, hand = null) => {
  3.                 let cards = [];
  4.  
  5.                 for (let n = 0; n < 5; n++)
  6.                     cards.push(sendmsg(self, 'deal'));
  7.  
  8.                 return hand ? sendmsg(hand, 'init', cards) : sendmsg(Hand, 'new', cards);
  9.             },
  1.         'toString':
  2.             (self) => {
  3.                 let cards = sendmsg(self, 'get', 'cards');
  4.                 let top = sendmsg(self, 'get', 'top');
  5.  
  6.                 let s = cards.map((c) => sendmsg(c, 'toString'));
  7.  
  8.                 return s.join(',', s) + ' ' + top;
  9.             }
  10.     }
  11. );
  1. import { sendmsg } from 'So-o';
  2.  
  3. import 'Deck';
  4.  
  5. let deck = sendmsg(Deck, 'new', true);
  6.  
  7. console.log(sendmsg(deck, 'toString'));
  8. sendmsg(deck, 'shuffle');
  9. console.log(sendmsg(deck, 'toString'));
  10.  
  11. let card = sendmsg(deck, 'deal');
  12.  
  13. console.log(sendmsg(card, 'toString'));
  14.  
  15. for (let n = 0; n < 5; n++)
  16.     console.log(sendmsg(sendmsg(deck, 'hand'), 'toString'));
  17.  
  18. let ndeals = 100000;
  19. let stats = [];
  20. for (let i = 0; i < Hand.hand2s.length; i++)
  21.     stats[i] = 0;
  22.  
  23. let hand = sendmsg(deck, 'hand');
  24.  
  25. let startTime = Date.now();
  26.  
  27. console.log('Dealing ' + ndeals + ' hands...');
  28.  
  29. for (let n = 0; n < ndeals; n++ )
  30.     stats[ sendmsg(sendmsg(deck, 'hand', hand), 'evaluate') ]++;
  31.  
  32. let endTime = Date.now();
  33.  
  34. console.log( 'in ' + (endTime - startTime) / 1000 + ' s.');
  35.  
  36. let maxw = 0;
  37.  
  38. for (let s of Hand.hand2s) {
  39.     const w = s.length;
  40.  
  41.     if (w > maxw)
  42.         maxw = w;
  43. }
  44.  
  45. let formatter = new Intl.NumberFormat('en-US', { style: 'percent', minimumFractionDigits: 2 });
  46.  
  47. for (let i = 0; i < Hand.hand2s.length; i++)
  48.     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%
SEE ALSO

The JavaScript manual

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