Python用类做一个简单的扑克牌比大小游戏
时间: 2024-02-03 14:13:48 浏览: 174
好的,下面是一个简单的扑克牌比大小游戏的Python代码:
```python
import random
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return f"{self.rank} of {self.suit}"
class Deck:
def __init__(self):
self.cards = []
for suit in ["Hearts", "Diamonds", "Clubs", "Spades"]:
for rank in range(2, 11):
self.cards.append(Card(suit, str(rank)))
for rank in ["Jack", "Queen", "King", "Ace"]:
self.cards.append(Card(suit, rank))
def __str__(self):
deck_comp = ""
for card in self.cards:
deck_comp += f"\n {card.__str__()}"
return f"The deck has:{deck_comp}"
def shuffle(self):
random.shuffle(self.cards)
def deal(self):
single_card = self.cards.pop()
return single_card
class Hand:
def __init__(self):
self.cards = []
self.value = 0
self.aces = 0
def add_card(self, card):
self.cards.append(card)
if card.rank.isdigit():
self.value += int(card.rank)
elif card.rank in ["Jack", "Queen", "King"]:
self.value += 10
else:
self.aces += 1
self.value += 11
def adjust_for_ace(self):
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1
class Chips:
def __init__(self):
self.total = 100
self.bet = 0
def win_bet(self):
self.total += self.bet
def lose_bet(self):
self.total -= self.bet
def take_bet(chips):
while True:
try:
chips.bet = int(input("How many chips would you like to bet? "))
except:
print("Sorry, please provide an integer.")
else:
if chips.bet > chips.total:
print(f"Sorry, your bet can't exceed {chips.total}")
else:
break
def hit(deck, hand):
hand.add_card(deck.deal())
hand.adjust_for_ace()
def hit_or_stand(deck, hand):
global playing
while True:
x = input("Would you like to Hit or Stand? Enter 'h' or 's': ")
if x[0].lower() == "h":
hit(deck, hand)
elif x[0].lower() == "s":
print("Player stands. Dealer is playing.")
playing = False
else:
print("Sorry, please try again.")
continue
break
def show_some(player, dealer):
print("\nDealer's Hand:")
print("<card hidden>")
print(dealer.cards[1])
print("\nPlayer's Hand:", *player.cards, sep="\n ")
def show_all(player, dealer):
print("\nDealer's Hand:", *dealer.cards, sep="\n ")
print("Dealer's Hand =",dealer.value)
print("\nPlayer's Hand:", *player.cards, sep="\n ")
print("Player's Hand =",player.value)
def player_busts(player, dealer, chips):
print("BUST PLAYER!")
chips.lose_bet()
def player_wins(player, dealer, chips):
print("PLAYER WINS!")
chips.win_bet()
def dealer_busts(player, dealer, chips):
print("PLAYER WINS! DEALER BUSTED!")
chips.win_bet()
def dealer_wins(player, dealer, chips):
print("DEALER WINS!")
chips.lose_bet()
def push(player, dealer):
print("Dealer and player tie! PUSH")
# 游戏逻辑
while True:
print("Welcome to BlackJack! Get as close to 21 as you can without going over!\n\
Dealer hits until she reaches 17. Aces count as 1 or 11.")
deck = Deck()
deck.shuffle()
player_hand = Hand()
player_hand.add_card(deck.deal())
player_hand.add_card(deck.deal())
dealer_hand = Hand()
dealer_hand.add_card(deck.deal())
dealer_hand.add_card(deck.deal())
player_chips = Chips()
take_bet(player_chips)
show_some(player_hand, dealer_hand)
playing = True
while playing:
hit_or_stand(deck, player_hand)
show_some(player_hand, dealer_hand)
if player_hand.value > 21:
player_busts(player_hand, dealer_hand, player_chips)
break
if player_hand.value <= 21:
while dealer_hand.value < 17:
hit(deck, dealer_hand)
show_all(player_hand, dealer_hand)
if dealer_hand.value > 21:
dealer_busts(player_hand, dealer_hand, player_chips)
elif dealer_hand.value > player_hand.value:
dealer_wins(player_hand, dealer_hand, player_chips)
elif dealer_hand.value < player_hand.value:
player_wins(player_hand, dealer_hand, player_chips)
else:
push(player_hand, dealer_hand)
print("\nPlayer's winnings stand at",player_chips.total)
new_game = input("Would you like to play another hand? Enter 'y' or 'n': ")
if new_game[0].lower() == "y":
playing = True
continue
else:
print("Thanks for playing!")
break
```
通过运行以上代码,你可以开始一个简单的扑克牌比大小游戏。在游戏中,你可以和电脑进行对战,看看谁能更接近21点但不超过21点。祝你好运!
阅读全文