因此,对于我正在编写的这段代码,我试图为三个不同的玩家生成一组三张随机卡片,其中一张是人类的,两张是模拟的。为此,我使用以下代码。
def shuffle_p1():
p_1_human_card_1=random.randint(1,3)
p_1_human_card_2=random.randint(1,3)
p_1_human_card_3=random.randint(1,3)
p_1_human_cards=[p_1_human_card_1,p_1_human_card_2,p_1_human_card_3]
return (p_1_human_cards)
def shuffle_p2():
p_2_ai_card_1=random.randint(1,3)
p_2_ai_card_2=random.randint(1,3)
p_2_ai_card_3=random.randint(1,3)
p_2_ai_cards=[p_2_ai_card_1,p_2_ai_card_2,p_2_ai_card_3]
return (p_1_human_cards)
def shuffle_p3():
p_3_ai_card_1=random.randint(1,3)
p_3_ai_card_2=random.randint(1,3)
p_3_ai_card_3=random.randint(1,3)
p_3_ai_cards=[p_3_ai_card_1,p_3_ai_card_2,p_3_ai_card_3]
return (p_1_human_cards)
p_1_human_cards=shuffle_p1()
p_2_ai_cards=shuffle_p2()
p_3_ai_cards=shuffle_p3()
这会生成三套牌,但是玩家1和玩家2每次都有完全相同的牌。我甚至放了一组不同的代码
def card_auth_p1():
while p_1_human_cards[0]==p_1_human_cards[1] or p_1_human_cards[0]==p_1_human_cards[2]:
p_1_human_cards[0]=random.randint(1,3)
while p_1_human_cards[1]==p_1_human_cards[0] or p_1_human_cards[1]==p_1_human_cards[2]:
p_1_human_cards[1]=random.randint(1,3)
def card_auth_p2():
while p_2_ai_cards[0]==p_2_ai_cards[1] or p_2_ai_cards[0]==p_2_ai_cards[2]:
p_2_ai_cards[0]=random.randint(1,3)
while p_2_ai_cards[1]==p_2_ai_cards[0] or p_2_ai_cards[1]==p_2_ai_cards[2]:
p_2_ai_cards[1]=random.randint(1,3)
def card_auth_p3():
while p_3_ai_cards[0]==p_3_ai_cards[1] or p_3_ai_cards[0]==p_3_ai_cards[2]:
p_3_ai_cards[0]=random.randint(1,3)
while p_3_ai_cards[1]==p_3_ai_cards[0] or p_3_ai_cards[1]==p_3_ai_cards[2]:
p_3_ai_cards[1]=random.randint(1,3)
而且还
if p_1_human_cards == p_2_ai_cards or p_1_human_cards == p_3_ai_cards:
p_1_human_cards=shuffle_p1()
if p_2_ai_cards == p_1_human_cards or p_2_ai_cards == p_3_ai_cards:
p_2_ai_cards=shuffle_p2()
if p_3_ai_cards == p_1_human_cards or p_2_ai_cards == p_3_ai_cards:
p_3_ai_cards=shuffle_p3()
为了确保它们不都相同,但打印这三个列表会显示玩家1和2仍然相同。此外,即使在第四块代码之后,在三个集合之间使用此代码进行随机交易
def card_trade():
print('Round 1')
p_1_choice=int(input('pick the card you want to take from player 2'))
while p_1_choice<1 or p_1_choice>3:
print('pick between 1 and 3')
p_1_choice=int(input('pick the card you want to take from player 2'))
if p_1_choice==1:
p_1_extra_card=p_2_ai_cards[0]
p_2_ai_cards.remove(p_2_ai_cards[0])
elif p_1_choice==2:
p_1_extra_card=p_2_ai_cards[1]
p_2_ai_cards.remove(p_2_ai_cards[1])
elif p_1_choice==3:
p_1_extra_card=p_2_ai_cards[2]
p_2_ai_cards.remove(p_2_ai_cards[2])
p_1_human_cards.append(p_1_extra_card)
p_2_choice=random.randint(1,3)
print('AI decided to take',p_2_choice,'from player 3')
if p_2_choice==1:
p_2_extra_card=p_3_ai_cards[0]
p_3_ai_cards.remove(p_3_ai_cards[0])
elif p_2_choice==2:
p_2_extra_card=p_3_ai_cards[1]
p_3_ai_cards.remove(p_3_ai_cards[1])
elif p_2_choice==3:
p_2_extra_card=p_3_ai_cards[2]
p_3_ai_cards.remove(p_3_ai_cards[2])
p_2_ai_cards.append(p_2_extra_card)
p_3_choice=random.randint(1,4)
print('AI decided to take',p_3_choice,'from player 1')
if p_3_choice==1:
p_3_extra_card=p_1_human_cards[0]
p_1_human_cards.remove(p_1_human_cards[0])
elif p_3_choice==2:
p_3_extra_card=p_1_human_cards[1]
p_1_human_cards.remove(p_1_human_cards[1])
elif p_3_choice==3:
p_3_extra_card=p_1_human_cards[2]
p_1_human_cards.remove(p_1_human_cards[2])
elif p_3_choice==3:
p_3_extra_card=p_1_human_cards[3]
p_1_human_cards.remove(p_1_human_cards[33])
p_3_ai_cards.append(p_3_extra_card)
前两个玩家总是以完全相同的方式结束。我似乎弄不明白为什么它们似乎永远不会改变,希望能有任何见解来帮助解决这个问题。
另一件事需要注意,有时在牌交易()中,当人类玩家选择3作为他们的选择时,计算机可能会抛出一个错误,但它这样做是非常不一致的,所以我也找不到错误的根源。
发布于 2020-10-14 09:32:31
看起来你复制了前面的代码块,而return
部分保持不变。:)尝试尽可能地code DRY。你也可以这样做:
import random
def shuffle_p1():
p_1_human_cards=[random.randint(1,3) for _ in range(3)]
return p_1_human_cards
def shuffle_p2():
p_2_ai_cards=[random.randint(1,3) for _ in range(3)]
return p_2_ai_cards
def shuffle_p3():
p_3_ai_cards=[random.randint(1,3) for _ in range(3)]
return p_3_ai_cards
发布于 2020-10-14 09:44:20
如果你有Python 3.6+,你也可以尝试加密。
try:
import secrets as random
except ModuleNotFoundError:
import random
# Use system resources for randomization
rnd = random.SystemRandom()
# Set available card options
AVAILABLE_CARDS = [1,2,3,]
# Set number of cards per hand.
NUMBER_OF_CARDS = 3
# Create a simple key-value collection.
players = dict(
p_1_human_cards = None,
p_2_ai_cards = None,
p_3_ai_cards= None,
)
# Define a shuffler() function. Parameters are number of cards and available cards.
def shuffler(n_cards=NUMBER_OF_CARDS , available_cards = AVAILABLE_CARDS ):
return tuple([rnd.choice(available_cards) for _ in range(n_cards)])
# Iterate through players to set each hand
for hand in players:
players[hand] = shuffler()
# Print result
for player, hand in players.items():
print(f"{player}: {hand}")
输出:
p_1_human_cards: (2, 1, 3)
p_2_ai_cards: (3, 2, 1)
p_3_ai_cards: (2, 3, 3)
https://stackoverflow.com/questions/64345240
复制相似问题