2015年6月1日

用Python穷举魔兽世界队伍的构成

居然是2015第一篇……

起因是知乎游戏群里,有人出了道题,魔兽世界当前版本里,1坦1治疗3输出,能有多少种组合?前置条件是,只看职业组合,不分具体天赋。例如1德鲁依1武僧3法师,无论是德鲁依还是武僧做坦,都算作一种组合。

然后大家陷入了各种排列组合的陷阱中……各种答案都有,并且看上去都没错。于是最后我决定用代码暴力验算。最后几个人的验算结果一致得出结论,排列是2450种。

代码如下:

import itertools

WOW_classes = { "war": "Warrior",
    "dk": "Dark Knight",
    "pal": "Paladin",
    "ht": "Hunter",
    "sm": "Shaman",
    "dru": "Druid",
    "rog": "Rogue",
    "monk": "Monk",
    "mage": "Mage",
    "wl": "Warlock",
    "pri": "Priest"
    }

all_DPS = [WOW_classes[dps] for dps in WOW_classes]
T = ["war", "dk", "dru", "pal", "monk"]
all_T = [WOW_classes[t] for t in T]
H = ["pal", "sm", "dru", "monk", "pri"]
all_H = [WOW_classes[h] for h in H]

team_queue = [all_T] * 1 + [all_H] * 1 + [all_DPS] * 3

print("Generating possible teams...")
possible_team = list(itertools.product(*team_queue))
print("Generating possible teams are generated.")

all_team = []
print("Start sorting and removing duplicate team...")
for team in possible_team:
 if sorted(team) not in all_team:
  all_team.append(sorted(team))
print("Sorting and removing are done.")


print("Begin to write to file...")
file = open("team.txt", "w+")
for team in all_team:
 file.write(str(team) + '\n')
file.write(("\nTotal combination is %d!" % len(all_team)))
file.close()
print("Writing finished.")


然后,我们决定玩个更刺激的,算算25人团队副本里,2T6H17DPS的组合会有多少种。
我改完上面代码里的变量,开始运行后几秒,就华丽丽的黑屏了。内存用尽而亡……

代码尚未成功,仍需努力……

没有评论: