我的任务是做一个骰子扑克游戏,目前的问题是给玩家手牌分配一个等级,即一个介于0和6之间的整数值。
下面是完整的问题
“确定玩家手牌的等级(即”掷骰子扑克游戏和规则“一节中描述的介于0和6之间的整数值)。提示:为了确定手牌的等级,请使用保存每个骰子面值掷出次数的die_count列表。”
我该如何根据牌的数量给牌分配一个等级?我不打算使用.count()或任何其他类型的列表方法
到目前为止,以下是我的代码
import dice
import random
player_hand = [0, 0, 0, 0, 0]
computer_hand = [0, 0, 0, 0, 0]
die_count = [0, 0, 0, 0, 0, 0, 0]
die_count2 = [0, 0, 0, 0, 0, 0, 0]
i = 0
l = 0
for x in range(5):
player_hand[x] = random.randint(1,6)
computer_hand[x] = random.randint(1,6)
while i < 5 :
die_value = player_hand[i]
die_count[die_value] = die_count[die_value] + 1
i = i + 1
while l < 5 :
die_value = computer_hand[l]
die_count2[die_value] = die_count2[die_value] + 1
l = l + 1
print ("Player's hand:")
dice.display_hand(player_hand, 5)
print (" ")
print ("Computer's hand:\n")
dice.display_hand(computer_hand, 5)
print (str(die_count))
print (str(die_count2))发布于 2019-05-05 22:45:32
如果您想要包含骰子值计数的列表:
dice_count_player=[player_hand.count(x) for x in range(1,7)]如果你真的不想使用count,那么你可以像你在问题中写的那样做这一部分,但要短一点:
dice_count_player = [0, 0, 0, 0, 0, 0, 0]
for v in player_hand:
dice_count_player[v] += 1如果您希望以相反的顺序进行计数:
dice_count_player_desc = sorted(dice_count_player, reverse=True)然后,您可以这样计算该值:
if dice_count_player_desc[0] == 5:
print("Five of a kind")
elif dice_count_player_desc[0] == 4:
print("Four of a kind")
...
elif dice_count_player_desc[0] == 2 and dice_count_player_desc[1] == 2:
print("Two pairs")完整的示例:
import random
player_hand = [0, 0, 0, 0, 0, 0]
computer_hand = [0, 0, 0, 0, 0, 0]
dice_count_player = [0, 0, 0, 0, 0, 0, 0]
dice_count_computer = [0, 0, 0, 0, 0, 0, 0]
for x in range(6):
player_hand[x] = random.randint(1,6)
computer_hand[x] = random.randint(1,6)
print ("Player's hand:")
print(player_hand)
print ()
print ("Computer's hand:\n")
print(computer_hand)
for v in player_hand:
dice_count_player[v] += 1
for v in computer_hand:
dice_count_computer[v] += 1
dice_count_player_desc = sorted(dice_count_player, reverse=True)
if dice_count_player_desc[0] == 5:
print("Five of a kind")
elif dice_count_player_desc[0] == 4:
print("Four of a kind")
elif dice_count_player_desc[0] == 3 and dice_count_player_desc[1] == 2:
print("Full house")
elif max(dice_count_player) == 1 and (dice_count_player[1] == 0 or dice_count_player[6] == 0):
print("Straight")
elif dice_count_player_desc[0] == 3:
print("Three of a kind")
elif dice_count_player_desc[0] == 2 and dice_count_player_desc[1] == 2:
print("Two pairs")
elif dice_count_player_desc[0] == 2 and dice_count_player_desc[1] < 2:
print("One pair")
else:
print("Bust")发布于 2019-05-06 23:50:08
欢迎来到Stack Overflow!给未来的提示:当你排除了重要的信息时,你会降低得到好答案的可能性,比如你正在编程的游戏规则。对于骰子扑克,似乎有多套相似但不同的规则。知道你在使用哪一个是很有用的。一定要包括像这样的东西。
例如,不清楚为什么die_count中有7个元素,因为骰子有6个边。我只能假设您使用的规则是1可以是1,也可以是A。但如果是这种情况,那么任何6的计数都应该是索引5,而不是6-也就是说,如果你扮演5个6,那么die_count应该是[0, 0, 0, 0, 0, 5, 0],但是你的程序计算[0, 0, 0, 0, 0, 0, 5]。此外,您只使用值1-6作为die_count的索引,这意味着根本不使用第一个元素(索引0)。
我使用了骰子扑克规则发现here,这显然偏离了你的规则(例如9手/等级而不是7手)。我的代码应该仍然有用,您可以根据需要对其进行调整。我的代码还假设骰子的面值不会影响排名,即一对2和一对6具有相同的排名。
实际的排名过程是使用字典rank_dict在函数rank_hand中进行的。我已经排除了你的dice模块,因为我不知道它是做什么的,也不知道从哪里得到它(不管怎么说,它似乎不会影响排名)。我已经添加了丰富的注释,这样您就可以理解每个部分的作用--没有它们,代码实际上非常紧凑。
from random import randint
hand_play, die_count_play = [0] * 5, [0] * 6
hand_comp, die_count_comp = [0] * 5, [0] * 6
# The work done in your three loops can be done in one for-loop.
for i in range(5):
hand_play[i], hand_comp[i] = randint(1,6), randint(1,6)
die_count_play[hand_play[i]-1] += 1
die_count_comp[hand_comp[i]-1] += 1
# Put the rankings in a dictionary with tuples corresponding to hands as keys.
rank_dict = { (5,):8, # five of a kind
(4,):7, # four of a kind
(2, 3):6, # full house
(0, 1, 1, 1, 1, 1):5, # high straight
(1, 1, 1, 1, 1, 0):4, # low straight
(3,):3, # three of a kind
(2, 2):2, # two pairs
(2,):1 # one pair
}
def rank_hand(die_count, rank_dict = rank_dict):
"""
Take in list (length == 6) where each element is a count of the number of
times the digits 1-6 occur in throw of 5 dice. Return a hand ranking using
a dictionary of rankings `rank_dict`.
"""
# If max value is two then key is either (2, 2) or (2,).
if max(die_count) == 2:
rank = rank_dict[tuple([c for c in die_count if c == 2])]
# If max value is 1 then hand is either high or low straight, or no hand.
elif max(die_count) == 1:
# One of the values, first or last, must be 0 if hand is a high/low
# straight. If not, then there's no hand.
if 0 in die_count[0::5]:
rank = rank_dict[tuple(die_count)]
else:
rank = 0
# If not a pair, straight, or no hand, then it must be one of remaining
# hands.
else:
rank = rank_dict[tuple(set([c for c in die_count if c > 1]))]
return rank
# Call rank_hand on die counts for player and computer.
rank_play = rank_hand(die_count_play)
rank_comp = rank_hand(die_count_comp)
print(f"Player's rank: {rank_play}\n" +
f"Player's hand: {hand_play}\n" +
f"Die count: {die_count_play}\n" + 30*"-"
)
print(f"Computer's rank: {rank_comp}\n" +
f"Computer's hand: {hand_comp}\n" +
f"Die Count: {die_count_comp}"
)https://stackoverflow.com/questions/55992869
复制相似问题