Build a Python Card Game: From Deck Generation to Winner Ranking
This article walks through implementing the Chinese poker game "ZhaJinHua" in Python, covering the game rules, generating a full deck, dealing cards to five players, evaluating hand types, scoring, and determining the winner, and also introduces a comprehensive book on financial large‑model development.
Introduction
The author, a Python enthusiast, presents a programming challenge inspired by the popular Chinese card game "ZhaJinHua" (also known as Three‑Card Brag). The task is to write a Python program that creates a deck, deals three cards to each of five players, compares the hands, and outputs the winner.
Game Rules
The game uses a standard 52‑card deck without jokers. Hand types and their hierarchy are:
Leopard (豹子) : three identical cards.
Straight Flush (顺金) : three consecutive cards of the same suit.
Straight (顺子) : three consecutive cards of different suits.
Pair (对子) : two identical cards.
High Card (单张) : the highest single card, with Ace being the highest.
The ranking order is Leopard > Straight Flush > Straight > Pair > High Card.
Solution Code
# -*- coding: utf-8 -*-
import random
puke = [] # store the deck
num_list = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
hua_list = ['梅花','红桃','黑桃','方块']
sort_dic = {'2':0,'3':1,'4':2,'5':3,'6':4,'7':5,'8':6,'9':7,'10':8,'J':9,'Q':10,'K':11,'A':12,
'对子':15,'顺子':30,'顺金':60,'豹子':100}
count_new_list = []
count_dic = {}
# build the deck
for hua in hua_list:
for num in num_list:
puke.append(hua + num)
player_dic = {'玩家1':[],'玩家2':[],'玩家3':[],'玩家4':[],'玩家5':[]}
# deal cards randomly
for key in player_dic:
plate = random.sample(puke,3)
player_dic[key] = plate
for i in plate:
puke.remove(i)
print(player_dic)
def paixing(list1):
num = []
huase = []
for i in list1:
a = i[2:]
b = i[:2]
num.append(a)
huase.append(b)
return num, huase
def sort(num):
new_num = [sort_dic[i] for i in num]
new_num.sort()
sort_list2 = []
for new in new_num:
sort_list2.append([k for k,v in sort_dic.items() if v==new])
list1 = []
for m in sort_list2:
for n in m:
list1.append(n)
return list1
def count(num, huase):
a = 0
base_count = sum(sort_dic[n] for n in num)
if num[0]==num[1]==num[2]:
paixing='豹子'
a = base_count + sort_dic[paixing]
elif (sort_dic[num[0]]+1==sort_dic[num[1]] and sort_dic[num[2]]-1==sort_dic[num[1]]) and (huase[0]==huase[1]==huase[2]):
paixing='顺金'
a = base_count + sort_dic[paixing]
elif (sort_dic[num[0]]+1==sort_dic[num[1]] and sort_dic[num[2]]-1==sort_dic[num[1]]) and (huase[0]!=huase[1] or huase[1]!=huase[2]):
paixing='顺子'
a = base_count + sort_dic[paixing]
elif (num[0]==num[1]!=num[2]) or (num[1]==num[2]!=num[0]) or (num[0]==num[2]!=num[1]):
paixing='对子'
a = base_count + sort_dic[paixing]
else:
a = base_count
return a
def compare(count_dic):
d = list(zip(count_dic.values(), count_dic.keys()))
return sorted(d, reverse=True)
for key, value in player_dic.items():
num, huase = paixing(value)
num = sort(num)
score = count(num, huase)
count_dic[key] = score
print(key + "的牌为:" + str(value))
count_new_list = compare(count_dic)
print('最终排名:\t' + '\t'.join([f"{item[1]}{i+1}名" for i,item in enumerate(count_new_list)]))Improved Version
# -*- coding: utf-8 -*-
import random
puke = []
num_list = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
hua_list = ['梅花','红桃','黑桃','方块']
sort_dic = {'2':0,'3':1,'4':2,'5':3,'6':4,'7':5,'8':6,'9':7,'10':8,'J':9,'Q':10,'K':11,'A':12,
'对子':15,'顺子':30,'顺金':60,'豹子':100}
count_new_list = []
count_dic = {}
for hua in hua_list:
for num in num_list:
puke.append(hua + num)
player_dic = {'玩家1':[],'玩家2':[],'玩家3':[],'玩家4':[],'玩家5':[]}
print(len(puke))
for key in player_dic:
plate = random.sample(puke,3)
player_dic[key] = plate
for i in plate:
puke.remove(i)
print(player_dic)
def paixing(list1):
num = []
huase = []
for data in list1:
huase_type = data[:2]
pai_number = data[2:]
num.append(pai_number)
huase.append(huase_type)
return num, huase
def get_score(num, huase):
base_count = sum(sort_dic[n] for n in num)
if num[0]==num[1]==num[2]:
paixing='豹子'
score = base_count + sort_dic[paixing]
elif (sort_dic[num[0]]+1==sort_dic[num[1]] and sort_dic[num[2]]-1==sort_dic[num[1]]) and (huase[0]==huase[1]==huase[2]):
paixing='顺金'
score = base_count + sort_dic[paixing]
elif (sort_dic[num[0]]+1==sort_dic[num[1]] and sort_dic[num[2]]-1==sort_dic[num[1]]) and (huase[0]!=huase[1] or huase[1]!=huase[2]):
paixing='顺子'
score = base_count + sort_dic[paixing]
elif (num[0]==num[1]!=num[2]) or (num[1]==num[2]!=num[0]) or (num[0]==num[2]!=num[1]):
paixing='对子'
score = base_count + sort_dic[paixing]
else:
score = base_count
return score
if __name__ == '__main__':
for key, value in player_dic.items():
num, huase = paixing(value)
num = sorted(num)
score = get_score(num, huase)
count_dic[key] = score
print(key + "的牌为:" + str(value))
count_new_list = sorted(zip(count_dic.values(), count_dic.keys()), reverse=True)
print('最终排名:')
for i in range(len(count_new_list)):
print(count_new_list[i][1] + '\t', end='')Result
Financial Large‑Model Development Book
The article also promotes the book "Financial Large‑Model Development Basics and Practice", which covers the full workflow of building large models for finance, including time‑series analysis, risk modeling, high‑frequency trading, credit risk assessment, asset pricing, and explores emerging topics such as blockchain and AI‑driven fintech.
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 Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
