python二人21点黑手杰克游戏
怎么样修改可以做到使用turn的function,实现让两个玩家轮流玩,我写的有点问题,我写的是让两个玩家同时进行
而且我的代码中update_hand_value方程中,无法实现我想要的结果
import random
class Player:
def __init__(self, name):
self.name = name
self.hand = []
self.bjvalue = 0
def __str__(self):
return (self.name+ " has the bjvalue "+self.bjvalue)
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
if rank == 'A':
self.bjValue = 11
elif rank in ['J', 'Q', 'K']:
self.bjValue = 10
else:
self.bjValue = int(rank)
def __str__(self):
return (self.rank+self.suit)
def instructions():
print("You two will play this Black Jack game in turn.")
print("Each of you can get two cards randomly and you can choose request or hold a card.")
print("The winner will be the person whose bjvalue is the closet to 21 and less than 21.")
def makeADeckOf52():
cardSuits = ["♣","♦","♥","♠"]
cardRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
deck = []
for s in cardSuits:
for r in cardRanks:
deck.append(s+r)
random.shuffle(deck)
return(deck)
def update_hand_value(player):
# calculate and update the bjvalue
values={'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,\
'9':9,'10':10,'J':10,'Q':10,'K':10,"A":10}
numA=0
for card in player.hand:
player.bjvalue=values]
if card=="A":
numA=numA+1
while player.bjvalue>=21 and numA>0:
player.bjvalue=player.bjvalue-10
numA=numA-1
def dealAHand(deck, player):
# 从牌组中发出两张牌给玩家,并将这两张牌存入玩家的手牌列表
card=deck.pop()
player.hand.append(card)
def displayHand(player):
# display their hands
print(player.name+" has card "+str(player.hand))
def bjHandValue(hand):
# display the total bjvalue
pass
def turn(deck, player):
# including whether to accept another card
update_hand_value(player)
while player.bjvalue<21:
answer=input("Do you want more card? ")
while answer.lower=="y":
card=dealAHand(deck, player)
print(player.name+" has a new card: "+card)
if player.bjvalue>=21:
print(player.name+", you lose! Game over!")
def declare_winner(player1,player2):
# accoring to their total bjvalue, declare the winner
if (21-player1.bjvalue)<(21-player2.bjvalue):
print(player1.name+", you win!")
else:
print(player2.name+", you win!")
def main():
instructions()
deck = makeADeckOf52()
name1 = input("Enter the name of the first player: ")
player1 = Player(name1)
name2 = input("Enter the name of the second player: ")
player2 = Player(name2)
# give the players cards
for i in range(2):
dealAHand(deck, player1)
dealAHand(deck, player2)
# show the players' hands
displayHand(player1)
displayHand(player2)
# 玩家进行操作,直到不再要牌或手牌总黑杰克值超过21点
turn(deck, player1)
turn(deck, player2)
bjHandValue(player1)
bjHandValue(player2)
# declare the winner
while player1.bjvalue<21 and player2.bjvalue<21:
declare_winner(player1,player2)
main() 要实现让两个玩家轮流玩的功能,你可以使用一个循环来交替调用每个玩家的回合函数。在每个玩家的回合函数中,你可以询问玩家是否要继续请求或停留,并根据玩家的选择更新手牌和bjvalue。
首先,在Player类中添加一个方法`turn(self, deck)`来表示玩家的回合:
class Player:
...
def turn(self, deck):
while True:
decision = input(f"{self.name}, do you want to hit or stand? (h/s): ")
if decision.lower() == 'h':
self.hit(deck)
self.update_hand_value()
if self.bjvalue > 21:
print(f"{self.name}, you bust! Game over!")
break
elif decision.lower() == 's':
print(f"{self.name} chooses to stand.")
break
else:
print("Invalid input. Please enter 'h' or 's'.")
在该方法中,我们使用一个无限循环来询问玩家是否要继续请求或停留。如果玩家选择继续请求(输入'h'),我们调用`hit()`方法发一张牌给玩家,然后更新手牌值并检查是否爆牌(bjvalue>21)。如果玩家选择停留(输入's'),我们打印一条消息并结束循环。对于其他无效的输入,我们会打印错误消息并再次询问。
接下来,在主程序中,我们使用一个循环来交替调用每个玩家的回合函数,直到有人爆牌或选择停留:
def play_game():
# 创建两个玩家对象和一副牌
player1 = Player("Player 1")
player2 = Player("Player 2")
deck = makeADeckOf52()
# 开始游戏
while True:
# 玩家1的回合
print("\n" + "=" * 20)
player1.displayHand()
player1.turn(deck)
if player1.bjvalue > 21:
break
# 玩家2的回合
print("\n" + "=" * 20)
player2.displayHand()
player2.turn(deck)
if player2.bjvalue > 21:
break
# 判断结果
if player1.bjvalue <= 21 and player2.bjvalue <= 21:
if 21 - player1.bjvalue < 21 - player2.bjvalue:
print("Player 1 wins!")
elif 21 - player1.bjvalue > 21 - player2.bjvalue:
print("Player 2 wins!")
else:
print("It's a tie!")
elif player1.bjvalue <= 21:
print("Player 1 wins!")
elif player2.bjvalue <= 21:
print("Player 2 wins!")
play_game()
在主程序中,我们先创建了两个玩家对象和一副牌。然后我们使用一个无限循环来交替调用每个玩家的回合函数,直到有人爆牌或选择停留。最后,我们根据双方的bjvalue来判断结果并打印胜利者。
关于`update_hand_value()`函数的问题,你可以按照以下方式修改它:
def update_hand_value(self):
values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, "A": 10}
numA = 0
self.bjvalue = 0# 初始化bjvalue为0
for card in self.hand:
self.bjvalue += values# 累加每张牌的bjValue
if card.rank == "A":
numA += 1
while self.bjvalue > 21 and numA > 0:
self.bjvalue -= 10
numA -= 1
在该方法中,我们首先将bjvalue初始化为0,然后累加每张牌的bjValue。这样就能正确计算出bjvalue了。
希望能够帮助到你!祝你好运!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]