Python Implementation of a Simplified ZhaJinHua Card Game with Scoring and Statistics
This article explains how to build a Python version of the popular Chinese card game ZhaJinHua, covering deck preparation, player enrollment, card dealing, hand‑type detection, scoring rules, winner determination, and statistical analysis of thousands of simulated rounds.
The article introduces the classic Chinese card game ZhaJinHua (also known as "Three‑Card Poker") and demonstrates how to implement a simplified version using Python.
Game Rules : A standard 52‑card deck (without jokers) is used; each player receives three cards. Hand types are ranked from highest to lowest as follows: Straight Flush, Three of a Kind ("豹子"), Straight, Flush, Pair, and High Card. The article notes that the implemented rules differ slightly from the traditional game.
1. Deck Preparation
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 += 12. Player Enrollment
players = [f"p{i}" for i in range(1, 6)]3. Dealing Cards
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
pokers = list(score_map.keys())
poker_grp = get_pk_lst(players, pokers)4. Hand Evaluation and Scoring
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, "同花顺"5. Determining the Winner
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)
print("赢家是------")
best = max(pk_grp, key=lambda x: x["score"])["name"]
print(best)
return pk_grp6. Running a Single Game
def show(_score_map, _players):
pokers = list(_score_map.keys())
poker_grp = get_pk_lst(_players, pokers)
return compare(_score_map, poker_grp)7. Batch Simulation and Statistics
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 item, cnt in c.items():
print(f"{item}频率:{cnt/total:.2%}")8. Main Execution
if __name__ == "__main__":
# deck preparation (as above)
players = [f"p{i}" for i in range(1, 6)]
start_game(score_map, players, freq=100000)The script runs 100,000 simulated games, prints the frequency of each hand type, and demonstrates that the empirical probabilities closely match theoretical values (e.g., high‑card ~74.37%, pair ~16.95%, flush ~4.97%, straight ~3.25%, three‑of‑a‑kind ~0.24%, straight‑flush ~0.22%).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
