banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

来玩诈金花嘛?Python实现的那种

无聊用 bolt.new 搞个诈金花的代码来玩玩,AI 写的代码。经常读 AI 的代码,进步神速

写一个程序,实现发牌、比大小判断输赢。

#### 游戏规则:

一付扑克牌,去掉大小王,每个玩家发 3 张牌,最后比大小,看谁赢。

有以下几种牌:

豹子:三张一样的牌,如 3 张 6.

顺金:又称同花顺,即 3 张同样花色的顺子, 如红桃 5、6、7

顺子:又称拖拉机,花色不同,但是顺子,如红桃 5、方片 6、黑桃 7,组成的顺子

对子:2 张牌一样

单张:单张最大的是 A

这几种牌的大小顺序为, 豹子 > 顺金 > 顺子 > 对子 > 单张

#### 需程序实现的点:

1. 首先生成一付完整的扑克牌

2. 给 5 个玩家随机发牌

3. 统一开牌,比大小,输出赢家是谁

import random

# 定义牌的花色和点数
suits = ['♥', '♦', '♣', '♠']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

# 生成一副完整的扑克牌
deck = [(suit, rank) for suit in suits for rank in ranks]

# 洗牌
random.shuffle(deck)

# 发牌给5个玩家,每人3张牌
players = []
for i in range(5):
    hand = deck[:3]
    players.append(hand)
    deck = deck[3:]

# 定义牌型的大小顺序
def rank_value(rank):
    if rank == 'A':
        return 14
    elif rank == 'K':
        return 13
    elif rank == 'Q':
        return 12
    elif rank == 'J':
        return 11
    else:
        return int(rank)

def is_straight(hand):
    values = sorted([rank_value(rank) for suit, rank in hand])
    return values[0] + 1 == values[1] and values[1] + 1 == values[2]

def is_flush(hand):
    suits = [suit for suit, rank in hand]
    return len(set(suits)) == 1

def is_three_of_a_kind(hand):
    values = [rank_value(rank) for suit, rank in hand]
    return values[0] == values[1] == values[2]

def is_pair(hand):
    values = [rank_value(rank) for suit, rank in hand]
    return values[0] == values[1] or values[1] == values[2] or values[0] == values[2]

def hand_rank(hand):
    if is_three_of_a_kind(hand):
        return (5, max([rank_value(rank) for suit, rank in hand]))
    elif is_straight(hand) and is_flush(hand):
        return (4, max([rank_value(rank) for suit, rank in hand]))
    elif is_straight(hand):
        return (3, max([rank_value(rank) for suit, rank in hand]))
    elif is_pair(hand):
        return (2, max([rank_value(rank) for suit, rank in hand]))
    else:
        return (1, max([rank_value(rank) for suit, rank in hand]))

# 比较牌型大小
def compare_hands(hand1, hand2):
    rank1 = hand_rank(hand1)
    rank2 = hand_rank(hand2)
    if rank1 > rank2:
        return 1
    elif rank1 < rank2:
        return -1
    else:
        return 0

# 输出每个玩家的牌和牌型
for i, hand in enumerate(players):
    print(f"Player {i + 1}: {hand}, Hand Rank: {hand_rank(hand)}")

# 比较所有玩家的牌,找出赢家
winner = players[0]
for hand in players[1:]:
    if compare_hands(hand, winner) > 0:
        winner = hand

# 输出赢家
winner_index = players.index(winner)
print(f"Winner is Player {winner_index + 1} with hand {winner}")
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。