|
楼主 |
发表于 2023-12-11 10:36:56
|
显示全部楼层
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?[y/n]")
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!")
elif 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
players.append(player)
if players[0].bjhandvalue>21 and players[1].bjhandvalue<21:
print(players[1].name+", you wins!")
elif players[1].bjhandvalue>21 and players[0].bjhandvalue<21:
print(players[0].name+", you wins!")
elif players[0].bjhandvalue>21 and players[1].bjhandvalue>21:
print("Both of you lose.")
elif players[0].bjhandvalue<21 and players[1].bjhandvalue<21:
if (21-players[0].bjhandvalue)<(21-players[1].bjhandvalue):
print(players[0].name+", you wins!")
else:
print(players[1].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[rank]
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()
你可以帮我检查一下有什么问题吗
|
|