Implementing a Zha Jin Hua (Three‑Card Poker) Game in Python
This article explains how to use Python to build a Zha Jin Hua card‑game simulator, covering the game rules, hand‑type definitions, scoring logic, player management, statistical analysis of 100 000 rounds, and provides the complete source code with detailed comments.
Using Python, we implement a simplified version of the popular Chinese card game Zha Jin Hua (also known as "Three‑Card Poker"). The article first introduces the basic hand rankings—straight flush, three of a kind, straight, flush, pair, and high card—along with their probability order.
We then walk through the implementation steps:
Prepare the deck : generate four suits (Spades, Hearts, Diamonds, Clubs) and ranks 2‑10, J, Q, K, A, and assign each card a numeric score.
Player entry : create a list of player identifiers (p1‑p5).
Deal cards : randomly draw three cards for each player without replacement.
Determine hand type and calculate score : map each card to its score, check for same suit, continuity, and duplicate values, then apply the corresponding multiplier (e.g., 9× for flush, 81× for straight, 666× for three of a kind, 999× for straight flush).
Decide the winner : use the max function on the calculated scores to find the player with the highest hand.
The core functions are shown below. Comments have been translated to English for clarity.
<code># @Seon
# Zha Jin Hua implementation
from random import sample
from collections import Counter
def get_pk_lst(pls, pks):
"""Deal three cards to each player without replacement."""
result = []
for p in pls:
pk = sample(pks, 3)
for _pk in pk:
pks.remove(_pk)
result.append({"name": p, "poker": pk})
return result
def calculate(_score_map, pk_lst):
"""Return the hand score and type for a given three‑card list."""
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), "High Card"
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, "Pair"
if same_suit and not continuity:
return sum(n_lst)*9, "Flush"
if continuity and not same_suit:
return sum(n_lst)*81, "Straight"
if check == 1:
return sum(n_lst)*666, "Three of a Kind"
if continuity and same_suit:
return sum(n_lst)*999, "Straight Flush"
def compare(_score_map, pk_grp):
"""Calculate scores for all players and print results."""
for p in pk_grp:
p["score"], p["type"] = calculate(_score_map, p["poker"])
print("Deal results------")
for p in pk_grp:
print(p)
print("Winner is------")
best = max(pk_grp, key=lambda x: x["score"])["name"]
print(best)
return pk_grp
def show(_score_map, _players):
pokers = list(_score_map.keys())
poker_grp = get_pk_lst(_players, pokers)
return compare(_score_map, poker_grp)
def start_game(_score_map, _players, freq=1):
"""Run the game <code>freq</code> times and collect hand‑type statistics."""
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%}")
return c
if __name__ == "__main__":
# Prepare the deck
suit = ["Spades", "Hearts", "Diamonds", "Clubs"]
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
# Five players
players = [f"p{i}" for i in range(1, 6)]
# Simulate 100,000 games and show hand‑type frequencies
start_game(score_map, players, freq=100000)
</code>Running the simulation for 100 000 rounds yields hand‑type frequencies that closely match theoretical probabilities (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 %). The article also provides example game outputs showing dealt cards, calculated scores, hand types, and the declared winner.
Overall, the tutorial demonstrates how to model card‑game logic, perform combinatorial analysis, and use Python’s standard library for random sampling and counting, making it a useful reference for developers interested in game development and algorithmic simulations.
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.