QQPOKER下载中心

经典案例

扑克牌游戏代码简单

2025-12-27 13:44:05

以下是一个简单的21点扑克牌游戏的Python实现:

扑克牌游戏代码简单

python

QQ天天德州扑克

import random

class Card:

表示一张扑克牌

def __init__(self, suit, value):

self.suit = suit # 花色: ♠, ♥, ♦, ♣

self.value = value # 牌值: A, 2-10, J, Q, K

def __str__(self):

return f"{self.value}{self.suit}

def get_value(self):

获取牌的点数

if self.value in ['J', 'Q', 'K']:

return 10

elif self.value == 'A':

return 11 # 简化为总是11,实际游戏中A可以是1或11

else:

return int(self.value)

class Deck:

表示一副扑克牌

def __init__(self):

self.cards = []

self.build

def build(self):

构建一副完整的扑克牌

suits = ['♠', '♥', '♦', '♣']

values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

for suit in suits:

for value in values:

self.cards.append(Card(suit, value))

def shuffle(self):

洗牌

random.shuffle(self.cards)

def deal(self):

发一张牌

return self.cards.pop if self.cards else None

def calculate_hand_value(hand):

计算手牌的总点数

total = 0

aces = 0

for card in hand:

if card.value == 'A':

aces += 1

total += card.get_value

# 处理A的情况,如果总分超过21且有A,将A视为1而不是11

while total > 21 and aces > 0:

total -= 10

aces -= 1

return total

def display_hand(hand, hide_first=False):

显示手牌

if hide_first:

print("[隐藏]", end=" ")

for i, card in enumerate(hand[1:]):

print(str(card), end=" ")

else:

for card in hand:

print(str(card), end=" ")

print

def play_blackjack:

玩21点游戏的主函数

print("=== 欢迎来到21点游戏! ===")

deck = Deck

deck.shuffle

player_hand = []

dealer_hand = []

# 初始发牌

player_hand.append(deck.deal)

dealer_hand.append(deck.deal)

player_hand.append(deck.deal)

dealer_hand.append(deck.deal)

player_busted = False

dealer_busted = False

# 玩家回合

while True:

print(f"\

你的手牌: ", end ", end="")

display_hand(player_hand)

print(f"当前点数: {calculate_hand_value(player_hand)}")

print(f"庄家的手牌: ", end="")

display_hand(dealer_hand, hide_first=True)

if calculate_hand_value(player_hand) > 21:

print("你爆牌了!")

player_busted = True

break

action = input("选择操作: (1)要牌 (2)停牌: ")

if action == '1':

player_hand.append(deck.deal)

print("你要了一张牌...")

elif action == '2':

break

# 庄家回合

if not player_busted:

print("\

  • 庄家回合 ")
  • while calculate_hand_value(dealer_hand)

    dealer_hand.append(deck.deal)

    print("庄家要了一张牌...")

    # 显示最终结果

    print(f"\

    === 游戏结果 ===")

    print(f"你的手牌: ", end="")

    display_hand(player_hand)

    player_total = calculate_hand_value(player_hand)

    print(f"你的点数: {player_total}")

    print(f"庄家的手牌: ", end="")

    display_hand(dealer_hand)

    dealer_total = calculate_hand_value(dealer_hand)

    print(f"庄家的点数: {dealer_total}")

    # 判定胜负

    if player_busted:

    print("你输了! 因为你爆牌了。")

    elif dealer_total > 21:

    print("你赢了! 庄家爆牌了。")

    elif player_total > dealer_total:

    print("你赢了!")

    elif player_total

    print("你输了!")

    else:

    print("平局!")

    # 运行游戏

    if __name__ == "__main__":

    play_blackjack

    如何运行这个游戏

    1. 将上述代码保存为一个Python文件,例如`poker_game.py`

    2. 在命令行中运行:`python poker_game.py`

    游戏规则说明

  • 这是一个简化的21点游戏
  • 玩家和庄家各发两张牌,玩家可以看到自己所有的牌和庄家的一张牌
  • 玩家可以选择"要牌"(增加一张牌)或"停牌"(结束自己的回合)
  • 如果玩家点数超过21,立即输掉游戏
  • 庄家在玩家停牌后继续要牌,直到点数达到17或更高
  • 最后比较双方点数,点数高者获胜
  • 你可以根据需要扩展这个基础代码,添加更多功能如赌注、多玩家支持、更复杂的A牌处理等。

    上一篇

    扑克牌有趣