6

Mahjong is one of the most popular games in Asia (not the solitaire style found in Windows 7). There were plenty of Mahjong games out there including online ones from Yahoo or offline ones back in the DOS days!

Just another day I was thinking to myself, how can I write one (excluding the GUI)?

The data modeling part is easy.

The winning and scoring rules are... pattern matching?

The strategic part of the game, such as determining which tile to throw out and when to make the Chow or Pong moves are the most difficult part. How to implement this?

Thanks!

Research:

Community
  • 1
  • 1
Henry
  • 32,689
  • 19
  • 120
  • 221
  • It does sound like an interesting project. Have you looked into any open source implementations to see how they did it? http://www.google.com/search?q=open+source+mahjong – David Nov 01 '10 at 23:57
  • I haven't looked into any open source implementations, I don't know if there are any.. Maybe I should start one. – Henry Nov 02 '10 at 00:07
  • Can you use an approach similar to a Sudoku solver? using backtracking, etc. Here are twenty Sudoku solvers (in Haskell) http://www.haskell.org/haskellwiki/Sudoku . The approaches may help you in your search for algorithms for solving other games/puzzles. – Jared Updike Nov 02 '10 at 00:20
  • Maybe I'm not fully understanding the algorithm, but the solution space is very big in a game of MJ and it changes based on what other tiles you have and what tiles have been disposed to the 'wall', so I don't think backtracking would work. – Henry Nov 02 '10 at 00:29

1 Answers1

2

Read this

http://homepage.mac.com/s_lott/books/python/html/p05/p05c05_mahjongg.html

It might help.

"determining which tile to throw out" is actually pretty straight-forward. Given the discards and the nature of the hand, defining a "ready" hand is a matter of determine which potential sets have the most available (non-discarded, not in-play) tiles.

Each tile belongs to one of several possible patterns (pair, up to 3 chows, pung, kong). It's easy to compute the tiles required to fill the pattern. It's easy to reduce the counts of possible tiles based on what's played and what's exposed. The resulting possible patterns can be ranked. Discards are picked from the most-difficult-to-fill combinations.

[See this http://www.amazon.com/Mah-Jong-Handbook-Play-Score/dp/0804838747]

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • @Henry: "Thanks, but I want something more than this". Please **update** your question to define what you mean by "more". – S.Lott Nov 02 '10 at 00:18
  • This link is heavy on the data model, but a little weak on the intelligence/strategic side, which is the most difficult part – Henry Nov 02 '10 at 00:27
  • @Henry: Hmm.. The TileSet Class Hierarchy section seems to cover all the various sets required to complete a hand. The Hand section seems to include the complete Hand Scoring algorithm. I can't think that anything more is required. Perhaps you could be **specific**? – S.Lott Nov 02 '10 at 00:58
  • ok, I read your page again and here are the things I think are missing: 1.) how can an AI player decide which tile to dispose to the wall for better chance of winning? 2.) when to decide whether to 'pong'/'kong' from the wall or 'chow' from the previous player (you call them 'melted' tiles) to improve the possibility of winning? 3.) looking at your first test case, your algorithm decompose the bamboos into [2,2,2][3,4,5][5,5], but some hands might alternatives like [2,2][2,3,4][5,5,5]. 4.) you identified the TileSet, but what are the strategies to work towards those TitleSet? – Henry Nov 02 '10 at 01:22
  • I'm not sure what scoring system you're using, but the MJ variant I have in mind, do not share the same scoring system as yours. The variant I'm planning to write will mostly be based on 'fan', and there are rules on how to accumulate 'fan', so the AI I'm planning to build should take account of how to archive the maximum fan with high possibility of winning before the game runs out of tiles. – Henry Nov 02 '10 at 01:29
  • Thanks for the updated answer, much appreciated. So did you finish implementing your MJ game? in Python? – Henry Nov 02 '10 at 02:08
  • The first link on this answer is not accessible in any browser that supports http 1.1 – Jeremy List Apr 20 '13 at 13:36
  • The link is dead (http://www.downforeveryoneorjustme.com/http://homepage.mac.com/s_lott/books/python/html/p05/p05c05_mahjongg.html) please fix this if you know how to find it again. – Nyamiou The Galeanthrope Aug 28 '14 at 17:39