python 21点
怎么样可以做到最后比较两个玩家的点数?import random
class Card:
def __init__(self,r,s):
self.rank=r
self.suit=s
def __str__(self):
return(self.rank+self.suit)
class Player:
def __init__(self, name):
self.name = name
self.hand = []
self.bjhandvalue = 0
def __str__(self):
return (self.name+ " has the bjvalue "+self.bjhandvalue)
def main():
instructions()
deck=makeADeckOf52()
for i in range(2):
name = input("Enter the name of the player: ")
player = Player(name)
player.bjhandvalue=0
player.bjhandvalue=turn(deck,player)
displayHand(player)
while player.bjhandvalue<21:
answer=input("Do you want to request a card?")
if answer.lower=="y":
card=dealAHand(deck,player)
cardbjvalue=create_BJValuecard_LookUp(card)
player.bjhandvalue=player.bjhandvalue+cardbjvalue
if player.bjhandvalue>=21:
numA=0
for card in player.hand:
if card.rank =="A":
numA=numA+1
while player.bjhandvalue>=21 and numA>0:
player.bjhandvalue=player.bjhandvalue-10
numA=numA-1
if player.bjvalue>=21:
print(player.name+", you lose! Game over!")
elifplayer.bjhandvalue<21:
numA=0
for card in player.hand:
if card.rank =="A":
numA=numA+1
while player.bjhandvalue<21 and numA>0:
player.bjhandvalue=player.bjhandvalue+10
numA=numA-1
def makeADeckOf52():
#input: none
#output: a list of 52 strings
#process: pair the 4 suits with the 13 card rank values
#modeling the 52 cards:
#A-K of clubs,diamonds, hearts, spades
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(Card(r,s))
random.shuffle(deck)
return(deck)
def turn(deck,player):
player.bjhandvalue=0
for i in range(2):
card=dealAHand(deck, player)
player.hand.append(card)
player.bjhandvalue=update_hand_value(player)
return player.bjhandvalue
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 dealAHand(deck, player):
card=deck.pop()
return card
def create_BJValuecard_LookUp(cardStr):
#assume card is in this format: A♣ , 10♥
#"♣","♦","♥","♠"
rank = cardStr[:len(cardStr)-1]
print(rank)
specialValues = {"A":1, "J":10,"Q":10,"K":10}
if rank in specialValues:
bjValue = specialValues
else:
bjValue = int(rank)
return(bjValue)
def displayHand(player):
print(player.name+"' hand card is "+player.hand)
def update_hand_value(player):
for card in player.hand:
cardbjvalue=create_BJValuecard_LookUp(card)
player.bjhandvalue=player.bjhandvalue+cardbjvalue
return player.bjhandvalue 要比较两个玩家的点数,可以在游戏结束后,比较两个玩家的bjhandvalue属性大小即可。
首先,在Player类中添加一个方法用于获取玩家的点数:
def get_bjhandvalue(self):
return self.bjhandvalue
然后,在main函数中,创建两个Player对象,并在游戏结束后比较它们的点数:
def main():
instructions()
deck = makeADeckOf52()
players = []
for i in range(2):
name = input("Enter the name of the player: ")
player = Player(name)
player.bjhandvalue = 0
player.bjhandvalue = turn(deck, player)
displayHand(player)
while player.bjhandvalue == 21:
# 游戏逻辑
...
players.append(player)
# 比较两个玩家的点数
if players.get_bjhandvalue() > players.get_bjhandvalue():
print(players.name + " wins!")
elif players.get_bjhandvalue() < players.get_bjhandvalue():
print(players.name + " wins!")
else:
print("It's a draw!")
if __name__ == '__main__':
main()
这样就可以在游戏结束后比较两个玩家的点数了。记得在比较之前,确保两个玩家都已经完成了他们的回合,并计算出了最终的点数。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2023-12-11 09:57
要比较两个玩家的点数,可以在游戏结束后,比较两个玩家的bjhandvalue属性大小即可。
首先,在Player类中 ...
import random
class Card:
def __init__(self,r,s):
self.rank=r
self.suit=s
def __str__(self):
return(self.rank+self.suit)
class Player:
def __init__(self, name):
self.name = name
self.hand = []
self.bjhandvalue = 0
def __str__(self):
return (self.name+ " has the bjvalue "+self.bjhandvalue)
def main():
instructions()
deck=makeADeckOf52()
players=[]
for i in range(2):
name = input("Enter the name of the player: ")
player = Player(name)
player.bjhandvalue=0
player.bjhandvalue=turn(deck,player)
displayHand(player)
while player.bjhandvalue<21:
answer=input("Do you want to request a card?")
if answer.lower=="y":
card=dealAHand(deck,player)
cardbjvalue=create_BJValuecard_LookUp(card)
player.bjhandvalue=player.bjhandvalue+cardbjvalue
if player.bjhandvalue>=21:
numA=0
for card in player.hand:
if card.rank =="A":
numA=numA+1
while player.bjhandvalue>=21 and numA>0:
player.bjhandvalue=player.bjhandvalue-10
numA=numA-1
if player.bjvalue>=21:
print(player.name+", you lose! Game over!")
elifplayer.bjhandvalue<21:
numA=0
for card in player.hand:
if card.rank =="A":
numA=numA+1
while player.bjhandvalue<21 and numA>0:
player.bjhandvalue=player.bjhandvalue+10
numA=numA-1
players.append(player)
if players.bjhandvalue>21 and players.bjhandvalue<21:
print(players.name+", you wins!")
elif players.bjhandvalue>21 and players.bjhandvalue<21:
print(players.name+", you wins!")
elif players.bjhandvalue>21 and players.bjhandvalue>21:
print("Both of you lose.")
elif players.bjhandvalue<21 and players.bjhandvalue<21:
if (21-players.bjhandvalue)<(21-players.bjhandvalue):
print(players.name+", you wins!")
else:
print(players.name+", you wins!")
def makeADeckOf52():
#input: none
#output: a list of 52 strings
#process: pair the 4 suits with the 13 card rank values
#modeling the 52 cards:
#A-K of clubs,diamonds, hearts, spades
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(Card(r,s))
random.shuffle(deck)
return(deck)
def turn(deck,player):
player.bjhandvalue=0
for i in range(2):
card=dealAHand(deck, player)
player.hand.append(card)
player.bjhandvalue=update_hand_value(player)
return player.bjhandvalue
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 dealAHand(deck, player):
card=deck.pop()
return card
def create_BJValuecard_LookUp(cardStr):
#assume card is in this format: A♣ , 10♥
#"♣","♦","♥","♠"
rank = cardStr[:len(cardStr)-1]
print(rank)
specialValues = {"A":1, "J":10,"Q":10,"K":10}
if rank in specialValues:
bjValue = specialValues
else:
bjValue = int(rank)
return(bjValue)
def displayHand(player):
print(player.name+"' hand card is "+player.hand)
def update_hand_value(player):
for card in player.hand:
cardbjvalue=create_BJValuecard_LookUp(card)
player.bjhandvalue=player.bjhandvalue+cardbjvalue
return player.bjhandvalue
main()
你可以帮我检查一下有什么问题吗
pan11 发表于 2023-12-11 10:36
import random
class Card:
def __init__(self, ...
看起来代码中缺少了一些函数的定义和实现,比如makeADeckOf52函数、turn函数、displayHand函数等。这些函数的具体实现我无法确定,所以无法直接运行代码。不过,根据你的代码逻辑,我可以尝试给出一种实现方式供你参考。
首先,需要添加一个函数用于比较两个玩家的点数:
def compare_players(player1, player2):
if player1.bjhandvalue > player2.bjhandvalue:
print(player1.name + " wins!")
elif player1.bjhandvalue < player2.bjhandvalue:
print(player2.name + " wins!")
else:
print("It's a draw!")
然后,在主函数main中,在游戏结束后调用compare_players函数比较两个玩家的点数:
def main():
instructions()
deck = makeADeckOf52()
players = []
for i in range(2):
name = input("Enter the name of the player: ")
player = Player(name)
player.bjhandvalue = 0
player.bjhandvalue = turn(deck, player)
displayHand(player)
while player.bjhandvalue == 21:
numA = 0
for card in player.hand:
if card.rank == "A":
numA += 1
while player.bjhandvalue >= 21 and numA > 0:
player.bjhandvalue -= 10
numA -= 1
if player.bjhandvalue >= 21:
print(player.name + ", you lose! Game over!")
break# 结束玩家回合
players.append(player)
if len(players) == 2:
compare_players(players, players)
if __name__ == '__main__':
main()
这样,游戏结束后就会根据比较玩家的点数,输出胜负结果。请注意,为了保证代码的完整性,你还需要补充其他函数的定义和实现。希望对你有所帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]