我正在尝试编写一个竞赛代码,该代码将获取竞争对手列表,然后随机决定他们在每一轮中必须面对的人,循环赛风格没有淘汰赛。 关于此类竞赛的维基文章
在每轮比赛结束时,每场比赛的获胜者将获得一分。
当所有可能的战斗结束时,得分最多的一方获胜。
但我遇到了一些麻烦,这是我到目前为止的代码:
import itertools
# list of competitors (does not need to be even, in this case, 7)
# maybe this should be a dict with their wins and loses like:
# {'Andy': [2,['Bob','Charlie'],0,[]], ... }
# which means andy won 2 times, against bob and charlie, and lost 0 times
competitors = ['Andy', 'Bob', 'Charlie', 'Daniel', 'Eric', 'Ferdinand', 'Gabriel']
# the starting round number
round_number = 1
# holds the winner
winner = None
# start the competition
print "The competitors are: \n" + " ".join(competitors)
# round handler
def do_round():
#round notifier
print "Round " + str(round_number)
#here is the problem
matches = itertools.permutations(competitors,2)
for match in matches:
print match
# increase round number
round_number += 1
#return this rounds matches
return matches
# gets the winners of each round for each match
def get_winners(matches):
winners = []
for match in matches:
winners.append(raw_input("who won?: " + " or ".join(match)))
# decides if the games are over yet
def is_there_a_winner(winners):
# this function should know how to get every rounds winners and add their points
# and then decide if the game is over and there is a total winner
winner = ??
# main loop
while not winner:
matches = do_round()
get_winners(matches)
is_there_a_winner(winners)
编辑:抱歉,在我出于某种原因写这部分之前,有人问了这个问题。
我的问题是排列给出了所有可能的排列,我只想在一轮中获得竞争对手的排列,然后下次我运行它时,能够参考他们已经与之对抗的人而不是比赛来又起来了。
Edit2:我决定在我的帖子中添加一个“期望的结果”。
我希望输出是这样的:
The competitors are:
Andy Bob Charlie Daniel Eric Ferdinand Gabriel
Round 1
Andy vs Bob
Charlie vs Daniel
Eric vs Ferdinand
Gabriel sits out
Round 1 Results:
Andy beat Bob
Charlie beat Daniel
Eric beat Ferdinand
Round 2
Andy vs Daniel
Bob vs Gabriel
Eric vs Charlie
Ferdinand sits out
... etc etc ... until at the end the winner (highest score) is revealed.