Python Implementation of the Zha Jin Hua Card Game
This article demonstrates how to create a Python program that simulates the Chinese card game Zha Jin Hua, covering deck preparation, player handling, card dealing, hand evaluation, scoring rules, and statistical analysis of game outcomes, with complete source code and example results.
This guide shows how to build a Python simulation of the popular Chinese card game Zha Jin Hua (also known as "炸金花" or "Three‑Card Poker"). It starts by defining the four suits and card ranks, then creates a score map that assigns a numeric value to each card for easy comparison.
Next, a list of five players (p1‑p5) is generated and a deck of 52 cards (without jokers) is prepared. The get_pk_lst(pls, pks) function deals three random cards to each player without replacement.
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 resultThe core of the game is the calculate(_score_map, pk_lst) function, which converts the three cards of a hand into their numeric scores, checks for same suit, continuity (straight), and duplicate values to determine the hand type (single, pair, flush, straight, three‑of‑a‑kind, straight‑flush). Each hand type receives a different multiplier for its score.
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, "同花顺"
return 0, "未知"After dealing, the compare(_score_map, pk_grp) function iterates over each player's hand, applies calculate to obtain the score and hand type, prints all results, and selects the winner using max on the score field.
def compare(_score_map, pk_grp):
for p in pk_grp:
p["score"], p["type"] = calculate(_score_map, p["poker"])
print("开牌结果------")
for p in pk_grp:
print(p)
best = max(pk_grp, key=lambda x: x["score"])["name"]
print("赢家是------", best)
return pk_grpThe show , start_game , and the if __name__ == '__main__' block tie everything together: they build the deck, create players, run the game many times (e.g., 100 000 iterations), collect hand‑type statistics with Counter , and print frequency percentages for each type.
if __name__ == '__main__':
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
players = [f"p{i}" for i in range(1, 6)]
start_game(score_map, players, freq=100000)Running the script produces sample hands, their evaluated types, the winning player for each round, and a statistical summary showing that single cards appear ~74 % of the time, pairs ~17 %, flushes ~5 %, straights ~3 %, three‑of‑a‑kind ~0.2 %, and straight‑flushes ~0.2 %.
The article concludes with a friendly sign‑off and promotional QR codes for additional Python 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.