所以我试图统计多个玩家的手并将它们相互比较。
这是我的主要代码:
def make_deck():
# Randomly shuffle a deck
import random
cards = []
for suit in ['H', 'C', 'S', 'D']:
for num in ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q',
'K']:
cards.append(num + suit)
random.shuffle(cards)
return cards
deck = make_deck()
num_of_players = int(input("How many players?: "))
def deal_blackjack(deck, num_of_players):
# Deal two cards to a number of players
hands = [[] for p in range(num_of_players)]
i = -1
for k in range(0,2):
for h in hands:
i += 1
h.append(deck[i])
return hands
pHands = deal_blackjack(deck, num_of_players)
def print_blackjack(pHands):
# Prints Players with respective hand
for i in range(len(pHands)):
print('Player', i, ':', pHands[i])
def get_max(pHands):
#Where I'm stuck at
for i in range(pHands):
total = 0
pTotal = int(pHands[i][x][0])
我知道我必须使用pHands和一个循环来获取每只手的第一个角色,并将其转换为int ex。 int(pHands [i #of hand] [x #of 1st str] [0])然后将其添加到总数中
但我似乎无法想象如何为玩家的每只手实现它。
离。 4名球员
玩家0:['3S','JH']总数= 13
玩家1:['6C','JC']总数= 16
玩家2:['4H','5D']总数= 9
玩家3:['7D','AC']总数= 18
另外,我想比较每只手并选择一个胜利者(手旁边出现一个星号)。
离。 4名球员
玩家0:['3S','JH']
玩家1:['6C','JC']
玩家2:['4H','5D']
玩家3:['7D','AC'] *
感谢您的帮助!
答案 0 :(得分:2)
您应该创建一个名为calculate_hand
的方法,然后在那里完成您的工作。
scores = {"A":1, "T":10, "J": 10, "K":10, "Q": 10, }
def calculate_hand(hand):
hand_value = 0
ace = False
for card in hand:
if card[0] == "A":
ace =True;
if card[:-1] in scores: #used [:-1] insted of [0] because at first, thought it is 10 instead of T
hand_value += scores[card[:-1]]
else:
hand_value += int(card[:-1])
if ace and hand_value + 10 < 22:
hand_value += 10
return hand_value
hand1 = ['AD', 'AC']
hand2 = ['6C', 'JC']
hand3 = ['7D', 'AC']
value1 = calculate_hand(hand1) #12
value2 = calculate_hand(hand2) #16
value3 = calculate_hand(hand3) #18
在计算每只手之后,它只是从calculate_hand
返回的简单比较值。
print max(value1,value2,value3) #18
您应该轻松地将这些实现到您的代码中。
答案 1 :(得分:1)
cards_dict = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12,'K':13}
def card_to_value(card):
return cards_dict[card[1]]
def get_max(pHands):
for i in range(len(pHands)):
pCards = pHands[i]
pTotal[i] = card_to_value(pCards[0]) + card_to_value(pCards[1])
return pTotal
def winner(pTotal):
biggest_hand = max(pTotal)
#if its a tie
if pTotal.count(biggest_hand) > 1:
#i don't know what happens
else:
return pTotal.index(biggest_hand)