我正在尝试学习如何在更复杂(但不太复杂)的Python程序中使用算法和统计信息。我一直在寻找一些我想尝试使自己变得有趣的例子,我在Github上找到了这个例子。可悲的是,我无法与创建者建立联系,因此我希望这里有人可以帮我解释一下。我了解该程序的大部分内容。这几乎是一个更高级的3人骰子掷骰,该掷骰返回一个统计数据,该骰子根据对手的选择选择获胜的最大和最差机会。首先让我向您展示整个代码。
from random import randint
rounds = 100
dice = {
"red": [0, 0, 4, 4, 8, 8],
"green": [2, 2, 3, 3, 7, 7],
"blue": [1, 1, 5, 5, 6, 6]
}
def runGame(p1, p2):
die1 = dice[p1]
die2 = dice[p2]
return 1 if die1[randint(0, 5)] > die2[randint(0, 5)] else 2
def getWinner(r, b, g):
if max(r, b, g) == r:
return "red"
elif max(b, g) == b:
return "blue"
else:
return "green"
def getLoser(r, b, g):
if min(r, b, g) == r:
return "red"
elif min(b, g) == b:
return "blue"
else:
return "green"
# Simulate all possible game states with two players
# the key represents the die chosen by opponents, the value is itself a dict with win rates
# eg {"blue" : 0.45, "green" : 0.68}
results = {
"red": {},
"blue": {},
"green": {}
}
print("Running %d simulations for every combination of two players..." % rounds, end="")
for opponent in results.keys():
remaining = list(dice.keys())
remaining.remove(opponent)
for player in remaining:
wins = 0
for _ in range(rounds):
if runGame(player, opponent) == 1:
wins += 1
results[opponent][player] = wins / rounds
print("completed.")
for die in dice.keys():
rem = list(dice.keys())
rem.remove(die)
if results[die][rem[0]] > results[die][rem[1]]:
print("If opponent chooses the %s dice, we should choose the %s dice, giving us %.2f%% chance of victory, "
"as opposed to %.2f%% chance with the %s dice." % (die, rem[0], results[die][rem[0]] * 100, results[die][
rem[1]] * 100, rem[1]))
else:
print("If opponent chooses the %s dice, we should choose the %s dice, giving us %.2f%% chance of victory, "
"as opposed to %.2f%% chance with the %s dice." % (die, rem[1], results[die][rem[1]] * 100,
results[die][rem[0]] * 100, rem[0]))
# initialise a result dictionary for the three player game
results3Player = {}
for die in dice.keys():
results3Player[die] = {
"wins": 0,
"losses": 0
}
print("\nNow running %d simulations for 3 players" % rounds)
for _ in range(rounds):
# Simulate the 3 player game
red = dice["red"][randint(0, 5)]
blue = dice["blue"][randint(0, 5)]
green = dice['green'][randint(0, 5)]
winner = getWinner(red, blue, green)
loser = getLoser(red, blue, green)
results3Player[winner]['wins'] += 1
results3Player[loser]['losses'] += 1
min = 0
max = 0
minDie = None
maxDie = None
for die in dice.keys():
# Find the maximum winner and maximum loser
if results3Player[die]['wins'] > max:
max = results3Player[die]['wins']
maxDie = die
if results3Player[die]['losses'] > min:
min = results3Player[die]['losses']
minDie = die
print("The die most likely to win is %s with %.2f%% probability of winning" %(maxDie, max*100/rounds))
print("The die most likely to lose is %s with %.2f%% probability of losing" %(minDie, min*100/rounds))
此代码有两部分我仍然不明白。
def runGame(p1, p2):
die1 = dice[p1]
die2 = dice[p2]
return 1 if die1[randint(0, 5)] > die2[randint(0, 5)] else 2
如果骰子1返回的数字介于0到5之间,大于骰子2返回的数字,则程序将返回1。否则,程序将返回2。在这种情况下1和2是什么意思?
然后有这部分
for opponent in results.keys():
remaining = list(dice.keys())
remaining.remove(opponent)
for player in remaining:
wins = 0
for _ in range(rounds):
if runGame(player, opponent) == 1:
wins += 1
results[opponent][player] = wins / rounds
print("completed.")
如果我理解正确, “剩余”表示骰子字典中的值列表。当玩家和对手获胜时,新值将添加到词典中。但是我不确定“ remaining.remove(opponent)”是什么意思。对手以什么方式被撤职?
在这种情况下1和2是什么意思?
[1
表示以p1
代表的玩家获胜,2
表示以p2
代表的玩家获胜。
但是我不确定
remaining.remove(opponent)
是什么意思。对手以什么方式被撤职?
它只是从该列表中删除。循环for player in remaining:
使该对手与每个玩家对战,而没有一个玩家与自己对战。因此,我们首先将播放器从列表中删除。
另一种方法是跳过循环中的该播放器:
players = list(results.keys())
for player in players:
for opponent in players:
if player == opponent:
continue # don't play against yourself
wins = 0
for _ in range(rounds):
if runGame(player, opponent) == 1:
wins += 1
results[opponent][player] = wins/rounds