2
688

A game of poker

Download the code of the game of poker in the folder nodejs:

  1. nodejs
    1. node_modules
    2. So-o.js
    3. OL.js
    4. Root.js
    5. ...
    6. Makefile
    7. ...
    8. poker
      1. poker.js
      2. Card.js
      3. Hand.js
      4. Deck.js
      5. ...

Create the folder nodejs with the subfolder node_modules:

$ mkdir -p ~/nodejs/node_modules

Extract the ZIP files in the folder nodejs:

$ unzip so-o-js.zip poker-js.zip

Link all the code files for So-o and the poker game in node_modules while replacing the extension .js by .mjs:

$ make setup
mkdir node_modules
for f in So-o.js Root.js OL.js Once.js Application.js Responder.js; do \
	ln -f $f node_modules/`basename $f .js`.mjs; \
done
if [ -d poker ]; then \
	ln -f poker/poker.js `basename poker.js .js`.mjs; \
	for f in Deck.js Hand.js Card.js; do \
		ln -f poker/$f node_modules/`basename $f .js`.mjs; \
	done \
fi

Run the program poker.mjs:

$ nodejs --experimental-modules poker
3h,4h,4s,Jd,6c -> ONEPAIR
Keep (1-5...)? 23

You have one pair. The program asks which cards you wish to keep. Enter 23 to keep the second and the third card and draw 3 new cards. The program displays the result of the second draw:

Js,4h,4s,Ks,4c -> THREEOFKIND
Play or (q)uit? 

You have a three of a kind. Press Enter to play again:

3c,Ah,2s,7d,3d -> ONEPAIR
Keep (1-5...)? 15
3c,5h,3s,5d,3d -> FULLHOUSE
Play or (q)uit? q

Enter q to quit the program.

CODE
  1. import * as os from 'os';
  2. import * as readline from 'readline';

Gives access to the modules os and readline by Node.js.

  1. import { sendmsg } from 'So-o';

Imports the function sendmsg of So-o. The file So-o.js automatically includes the Root class.

  1. import 'Deck';

Imports the Deck class.

  1. (async () => {
  1. })();

The program runs in an asynchronous closure function.

  1.     const deck = sendmsg(sendmsg(Deck, 'new', true), 'shuffle');

Creates a deck of cards and shuffles it.

  1.     const terminal = readline.createInterface({ input: process.stdin, output: process.stdout });

Creates an interface with the terminal.

  1.     const ask = (q) => new Promise((resolve) => terminal.question(`${q}? `, (r) => resolve(r.trim())));

Creates the promise used to display a prompt message to the user and return the answer.

  1.     let play = true;
  2.  
  3.     while (play) {
  1.     }

Plays until the user decides to quit the program.

  1.         let hand = sendmsg(deck, 'hand');
  2.  
  3.         terminal.write(sendmsg(hand, 'toString'));
  4.         terminal.write(os.EOL);

Creates a hand and displays it to the user.

  1.         await ask('Keep (1-5...)').then((s) => {
  2.             let m = s.match(/[1-5]/g);
  3.            
  4.             let keep = m ? m.map((s) => Number.parseInt(s)) : [];
  5.            
  6.             for (let i = 1; i <= 5; i++) {
  7.                 if (keep.indexOf(i) == -1)
  8.                     sendmsg(hand, 'setCard', i, sendmsg(deck, 'deal'));
  9.             }
  10.         });

Asks the user to choose the cards which are to be kept. Waits for the reply. Draws a new card from the deck for each card which is to be replaced.

  1.         terminal.write(sendmsg(hand, 'toString'));
  2.         terminal.write(os.EOL);

Displays the final hand to the user.

  1.         await ask('Play or (q)uit').then((s) => {
  2.             if (s.charAt(0) == 'q' || s.charAt(0) == 'Q')
  3.                 play = false;
  4.         });

Asks the user if the game should continue. Leaves the loop if the answer starts with a q or a Q. Goes for another round if not.

  1.     terminal.close();

Closes the terminal and exits the program.

SEE ALSO

The JavaScript manual, A game card

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