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