A game of poker
Download the code of the game of poker in the folder nodejs:
- nodejs
- node_modules
- So-o.js
- OL.js
- Root.js
- ...
- Makefile
- ...
- poker
- poker.js
- Card.js
- Hand.js
- Deck.js
- ...
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
- import * as os from 'os';
- import * as readline from 'readline';
Gives access to the modules os and readline by Node.js.
- import { sendmsg } from 'So-o';
Imports the function sendmsg
of So-o. The file So-o.js automatically includes the Root class.
- import 'Deck';
Imports the Deck class.
- (async () => {
- })();
The program runs in an asynchronous closure function.
- const deck = sendmsg(sendmsg(Deck, 'new', true), 'shuffle');
Creates a deck of cards and shuffles it.
- const terminal = readline.createInterface({ input: process.stdin, output: process.stdout });
Creates an interface with the terminal.
- 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.
- let play = true;
- while (play) {
- }
Plays until the user decides to quit the program.
- let hand = sendmsg(deck, 'hand');
- terminal.write(sendmsg(hand, 'toString'));
- terminal.write(os.EOL);
Creates a hand and displays it to the user.
- await ask('Keep (1-5...)').then((s) => {
- let m = s.match(/[1-5]/g);
- let keep = m ? m.map((s) => Number.parseInt(s)) : [];
- for (let i = 1; i <= 5; i++) {
- if (keep.indexOf(i) == -1)
- sendmsg(hand, 'setCard', i, sendmsg(deck, 'deal'));
- }
- });
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.
- terminal.write(sendmsg(hand, 'toString'));
- terminal.write(os.EOL);
Displays the final hand to the user.
- await ask('Play or (q)uit').then((s) => {
- if (s.charAt(0) == 'q' || s.charAt(0) == 'Q')
- play = false;
- });
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.
- terminal.close();
Closes the terminal and exits the program.
Comments