说我有一张纸牌。它们中的每一个代表玩家玩的牌。
('9D', '9S', '3S', '0D')
Player0 Player1 Player2 Player3
我希望在其中找到获胜的卡片,并记录谁是获胜者。
决定胜利者的规则非常混乱......
首先,我有一张用于确定特朗普套装的王牌。
假设我有2D
作为王牌。然后诉讼钻石成为特朗普诉讼。
其次,元组中的第一张牌意味着领先牌,所有其他牌手都应该尝试跟随该牌的牌照,如果没有,那么该牌手所玩的牌几乎会丢失。
我希望比较这些卡片的面值。在这种情况下, 0 代表 10 。价值最高的卡也跟随诉讼获胜。下面是我的代码,我甚至没有接近目标。我知道我的目标是什么。但我的代码肯定包含冗余。有没有更好的方法来实现这一目标?
非常感谢任何关于如何实施整洁解决方案的建议。
我的代码:
tricks = ('9D', '9S', '3S', '0D')
counter = 1
for i in tricks:
while counter < len(i):
if max_score[1] == deck_top[1]:
# Check if it is in trump suit.
if int(i[counter-1][0]) < int(i[counter][0]):
# Compare face value
if i[counter-1][0] == i[counter][0]:
# Compare the suit.
if i[counter][1] == i[counter][1]:
答案 0 :(得分:1)
如果我能正确理解问题,这是一个潜在的解决方案。
trump_suit = 'S'
cards = ('9D', '9S', '3S', '0D')
suit_to_follow = cards[0][1]
leading_player = None
leading_value = None # (is_trump, value)
for player, card in enumerate(cards):
value = int(card[0]) or 10
suit = card[1]
is_trump = suit == trump_suit
current_value = (is_trump, value)
if is_trump or suit == suit_to_follow:
if current_value > leading_value:
leading_player = player
leading_value = current_value
print 'winning player:', leading_player
答案 1 :(得分:1)
您可以使用列表推导,max()和自定义排序键来完成此操作。我不是肯定的我正确地理解了这个问题,但我把它解释为:胜利者是价值最高且与王牌相同的牌
def getCardValue(inputVal): #custom key func that calculates zero as a 10
num = int(inputVal[:-1])
if num == 0:
return 10
return num
trumpCard = '2D'
tricks = ('9D', '9S', '3S', '0D')
potentialWinners = [x for x in tricks if x[-1] == trumpCard[-1]] #remove cards that don't have same suit as trumpCard
print(max(potentialWinners, key=getCardValue)) #get max card value while calculating zero as 10
def getCardValue(inputVal): #custom key func that calculates zero as a 10
inputVal = inputVal[:-1]
specialValues = {"J": 11, "Q": 12, "K": 13, "A": 14}
if inputVal in specialValues.keys():
return specialValues[inputVal]
num = int(inputVal)
if num == 0:
return 10
return num
将最后两行更改为:
winner = max(potentialWinners, key=getCardValue) #get max card value while calculating zero as 10
print("player", tricks.index(winner), "wins with the card", winner)