写一个纸牌比大小的python程序,谁大谁就获胜
时间: 2023-05-28 09:02:26 浏览: 97
以下是一个简单的纸牌大小比较的 Python 程序:
```python
import random
# 创建一副扑克牌
def create_deck():
suits = ['hearts', 'diamonds', 'clubs', 'spades']
ranks = {'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7,
'eight':8, 'nine':9, 'ten':10, 'jack':11, 'queen':12, 'king':13, 'ace':14}
deck = []
for suit in suits:
for rank, value in ranks.items():
deck.append({'suit': suit, 'rank': rank, 'value': value})
random.shuffle(deck)
return deck
# 通过值来比较两张牌
def compare_cards(card1, card2):
if card1['value'] > card2['value']:
return 'player1'
elif card1['value'] < card2['value']:
return 'player2'
else:
return 'tie'
# 玩家抽一张牌
def draw_card(deck):
return deck.pop()
# 游戏主循环
def main():
deck = create_deck()
player1_card = draw_card(deck)
player2_card = draw_card(deck)
print(f"Player 1 draws {player1_card['rank']} of {player1_card['suit']}")
print(f"Player 2 draws {player2_card['rank']} of {player2_card['suit']}")
winner = compare_cards(player1_card, player2_card)
if winner == 'player1':
print("Player 1 wins!")
elif winner == 'player2':
print("Player 2 wins!")
else:
print("It's a tie!")
# 运行游戏
main()
```
运行结果示例:
```
Player 1 draws five of diamonds
Player 2 draws jack of hearts
Player 2 wins!
```
阅读全文