17
Une main
- import { defclass, sendmsg, supersend } from 'So-o';
- import 'Card';
- defclass('Hand', null, 1,
- null,
- ['cards'],
- null,
La classe Hand ajoute la propriété d'instance cards, redéfinit les messages d'instance init, copy et toString, ajoute les messages d'instance card, setCard, reorder, isOnePair, isTwoPairs, isThreeOfKind, isStraight, isFlush, isFullHouse, isFourOfKind, isStraightFlush et evaluate.
- { 'init':
- (self, cards) => {
- if (! (Array.isArray(cards) && cards.length == 5))
- throw new TypeError();
- sendmsg(self, 'set', 'cards', cards);
- return self;
- },
- 'copy':
- (self) => {
- let co = supersend(Hand, self, 'copy');
- let cards = sendmsg(co, 'get', 'cards');
- return sendmsg(co, 'set', 'cards', cards.slice(0));
- },
- 'reorder':
- (self) => {
- let cards = sendmsg(self, 'get', 'cards');
- cards.sort((c1, c2) => sendmsg(c1, 'compare', c2));
- return self;
- },
- 'card':
- (self, n) => {
- if (! (Number.isInteger(n) && n >= 1 && n <= 5))
- throw new RangeError();
- let cards = sendmsg(self, 'get', 'cards');
- return cards[n-1];
- },
- 'setCard':
- (self, n, card) => {
- if (! (Number.isInteger(n) && n >= 1 && n <= 5))
- throw new RangeError();
- let cards = sendmsg(self, 'get', 'cards');
- cards[n-1] = card;
- return self;
- },
- 'isOnePair':
- (self) => {
- // aabcd abbcd abccd abcdd
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isTwoPairs':
- (self) => {
- // aabbc aabcc abbcc
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isThreeOfKind':
- (self) => {
- // aabbc aabcc abbcc
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isStraight':
- (self) => {
- // a(a+1)(a+2)(a+3)(a+4)
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isFlush':
- (self) => {
- let cards = sendmsg(self, 'get', 'cards');
- let s1 = sendmsg(cards[0], 'suit');
- let s2 = sendmsg(cards[1], 'suit');
- let s3 = sendmsg(cards[2], 'suit');
- let s4 = sendmsg(cards[3], 'suit');
- let s5 = sendmsg(cards[4], 'suit');
- if (s1 == s2 && s2 == s3 && s3 == s4 && s4 == s5)
- return true; // could be a straight flush
- return false;
- },
- 'isFullHouse':
- (self) => {
- // aaabb aabbb
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isFourOfKind':
- (self) => {
- // aaaab abbbb
- let cards = sendmsg(self, 'get', 'cards');
- let r1 = sendmsg(cards[0], 'rank');
- let r2 = sendmsg(cards[1], 'rank');
- let r3 = sendmsg(cards[2], 'rank');
- let r4 = sendmsg(cards[3], 'rank');
- let 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;
- },
- 'isStraightFlush':
- (self) => {
- // a(a+1)(a+2)(a+3)(a+4)
- if (sendmsg(self, 'isStraight') && sendmsg(self, 'isFlush'))
- return true;
- return false;
- },
- 'evaluate':
- (self) => {
- // sort or nothing works!
- let copy = sendmsg(sendmsg(self, 'copy'), 'reorder');
- // DON'T change order
- if (sendmsg(copy, 'isStraightFlush'))
- return Hand.STRAIGHTFLUSH;
- if (sendmsg(copy, 'isFourOfKind'))
- return Hand.FOUROFKIND;
- if (sendmsg(copy, 'isFullHouse'))
- return Hand.FULLHOUSE;
- if (sendmsg(copy, 'isFlush'))
- return Hand.FLUSH;
- if (sendmsg(copy, 'isStraight'))
- return Hand.STRAIGHT;
- if (sendmsg(copy, 'isThreeOfKind'))
- return Hand.THREEOFKIND;
- if (sendmsg(copy, 'isTwoPairs'))
- return Hand.TWOPAIRS;
- if (sendmsg(copy, 'isOnePair'))
- return Hand.ONEPAIR;
- return Hand.NOTHING;
- },
- 'toString':
- (self) => {
- let val = sendmsg(self, 'evaluate');
- let cards = sendmsg(self, 'get', 'cards');
- let s = cards.map((c) => sendmsg(c, 'toString'));
- return s.join(',', s) + ' -> ' + Hand.hand2s[val];
- }
- }
- );
- Hand.NOTHING = 0;
- Hand.ONEPAIR = 1;
- Hand.TWOPAIRS = 2;
- Hand.THREEOFKIND = 3;
- Hand.STRAIGHT = 4;
- Hand.FLUSH = 5;
- Hand.FULLHOUSE = 6;
- Hand.FOUROFKIND = 7;
- Hand.STRAIGHTFLUSH = 8;
- Hand.hand2s = [
- 'NOTHING',
- 'ONEPAIR',
- 'TWOPAIRS',
- 'THREEOFKIND',
- 'STRAIGHT',
- 'FLUSH',
- 'FULLHOUSE',
- 'FOUROFKIND',
- 'STRAIGHTFLUSH',
- ];
- import { sendmsg } from 'So-o';
- import 'Hand';
- let card1 = sendmsg(Card, 'new', Card.ACE, Card.SPADES);
- let card2 = sendmsg(Card, 'new', Card.THREE, Card.CLUBS);
- let card3 = sendmsg(Card, 'new', Card.ACE, Card.DIAMONDS);
- let card4 = sendmsg(Card, 'new', Card.JACK, Card.HEARTS);
- let card5 = sendmsg(Card, 'new', Card.SIX, Card.SPADES);
- let hand = sendmsg(Hand, 'new', [card1, card2, card3, card4, card5]);
- console.log(sendmsg(hand, 'toString'));
- sendmsg(hand, 'reorder');
- console.log(sendmsg(hand, 'toString'));
- console.log(sendmsg(sendmsg(hand, 'card', 1), 'toString'));
- sendmsg(hand, 'setCard', 2, sendmsg(Card, 'new', Card.ACE, Card.HEARTS));
- console.log(sendmsg(hand, 'toString'));
- let testhands = [
- [[Card.JACK, Card.SPADES], [Card.KING, Card.HEARTS], [Card.ACE, Card.DIAMONDS], [Card.TWO, Card.CLUBS], [Card.FIVE, Card.SPADES]],
- [[Card.ACE, Card.SPADES], [Card.THREE, Card.CLUBS], [Card.FOUR, Card.DIAMONDS], [Card.THREE, Card.HEARTS], [Card.SIX, Card.SPADES]],
- [[Card.SEVEN, Card.SPADES], [Card.KING, Card.HEARTS], [Card.SEVEN, Card.DIAMONDS], [Card.JACK, Card.CLUBS], [Card.JACK, Card.SPADES]],
- [[Card.FOUR, Card.SPADES], [Card.NINE, Card.HEARTS], [Card.NINE, Card.DIAMONDS], [Card.EIGHT, Card.CLUBS], [Card.NINE, Card.SPADES]],
- [[Card.KING, Card.HEARTS], [Card.JACK, Card.DIAMONDS], [Card.QUEEN, Card.CLUBS], [Card.TEN, Card.SPADES], [Card.ACE, Card.DIAMONDS]],
- [[Card.FOUR, Card.HEARTS], [Card.NINE, Card.HEARTS], [Card.ACE, Card.HEARTS], [Card.SEVEN, Card.HEARTS], [Card.QUEEN, Card.HEARTS]],
- [[Card.FOUR, Card.SPADES], [Card.TEN, Card.HEARTS], [Card.TEN, Card.DIAMONDS], [Card.FOUR, Card.CLUBS], [Card.TEN, Card.SPADES]],
- [[Card.KING, Card.DIAMONDS], [Card.JACK, Card.DIAMONDS], [Card.QUEEN, Card.DIAMONDS], [Card.TEN, Card.DIAMONDS], [Card.ACE, Card.DIAMONDS]],
- ];
- for (let h of testhands) {
- let cards = h.map((c) => sendmsg(Card, 'perform', 'new', c));
- console.log(sendmsg(sendmsg(Hand, 'new', cards), 'toString'));
- }
$ ln poker/testHand.js testHand.mjs
$ nodejs --experimental-modules testHand
As,3c,Ad,Jh,6s -> ONEPAIR
3c,6s,Jh,As,Ad -> ONEPAIR
3c
3c,Ah,Jh,As,Ad -> 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,Ts,Ad -> STRAIGHT
4h,9h,Ah,7h,Qh -> FLUSH
4s,Th,Td,4c,Ts -> FULLHOUSE
Kd,Jd,Qd,Td,Ad -> STRAIGHTFLUSH
Commentaires