我有list
的{{1}}:
dicts
如果只有1名得分最高的玩家,我可以回答该玩家没问题:
>>> for player in players:
print(player.show_hand())
'{'name': 'Tim', 'hand': [Card(rank='7', suit='diamonds', value=6), Card(rank='9', suit='spades', value=8)], 'score': 1}
{'name': 'Manu', 'hand': [Card(rank='9', suit='clubs', value=8), Card(rank='7', suit='clubs', value=6)], 'score': 1}
{'name': 'Kawhi', 'hand': [Card(rank='K', suit='diamonds', value=12), Card(rank='2', suit='clubs', value=1)], 'score': 1}
{'name': 'Tony', 'hand': [Card(rank='Q', suit='clubs', value=11), Card(rank='3', suit='clubs', value=2)], 'score': 1}'
如果有相同分数的玩家因为他们拥有相同类型的牌(例如最高牌),我想迭代列表并检查每个score_list = []
for num in range(len(players)):
score_list.append(players[num].show_hand()['score'])
winner = []
for player in players:
if player.show_hand()['score'] == max(score_list):
winner.append(player.show_hand()['name'])
if len(winner) > 1:
print('draw')
highest_card = []
for player in players:
if player.show_hand()['name'] in winner:
print(player.show_hand()['name'])
for num in range(2):
highest_card.append(player.show_hand()['hand'][num].value)
for num in range(2):
if player.show_hand()['hand'][num].value == max(highest_card):
print('The Winner is ', player.show_hand()['name'])
print(highest_card)
else:
print('The Winner is ', winner)
的{{1}}玩家最高hand
list
以确定获胜者或游戏是否为平局。这是我失败的地方。
编辑:
当所有玩家Card
为1(最高牌)或2(对)时,这是我得到的输出:
value
卡是一个名为元组:
score
答案 0 :(得分:0)
我通过向key
highest_card_value
中的player
个实例添加新的players
list
来解决此问题。然后,我比较value
中Card
个hand
个对象,并给出highest_card_value
手中最高牌的值。
for player in players:
player.show_hand()['score'] = score.define_score(player.show_hand()['hand'] + dealer.show_hand())
if player.show_hand()['hand'][0].value > player.show_hand()['hand'][1].value:
player.show_hand()['highest_card_value'] = player.show_hand()['hand'][0].value
else:
player.show_hand()['highest_card_value'] = player.show_hand()['hand'][1].value
然后循环遍历值:
score_list = []
for num in range(len(players)):
score_list.append(players[num].show_hand()['score'])
winner = []
for player in players:
if player.show_hand()['score'] == max(score_list):
winner.append(player.show_hand()['name'])
print(winner)
if len(winner) > 1:
print('draw')
highest_card = []
for player in players:
if player.show_hand()['name'] in winner:
for num in range(2):
highest_card.append(player.show_hand()['hand'][num].value)
for player in players:
if player.show_hand()['highest_card_value'] == max(highest_card):
print('The Winner is ', player.show_hand()['name'])
print(max(highest_card))
else:
print('The Winner is ', winner)
这是输出:
{'hand': [Card(rank='3', suit='spades', value=2), Card(rank='2', suit='diamonds', value=1)], 'name': 'Tim', 'highest_card_value': 2, 'score': 1}
{'hand': [Card(rank='Q', suit='clubs', value=11), Card(rank='7', suit='diamonds', value=6)], 'name': 'Manu', 'highest_card_value': 11, 'score': 1}
{'hand': [Card(rank='6', suit='hearts', value=5), Card(rank='A', suit='spades', value=13)], 'name': 'Kawhi', 'highest_card_value': 13, 'score': 1}
{'hand': [Card(rank='3', suit='diamonds', value=2), Card(rank='2', suit='clubs', value=1)], 'name': 'Tony', 'highest_card_value': 2, 'score': 1}
['Tim', 'Manu', 'Kawhi', 'Tony']
draw
The Winner is Kawhi
13
每张牌都有最高牌hand
,但是Kawhi牌的价值最高,所以他赢了。