#定義牌的類別:
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
#定義牌堆的類別:
import random
class Deck:
def __init__(self):
self.deck = []
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
for suit in suits:
for rank in ranks:
card = Card(suit, rank)
self.deck.append(card)
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
return self.deck.pop()
#定義玩家的類別:
class Player:
def __init__(self, name):
self.name = name
self.hand = []
def add_card(self, card):
self.hand.append(card)
def show_hand(self):
print(f"{self.name}'s hand:")
for card in self.hand:
print(f"{card.rank} of {card.suit}")
#在主程式中創建牌堆和玩家,並進行遊戲:
deck = Deck()
deck.shuffle()
player1 = Player("Player 1")
player2 = Player("Player 2")
for i in range(5):
player1.add_card(deck.deal())
player2.add_card(deck.deal())
player1.show_hand()
player2.show_hand()
#這只是一個非常簡單的範例,可以根據你的需求進行擴展。
沒有留言:
張貼留言