Implementing the Chinese Card Game “Zha Jin Hua” (Three‑Card Poker) with Python
This article explains the rules of the popular Chinese card game Zha Jin Hua, demonstrates how to generate a standard deck, deal cards to multiple players, evaluate hand types, calculate scores with multipliers, run large‑scale simulations for frequency analysis, and provides the complete Python source code for the entire workflow.
Zha Jin Hua (also known as Three‑Card Poker) is a widely played Chinese card game; this tutorial shows how to implement a simplified version using Python.
Game rules : a 52‑card deck (no jokers) is used, each player receives three cards. Hand rankings from highest to lowest are: Straight Flush, Three of a Kind (豹子), Straight, Flush (金花), Pair, High Card. Multipliers are applied to the summed card values for scoring.
1. Prepare the deck
<code>suit = ["黑桃", "红心", "方块", "梅花"]
num = [str(i) for i in range(2, 11)] + ["J", "Q", "K", "A"]
score_map = {}
for s in suit:
count = 2
for n in num:
score_map[f"{s}{n}"] = count
count += 1
</code>2. Create players
<code>players = [f"p{i}" for i in range(1, 6)]
</code>3. Deal cards
<code>def get_pk_lst(pls, pks):
result = []
for p in pls:
pk = sample(pks, 3)
for _pk in pk:
pks.remove(_pk)
result.append({"name": p, "poker": pk})
return result
</code>4. Evaluate hand type and score
<code>def calculate(_score_map, pk_lst):
n_lst = list(map(lambda x: _score_map[x], pk_lst))
same_suit = len(set([pk[:2] for pk in pk_lst])) == 1
continuity = sorted(n_lst) == [i for i in range(min(n_lst), max(n_lst)+1)] or set(n_lst) == {14, 2, 3}
check = len(set(n_lst))
if not same_suit and not continuity and check == 3:
return sum(n_lst), "单张"
if not same_suit and check == 2:
w = [i for i in n_lst if n_lst.count(i) == 2][0]
single = [i for i in n_lst if i != w][0]
return w*2*2 + single, "对子"
if same_suit and not continuity:
return sum(n_lst)*9, "金花"
if continuity and not same_suit:
return sum(n_lst)*81, "顺子"
if check == 1:
return sum(n_lst)*666, "豹子"
if continuity and same_suit:
return sum(n_lst)*999, "同花顺"
</code>5. Compare players and determine the winner
<code>def compare(_score_map, pk_grp):
for p in pk_grp:
p["score"], p["type"] = calculate(_score_map, p["poker"])
best = max(pk_grp, key=lambda x: x["score"])["name"]
return pk_grp, best
</code>6. Run a single game and display results
<code>def show(_score_map, _players):
pokers = list(_score_map.keys())
poker_grp = get_pk_lst(_players, pokers)
return compare(_score_map, poker_grp)
</code>7. Large‑scale simulation
<code>def start_game(_score_map, _players, freq=1):
type_lst = []
for _ in range(freq):
grp, _ = show(_score_map, _players)
type_lst += [t["type"] for t in grp]
c = Counter(type_lst)
total = sum(c.values())
for hand, cnt in c.items():
print(f"{hand} frequency: {cnt/total:.2%}")
</code>Running start_game(score_map, players, freq=100000) yields frequencies that closely match the theoretical probabilities of each hand type.
8. Full source code (including imports and the if __name__ == '__main__' block) is provided at the end of the article for readers to copy and experiment with.
Finally, the article includes QR‑code links for a free Python public‑course and additional learning resources.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.