麻将胜牌算法
我正在寻找一种算法来确定当前的麻将牌是否获胜。如果您不熟悉该游戏,请看以下基本概念(简化):
- 共有三组牌,每组包含排名 1-9 的牌。还有特殊的瓷砖,有七种。每张牌都有四张,因此每种花色有 36 张牌和 28 张特殊牌。
- 一手牌有 14 块。
- chow是一组连续等级的单一花色的三张牌。
- 乒乓球是一组三块相同的牌。
- 杠是一组四块相同的牌。
- 对是一组两个相同的图块。
- 获胜牌是指牌组成任意数量的 chows、pongs 和/或 kongs 以及一对。
手牌按花色排序,然后按等级排序。我的想法是这样的:
- 将所有瓷砖标记为未访问和未获胜。
- 访问第一个未访问过的图块。
- 检查后续的牌,直到遇到 chow、pong 或 kong,或者直到不可能出现。如果组合完成,则将所有参与的图块标记为已访问并获胜。
- 如果所有图块都已被访问,则检查它们是否全部都获胜。如果尚未访问完所有牌,请转到 2。
问题是,一旦牌成为组合的一部分,就不能成为任何其他组合的成员,而这可能会使这手牌成为获胜组合。
对于可行的算法有什么想法吗?
I'm looking for an algorithm that will determine if the current mahjong hand is a winning one. If you are not familiar with the game, here's the basic idea (simplified):
- There are three suits of tiles, each containing tiles ranked 1-9. There are also special tiles, seven types of them. Each tile exists in four copies, so there are 36 tiles of each suit and 28 special tiles.
- A hand has 14 tiles in it.
- A chow is a set of three tiles of a single suit with consecutive ranks.
- A pong is a set of three identical tiles.
- A kong is a set of four identical tiles.
- A pair is a set of two identical tiles.
- A winning hand is one in which the tiles form any number of chows, pongs, and/or kongs and one pair.
The hand is sorted by suit and then by rank. My idea is something like this:
- Mark all tiles as unvisited and nonwinning.
- Visit first unvisited tile.
- Check subsequent tiles until a chow, pong, or a kong is encountered, or until there is no possibility of it. If a combination is completed, mark all participating tiles as visited and winning
- If all tiles have been visited, check if all of them are winning. If not all tiles have been visited, go to 2.
The problem is that once a tile is a part of a combination is cannot be a member of any other combination, one that might make the hand a winning one.
Any ideas for a working algorithm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将其嵌入到回溯算法中,您的算法就很好了(http://en.wikipedia.org/wiki /回溯)。最简单的方法是在找到匹配的对/chow/...后递归调用您的方法,但如果没有,则丢弃当前选择。在伪代码中,这看起来像这样:
背后的想法是,您首先尝试将每一对/...作为获胜手牌的一部分,但如果这不起作用,则假设它不是获胜手牌的一部分,然后尝试相同的操作。
如果您不仅感兴趣是否是获胜手牌,而且还感兴趣获胜对/chows/...的所有可能组合,请跳过步骤 3.3 中的返回。
Your algorithm is just fine if you embedded it into a backtracking algorithm (http://en.wikipedia.org/wiki/Backtracking). The easiest way to do it is to call your method recursively once you found a matching pair/chow/... but discard current choice it if not. In pseudo code this looks something like this:
The idea behind is that you first try each pair/... as part of a winning hand but if this does not work just try the same by assuming it is not part of the winning hand.
If you are not only interested if it is a winning hand but all possible combinations of winning pairs/chows/... skip the return in step 3.3.