0

我正在尝试编写一个竞赛代码,该代码将获取竞争对手列表,然后随机决定他们在每一轮中必须面对的人,循环赛风格没有淘汰赛。 关于此类竞赛的维基文章

在每轮比赛结束时,每场比赛的获胜者将获得一分。

当所有可能的战斗结束时,得分最多的一方获胜。

但我遇到了一些麻烦,这是我到目前为止的代码:

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.
4

3 回答 3

2

我在这里找到了答案:

https://stackoverflow.com/a/11246261/1561176

这就是我需要的,现在只需要输入我自己的值。并修改它的输出方式。

可能应该尝试更深入地寻找,谢谢大家的帮助。

于 2012-08-08T09:10:44.237 回答
0

而不是使用排列,例如:

import copy
import random
# Setup:
opponents = dict()
for competitor in competitors:
    opponents[competitor] = copy.deepcopy(competitors)

def do_round():
    players = copy.deepcopy(competitors)
    while players:
        p1 = players.pop(0)
        p2 = random.choice(opponents[p1])
        opponents[p2].remove(p1)
        opponents[p1].remove(p2)
        players.remove(p2)
        play_match(p1, p2)

for _ in range(len(competitors)):
    do_round()

如果比赛不并行进行,则创建时间表会容易得多

于 2012-08-08T07:50:26.347 回答
0

你说“随机决定他们必须在每一轮面对谁”,但你可以按照维基百科文章中描述的循环算法,这不是随机的,但可以确保对于 n 个玩家,在 (n-1) 轮之后,每个玩家遇到了所有其他人。

于 2012-08-08T08:46:16.133 回答