0

私は、競合他社のリストを取得し、各ラウンドで誰と対戦する必要があるかをランダムに決定する競技コードを作成しようとしています。 この種のコンテストに関する wiki 記事

各ラウンドの終わりに、各試合の勝者がポイントを受け取ります。

すべての可能な戦いが終了すると、最も多くのポイントを持つものが勝ちます。

しかし、私はいくつかの問題を抱えています。これまでのコードは次のとおりです。

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)

編集:申し訳ありませんが、何らかの理由でこの部分を書く前に質問されました。

私の問題は、順列がすべての可能な順列を与えることです.1ラウンドの競合他社の順列を取得し、次に実行するときに、彼らがすでに戦った相手を参照し、その試合が来ないようにしたいだけです.もう一度。

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

「各ラウンドで誰と対戦する必要があるかをランダムに決定する」と言いますが、ウィキペディアの記事で説明されているラウンドロビンアルゴリズムに従うだけで済みます。これはランダムではありませんが、n人のプレーヤーに対して、(n-1)ラウンド後にそれぞれプレイヤーは他のすべてに遭遇しました。

于 2012-08-08T08:46:16.133 に答える
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 に答える