簡體   English   中英

如何運行我的代碼 10,000 次並跟蹤每個玩家獲勝的次數?

[英]How to run my code 10,000 times and keep track of how many times each player wins?

我試圖讓我的代碼運行 10,000 次。 它需要運行 10,000 次。 記錄每位玩家獲勝的次數、每位玩家獲得獎金的次數以及所有三名玩家獲得獎金的次數。 我不知道如何讓它工作並且一直試圖弄清楚。 誰能告訴我如何做到這一點? 我將在下面發布代碼。

 #Part3
 #Create a simulation of the game that runs 10,000 times.  
 #Keep track of how many times each player wins, how many times each player scores the bonus
 #how many times all three players earned the bonus.
    import random       
    Spin1 = (random.randint(1,20)*5)
    print('Spin1', Spin1)
    if (Spin1) > 94 :
      Spin2 = 0
    if (Spin1) == 100 :
      Spin2 = 0
      print('Player 1 spun 100! They have won $1,000!')
    if (Spin1) < 94:
      Spin2 = (random.randint(1,20)*5)
      print('Spin2', Spin2)
    SpinTotal1 = (Spin1 + Spin2)
    print('Player 1 Score = ',SpinTotal1)
    if (SpinTotal1) > 100 :
      SpinTotal1 = 0
      print('Player 1 spun above 100. They have been reset to',SpinTotal1)
    import random       
    Spin1 = (random.randint(1,20)*5)
    print('Spin1', Spin1)
    if (Spin1) > 94 :
      Spin2 = 0
    if (Spin1) == 100 :
      Spin2 = 0
      print('Player 2 spun 100! They have won $1,000!')
    if (Spin1) < 94:
      Spin2 = (random.randint(1,20)*5)
      print('Spin2', Spin2)
    SpinTotal2 = (Spin1 + Spin2)
    print('Player 2 Score = ',SpinTotal2)
    if (SpinTotal2) > 100 :
      SpinTotal2 = 0
      print('Player 2 spun above 100. They have been reset to',SpinTotal2)
    import random       
    Spin1 = (random.randint(1,20)*5)
    print('Spin1', Spin1)
    if (Spin1) > 95 :
      Spin2 = 0
    if (Spin1) == 100 :
      Spin2 = 0
      print('Player 3 spun 100! They have won $1,000!')
    if (Spin1) < 95 :
      Spin2 = (random.randint(1,20)*5)
      print('Spin2', Spin2)
    SpinTotal3 = (Spin1 + Spin2)
    print('Player 3 Score = ',SpinTotal3)
    if (SpinTotal3) > 100 :
      SpinTotal3 = 0
      print('Player 3 spun above 100. You have been reset to',SpinTotal3)
    if (SpinTotal1 == SpinTotal2) :
      SpinOff3 = 0
      print('Tie! This results in a Spin Off between Player 1 and 2')
      SpinOff1 = (random.randint(1,20)*5)
      print('P1 Spin off score =', SpinOff1)
      SpinOff2 = (random.randint(1,20)*5)
      print('P2 Spin off score =', SpinOff2)
    if (SpinTotal1 == SpinTotal3) :
      SpinOff2 = 0 
      print('Tie! This results in a Spin Off between Player 1 and 3')
      SpinOff1 = (random.randint(1,20)*5)
      print('P1 Spin off score =', SpinOff1)
      SpinOff3 = (random.randint(1,20)*5)
      print('P3 Spin off score =', SpinOff3)
    if (SpinTotal2 == SpinTotal3) :
      SpinOff1 = 0
      print('Tie! This results in a Spin Off between Player2 and 3')
      SpinOff2 = (random.randint(1,20)*5)
      print('P2 Spin off score =', SpinOff2)
      SpinOff3 = (random.randint(1,20)*5)
      print('P3 Spin off score =', SpinOff3)
    if (SpinTotal1 > SpinTotal2 and SpinTotal1 > SpinTotal3 and SpinTotal1 > SpinOff2 and SpinTotal1 > SpinOff3):
      print('Player 1 is the winner, will recieve $10,000 prize!')
      if (SpinTotal1) == 100 :
        print('Your total prize is $11,000! Congratulations!')
    if (SpinTotal2 > SpinTotal1 and SpinTotal2 >SpinTotal3 and SpinTotal2 > SpinOff1 and SpinTotal2 > SpinOff3):
      print('Player 2 is the winner, will recieve $10,000!')
      if (SpinTotal2) == 100 :
        print('Your total prize is $11,000! Congratulations!')
    if (SpinTotal3 > SpinTotal1 and SpinTotal3 > SpinTotal2 and SpinTotal3 > SpinOff1 and SpinTotal3 > SpinOff2):
      print('Player 3 is the winner, will recieve $10,000!')
      if (SpinTotal3) == 100 :
        print('Your total prize is $11,000! Congratulations!')
    if (SpinOff1 > SpinTotal1 and SpinOff1 > SpinTotal2 and SpinOff1 > SpinTotal3 and SpinOff1 > SpinOff2 and SpinOff1 > SpinOff3):
      print('Player 1 is the winner, $10,000!')
      if (SpinOff1) == 100 :
        print('Your total prize is $11,000! Congratulations!')
    if (SpinOff2 > SpinTotal1 and SpinOff2 > SpinTotal2 and SpinOff2 > SpinTotal3 and SpinOff2 > SpinOff1 and SpinOff2 > SpinOff3):
      print('Player 2 is the winner, $10,000!')
      if (SpinOff2) == 100 :
        print('Your total prize is $11,000! Congratulations!')
    if (SpinOff3 > SpinTotal1 and SpinOff3 > SpinTotal2 and SpinOff3 > SpinTotal3 and SpinOff3 > SpinOff1 and SpinOff3 > SpinOff2):
      print('Player 3 is the winner, $10,000!')
      if (SpinOff3) == 100 :
        print('Your total prize is $11,000! Congratulations!')

您應該使用所需的輸入變量使您的代碼成為 function。 一旦你有了這樣的 function,在循環中運行並將每次運行的 output 存儲在列表中以供以后評估:

def my_game_function(var1, ..., varn):
    ...
    return output1, output2, ..., outputn

all_outputs = []
for i in range(10000):
    output = my_game_function(var1, ..., varn)
    all_outputs.extend(output)
    # if your function outputs a single value, use append
    # all_outputs.append(output)

這是一款類似的游戲,規則較少,只取所有角色中的最高分,並根據玩家獲得的分數獎勵該玩家,並跟蹤每個分數和勝利。

這是完整的代碼,但我建議您將 function 復制 function 並測試實際在做什么,並了解如何拆分復雜的任務。

import random

class Player:
    id = None
    n_wins = 0
    n_score = 0

class Game:

    n_plays = 0

    def score_value(self):
        if self.winner_score == 100:
            value = 10000
        elif self.winner_score >= 75:
            value = 1000
        else:
            value = 100
        return value

    def print_score(self):
        print('Game {0}, player {1} won ${2}!'.format(
            self.n_plays, self.winner_id + 1, self.score_value())
        )

    def play_for(self, n, show=True):
        for i in range(n):
            self.make_play()
            if show:
                self.print_score()

    def play(self):
        self.make_play()
        self.print_score()

    def make_play(self):
        play_res = []
        for i in range(self.n_player):
            play_res.append(random.randint(1, 20) * 5)

        winer = play_res.index(max(play_res))
        self.players[winer].n_wins += 1

        for i in range(self.n_player):
            self.players[i].n_score += play_res[i]

        self.winner_id = self.players[winer].id
        self.winner_score = max(play_res)
        self.n_plays += 1

    def add_players(self, n_players):
        self.n_player = n_players
        for i in range(n_players):
            self.players.append(Player())
            self.players[i].id = i

    def get_player(self, index):
        return self.players[index - 1]

    def __init__(self):
        self.players = []
        self.n_player = 0

        self.winner_id = 0
        self.winner_score = 0

if __name__ == '__main__':
    game = Game()
    game.add_players(3)
    game.play()
    game.play_for(9999, show=False)
    game.play_for(5)

    player_2 = game.get_player(2)
    print('Player 2 won {0} times and has a score of {1}'.format(
        player_2.n_wins, player_2.n_score)
    )


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM