Giter Site home page Giter Site logo

hskpeter / hk-mahjong-hand-calculation Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 410 KB

hk-mahjong is the Node.js library that implements the scoring rules of Hong Kong Mahjong.

Home Page: https://www.npmjs.com/package/hk-mahjong

License: MIT License

TypeScript 100.00%
typescript javascript nodejs node-module npm-package hong-kong mahjong

hk-mahjong-hand-calculation's Introduction

๐Ÿ€„ hk-mahjong

npm version GitHub license

hk-mahjong is the Node.js library that implements the scoring rules of Hong Kong Mahjong.

Example

Hong Kong Mahjong Calculator is built using this library.

Installation

Installation could be done using the npm install command:

npm install hk-mahjong

Terminology

Tile

Tile has four categories:

  • dot ๐Ÿ€™ ๐Ÿ€š ๐Ÿ€› ๐Ÿ€œ ๐Ÿ€ ๐Ÿ€ž ๐Ÿ€Ÿ ๐Ÿ€  ๐Ÿ€ก
  • bamboo ๐Ÿ€ ๐Ÿ€‘ ๐Ÿ€’ ๐Ÿ€“ ๐Ÿ€” ๐Ÿ€• ๐Ÿ€– ๐Ÿ€— ๐Ÿ€˜
  • character ๐Ÿ€‡ ๐Ÿ€ˆ ๐Ÿ€‰ ๐Ÿ€Š ๐Ÿ€‹ ๐Ÿ€Œ ๐Ÿ€ ๐Ÿ€Ž ๐Ÿ€
  • honor ๐Ÿ€€ ๐Ÿ€ ๐Ÿ€‚ ๐Ÿ€ƒ ๐Ÿ€„ ๐Ÿ€… ๐Ÿ€†

Meld

Meld is a group of tiles, consisting of either

  • Pong (three identical tiles e.g. ๐Ÿ€’๐Ÿ€’๐Ÿ€’)
  • Kong (four identical tiles e.g. ๐Ÿ€›๐Ÿ€›๐Ÿ€›๐Ÿ€›)
  • Chow (three suited tiles all of the same suit, in numerical sequence e.g. ๐Ÿ€™๐Ÿ€š๐Ÿ€›), or
  • Eyes (two identical tiles needed in a winning hand e.g. ๐Ÿ€‘๐Ÿ€‘)

Hand

  • Hand refers to the group of tiles that are in a player's hand.
  • During the game, each player's hand should always be 13 tiles.

Winning Hand

  • A Winning Hand should have 14-18 tiles that could form 5 Melds, including exactly 1 Eyes.

Faan

  • Faan is a value that weights the significance of a Winning Hand.
  • The Faan value would be used to calculate the money (or "points") the losers have to pay the winner.

Usage

Create a Tile

A Tile could be created by either passing in

  1. the tile's configuration, or
  2. the unicode string of the tile's symbo (click here to learn more about unicode of mahjong tiles)
const { Tile } = require("hk-mahjong");

const dot1 = new Tile({ suit: "dot", value: 1 });
const dot2 = new Tile("๐Ÿ€š");

// Printing the tile's symbol by calling the toString() method.
console.log(dot1.toString()); //๐Ÿ€™

The configurations of tiles are tabulated below.

Tile suit value
๐Ÿ€€ honor 1
๐Ÿ€ honor 2
๐Ÿ€‚ honor 3
๐Ÿ€ƒ honor 4
๐Ÿ€„ honor 5
๐Ÿ€… honor 6
๐Ÿ€† honor 7
๐Ÿ€™ dot 1
๐Ÿ€š dot 2
๐Ÿ€› dot 3
๐Ÿ€œ dot 4
๐Ÿ€ dot 5
๐Ÿ€ž dot 6
๐Ÿ€Ÿ dot 7
๐Ÿ€  dot 8
๐Ÿ€ก dot 9
๐Ÿ€ bamboo 1
๐Ÿ€‘ bamboo 2
๐Ÿ€’ bamboo 3
๐Ÿ€“ bamboo 4
๐Ÿ€” bamboo 5
๐Ÿ€• bamboo 6
๐Ÿ€– bamboo 7
๐Ÿ€— bamboo 8
๐Ÿ€˜ bamboo 9
๐Ÿ€‡ character 1
๐Ÿ€ˆ character 2
๐Ÿ€‰ character 3
๐Ÿ€Š character 4
๐Ÿ€‹ character 5
๐Ÿ€Œ character 6
๐Ÿ€ character 7
๐Ÿ€Ž character 8
๐Ÿ€ character 9

Create a Meld

A Meld could be created by passing in an array of Tiles.

const { Tile, Meld } = require("hk-mahjong");

const dot1 = new Tile({ suit: "dot", value: 1 })
const meld = new Meld([dot1, dot1, dot1]);

// Printing the tile's symbol of the Meld by calling the toString() method.
console.log(meld.toString()); //๐Ÿ€™๐Ÿ€™๐Ÿ€™

Create a Hand

A Hand could be created by passing in the configuration.

const { Tile, Meld, Hand } = require("hk-mahjong");

const dot1 = new Tile('๐Ÿ€™');
const meld = new Meld([dot1, dot1, dot1]);

const dot2 = new Tile('๐Ÿ€š');
const dot3 = new Tile('๐Ÿ€›');
const dot4 = new Tile('๐Ÿ€œ');
const dot9 = new Tile('๐Ÿ€ก');

const tiles = [
   dot2, dot2, dot2,
   dot3, dot3, dot3,
   dot4, dot4, dot4,
   dot9, dot9 
];

const hand = new Hand({ tiles, melds: [meld] });

// Printing the tile's symbol in the Hand by calling the toString() method.
console.log(hand.toString()); //๐Ÿ€™๐Ÿ€™๐Ÿ€™๐Ÿ€š๐Ÿ€š๐Ÿ€š๐Ÿ€›๐Ÿ€›๐Ÿ€›๐Ÿ€œ๐Ÿ€œ๐Ÿ€œ๐Ÿ€ก๐Ÿ€ก

Explore all winning possibilities of a Hand

  • The class ExplorerOfWinningPermutations could be used to check if a Hand is able to form a Winning Hand.
  • There are cases where a Hand have more than one possible permutations to form a Winning Hand. As such, the class ExplorerOfWinningPermutations uses the Breadth First Search algorithm to find all possible winning permutations of a Hand.
const { Tile, Meld, Hand, ExplorerOfWinningPermutations } = require("hk-mahjong");

const dot1 = new Tile('๐Ÿ€™');
const meld = new Meld([dot1, dot1, dot1]);

const dot2 = new Tile('๐Ÿ€š');
const dot3 = new Tile('๐Ÿ€›');
const dot4 = new Tile('๐Ÿ€œ');
const dot9 = new Tile('๐Ÿ€ก');

const tiles = [
   dot2, dot2, dot2,
   dot3, dot3, dot3,
   dot4, dot4, dot4, 
   dot9, dot9
];

const hand = new Hand({ tiles, melds: [meld] });
const explorer = new ExplorerOfWinningPermutations(hand);

// The method getWinningPermutations() would return an array of WinningHand.
const winningPermutations = explorer.getWinningPermutations();

Create a WinningHand

A Winning Hand could be created directly by passing in an array consisting of 5 Melds.

const { Tile, Meld, WinningHand } = require("hk-mahjong");

const tile1 = new Tile({ suit: 'dot', value: 1 });
const tile2 = new Tile({ suit: 'dot', value: 2 });
const tile3 = new Tile({ suit: 'dot', value: 3 });
const tile4 = new Tile({ suit: 'dot', value: 4 });
const tile5 = new Tile({ suit: 'dot', value: 5 });

const meld1 = new Meld([tile1, tile1, tile1]);
const meld2 = new Meld([tile2, tile2, tile2]);
const meld3 = new Meld([tile3, tile3, tile3]);
const meld4 = new Meld([tile4, tile4, tile4]);
const meld5 = new Meld([tile5, tile5]);

const winningHand = new WinningHand([meld1, meld2, meld3, meld4, meld5]);

// Printing the tile's symbol in the WinningHand by calling the toString() method.
console.log(winningHand.toString()); // ๐Ÿ€™๐Ÿ€™๐Ÿ€™ ๐Ÿ€š๐Ÿ€š๐Ÿ€š ๐Ÿ€›๐Ÿ€›๐Ÿ€› ๐Ÿ€œ๐Ÿ€œ๐Ÿ€œ ๐Ÿ€๐Ÿ€

Calculate Faan of a WinningHand

A Faan value could be obtained by calling the static method of the class FaanCalculator (an optional configuration parameter could be passed in if necessary).

const { Tile, Meld, WinningHand, FaanCalculator } = require("hk-mahjong");

const tile1 = new Tile({ suit: 'dot', value: 1 });
const tile2 = new Tile({ suit: 'dot', value: 2 });
const tile3 = new Tile({ suit: 'dot', value: 3 });
const tile4 = new Tile({ suit: 'dot', value: 4 });
const tile5 = new Tile({ suit: 'dot', value: 5 });

const meld1 = new Meld([tile1, tile1, tile1]);
const meld2 = new Meld([tile2, tile2, tile2]);
const meld3 = new Meld([tile3, tile3, tile3]);
const meld4 = new Meld([tile4, tile4, tile4]);
const meld5 = new Meld([tile5, tile5]);

const winningHand = new WinningHand([meld1, meld2, meld3, meld4, meld5]);
const config = {selfPick: true};

const faanValue = FaanCalculator.calculate(winningHand, config);

Contributing

  • Issues and pull requests are welcome.
  • The project is mostly written in TypeScript. Google's TypeScript style guide could be taken as reference.

License

Open sourced under MIT License

Resources

hk-mahjong-hand-calculation's People

Contributors

hskpeter avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

hk-mahjong-hand-calculation's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.