A game card

  1. import { defclass, sendmsg, supersend } from 'So-o';
  2.  
  3. defclass('Card', null, 1,
  4.     null,
  5.     ['rank', 'suit'],
  6.     null,

The Card class adds the instance properties rank and a suit, redefines the instance messages init and toString, adds the instance messages rank, suit and compare.

  1.     {   'init':
  2.             (self, rank, suit) => {
  3.                 sendmsg(self, 'set', 'rank', rank);
  4.                 sendmsg(self, 'set', 'suit', suit);
  5.  
  6.                 return self;
  7.             },
  1.         'rank':
  2.             (self) => sendmsg(self, 'get', 'rank'),
  1.         'suit':
  2.             (self) => sendmsg(self, 'get', 'suit'),
  1.         'compare':
  2.             (self, card) => {
  3.                     let rank1 = sendmsg(self, 'get', 'rank');
  4.                     let rank2 = sendmsg(card, 'get', 'rank');
  5.  
  6.                     return rank1 == rank2 ? 0 : rank1 > rank2 ? 1 : -1;
  7.                 },
  1.         'toString':
  2.             (self) => Card.rank2s[sendmsg(self, 'get', 'rank')] + Card.suit2s[sendmsg(self, 'get', 'suit')]
  3.     }
  4. );
  1. Card.TWO    = 0;
  2. Card.THREE  = 1;
  3. Card.FOUR   = 2;
  4. Card.FIVE   = 3;
  5. Card.SIX    = 4;
  6. Card.SEVEN  = 5;
  7. Card.EIGHT  = 6;
  8. Card.NINE   = 7;
  9. Card.TEN    = 8;
  10. Card.JACK   = 9;
  11. Card.QUEEN  = 10;
  12. Card.KING   = 11;
  13. Card.ACE    = 12;
  1. Card.CLUBS      = 0;
  2. Card.DIAMONDS   = 1;
  3. Card.HEARTS     = 2;
  4. Card.SPADES     = 3;
  1. Card.rank2s = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];
  1. Card.suit2s = ['c', 'd', 'h', 's'];
  1. import { sendmsg } from 'So-o';
  2.  
  3. import 'Card';
  4.  
  5. let card_2c = sendmsg(Card, 'new', Card.TWO, Card.CLUBS);
  6. let card_Td = sendmsg(Card, 'new', Card.TEN, Card.DIAMONDS);
  7. let card_Kh = sendmsg(Card, 'new', Card.KING, Card.HEARTS);
  8. let card_As = sendmsg(Card, 'new', Card.ACE, Card.SPADES);
  9.  
  10. console.log('2c -> ' + sendmsg(card_2c, 'toString') + ' (two of clubs)');
  11. console.log('Td -> ' + sendmsg(card_Td, 'toString') + ' (ten of diamonds)');
  12. console.log('Kh -> ' + sendmsg(card_Kh, 'toString') + ' (king of hearts)');
  13. console.log('As -> ' + sendmsg(card_As, 'toString') + ' (ace of spades)');
  14.  
  15. console.log('-1 -> ' + sendmsg(card_Kh, 'compare', card_As));
  16. console.log('0 -> ' + sendmsg(card_2c, 'compare', card_2c));
  17. console.log('1 -> ' + sendmsg(card_Kh, 'compare', card_Td));
$ ln poker/testCard.js testCard.mjs
$ nodejs --experimental-modules testCard
2c -> 2c (two of clubs)
Td -> Td (ten of diamonds)
Kh -> Kh (king of hearts)
As -> As (ace of spades)
-1 -> -1
0 -> 0
1 -> 1
SEE ALSO

The JavaScript manual, A hand

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