Algorithm for Mahjong Bots


Introduction
The algorithm below describes the level of AI or "intellect" for mahjong bots.
Implemented mahjong bots are not self-learning and always act in strict accordance with the algorithm below.

The selected algorithm for mahjong bots was based on observation and analysis of how experienced mahjong players approach the game. The algorithm below implements the most frequently used approaches that experienced players employ for assembling mahjong combinations.

General Provisions
  • Mahjong bots make decisions only according to the same open information that ordinary players have access to in accordance with the game rules
  • Bots can never see the contents of the wall or the closed hands of any other players
  • Bots are not self-learning and act in strict accordance with an algorithm. Bots do not change their behavior depending on who they are playing against or what the score may be.
  • Algorithm for the bots and their preferred combinations vary depending on the setting of Minimal Points (MP), so that the bot playing the same mahjong variation with MP0 and with MP8 would behave differently.
  • Bots never try to assemble special high-scoring combinations that are difficult to achieve, e.g. in Hong Kong Mahjong bots would never be assembling 7 pairs, Three Great Scholars, Thirteen Orphans, etc.

    Definitions
    1. Visible Tiles
    Visible Tiles are all tiles from open discard (River) and from the open melds of other players (for Riichi versions this includes Dora, Kan-Dora and Ura Kan-Dora tiles).

    2. Dead Tile
    A Dead Tile is a tile in own closed hand that can no longer be a part of any mahjong combination (with the exception of 13 Orphans). A Dead Tile also cannot be a part of a pair.
    For example, if SW (South Wind) is a part of the hand but there are already three SW tiles in Visible Tiles, then SW in the hand will be a Dead Tile.
    If the hand contains B2 (Two of Bamboos) and the open hand has four B1, three B2, four B3, then B2 -- a Dead Tile.

    3. A Problem Tile
    A Problem Tile is a tile that is unlikely to become a part of a combination.
    Example of a Problem Tile:
  • Any Wind or Dragon tiles if only one of these tiles is in the hand while Visible Tiles contain 1 or 2 of these tiles.
  • Any number tile if only one of these tiles is in the hand and if it can only be a part of a pair.

    4. A Single Tile
  • Any Wind or Dragon tile when the closed hand only contains one of these tiles
  • Any number tile when the closed hand does not contain the same tile or tile that differs by 1 or 2 from this tile
    Example: a hand contains: B1 B2 B4 C1 C4 D1 D2 D3 D4 WS WN WN WN
    B4 - NOT a Single Tile
    D1 and D4 - NOT Single Tiles
    C1, C4, WS - Single Tiles

    5. A Minor Tile
  • Any number tile that differs by 1 from an incomplete Chow
    Example:
    B1 B2 B4    C2 C4 C5
    B4, C2 are Minor Tiles and B1 B3 - an incomplete meld so that B1 and B3 are neither single nor minor tiles.

  • Any number tile discarding which does not decrease the maximum number of concealed combinations (Chow, Pong, Kong, Pair)
    Example from the previous hand:
    B1 B2 B4 C1 C4 D1 D2 D3 D4 WS WN WN WN
    B4, D1, D4 - Minor Tiles

    6. Dead Incomplete Chow
    Two consecutive number tiles from the closed hand that cannot be completed as chow.
    For example, if the hand contains B1, B2 but the Visible Tile already has four B3, then the combination of B1, B2 is a Dead Incomplete Chow.

    7. Dead Pair
    A pair that cannot be assembled to complete a Pong.
    For example, if the closed hand contains B2, B2 but Visible Tiles also have B2, B2, then B2, B2 are a Dead Pair.

    8. Incomplete Chow
    Two consecutive number tiles without the third or two number tiles that differ by 2.

    Bot Algorithm for Mahjong Hong Kong MP0

    1. Discard
    Bot decides which tile to discard only after analyzing own hand tiles plus Visible Tiles.
    Bot searches tiles according to Discard Priorities below and selects the 1st tile with the highest priority.
    Discard Priorities:
  • Dead Tiles
  • Problem Tiles
  • Tiles of Dead Incomplete Chows
  • Tiles of Dead Pair in case if the hand contains more than 1 Dead Pair
  • Single Tiles (non-Honor)
  • Single Tiles (Dragons and Winds)
  • Minor Tiles
  • Tiles of Incomplete Chow
  • Tiles of Dead Pairs
  • Tiles of Pairs

    2. Declaration of Mahjong
    Bot always accepts a declaration of Mahjong.

    3. Completion of Kong
    Bot always agrees to add a tile to a Kong.

    4. Pong, Kong, Chow
    Before discarding a tile that can be used in assembling any meld, a bot needs to compare the maximum amount of concealed combinations (only Chow, Pong, Kong) before and after the declaration of a meld.
    A meld is declared only in the following cases:
  • When the maximum amount of concealed combinations before and after the meld declaration remains the same
  • When the maximum number of concealed combinations decreases by 1 - in this case Kong combination is declared

    Bot Algorithm for Mahjong Hong Kong 1 < MP <= 8 (1, 2, 3 Fan)
    In this game variation, the hand needs to have a minimum of 3 Fan, otherwise it is impossible to declare mahjong.
    To exclude the case when the bot assembles 4 melds and is still unable to declare mahjong, from the very start of the game the bot will attempt to assemble one of two relatively simple combinations: Pung hand or One Suit with Honors.
    Every time the bot discards, it needs to determine which of the above two combinations is more likely.
    The likelihood of each of the combinations is determined by the highest number of points in the following way:
    Pung hand
    +5 points for each concealed or open Pong or Kong
    +2 points for each pair

    One Suit with Honors
    15 basis points
  • 2 points for each non-common suit tile
  • 1 points for each Dragon or Wind tile
    If a bot assembles One Suit with Honors, the first item in discard priority becomes:
  • Discarding a tile of incorrect suit If a bot assembles a Pung hand, calculating discard does not involve checking for chow.
    The rest of the algorithm is the same as with MP0.

    Mahjong Bot Algorithm for Hong Kong 8 < MP <= 16 (4, 5, 6 Fan)

    A Bot in this game variation is based on the previous variation.
    If a hand contains 1 guaranteed Fan (Flower or Season tile of own Wind, Pung of Dragons or Pung of own Wind or round Wind), a bot behaves the same as in the previous level.
    Without the guaranteed 1 Fan in hand, the bot in this variation assembles a combination One Suit Only and discards tiles of other suits as well as Wind and Dragon tiles.

    Mahjong Bot Algorithm for Hong Kong 16 < MP (7+ Fan)

    In this game variation, a bot only assembles a combination One Suit Only and aims to assemble its missing tiles with the help of the luck of the draw (Flowers, last draw, self-draw, etc.)

    Mahjong Bot Algotithm for Riichi
    According to Riichi rules, hand must always contain at least 1 Yaku.
    There are few simple combinations that are available with open hand and few others that are only available with concealed hand.
    Therefore, the bot.s main decision is whether to build a concealed hand or an open hand.

    If the bot already has an open meld, it will only pursue open hand combinations.
    If the bot has a Pon, a closed Kan or a live pair of Dragons or own Wind or round Wind, it will aim to build an open hand.
    Otherwise, the bot will aim to build a closed hand.

    A bot will always declare Riichi if there.s an opportunity to declare it.
    In case the bot is building an open hand and only has a pair of Dragons or own Wind or round Wind (and no Yaku), the bot must never discard tiles of that pair but may instead choose to discard tiles from hidden Chows or Pons.
    After all of the above rules are applied, bot acts exactly the same as Hong Kong MP0 bot.

    Mahjong Bot Algorithm for Red Mahjong variation

    In all variations of Red Mahjong, a bot must never discard Jokers.
    Otherwise, a bot acts exactly like in Hong Kong variations with very few exceptions.

    If minimal points are greater than 0 but less than 12, bot will try to collect either All Pungs or Flush with Honors combination.
    The rules to determine which combination to collect are the same as in Hong Kong Mahjong.
    If minimal points are greater than 12, a bot will only collect Pure Flush. A bot will collect the suit with most tiles in own hand and will first and foremost discard tiles of all the other suits and Honor tiles.