如何运行我的代码10000次,并跟踪每个玩家赢了多少次?

2024-10-04 01:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的代码运行10000次。它需要运行10000次。记录每个玩家赢了多少次,每个玩家获得奖金的次数,以及三个玩家共获得奖金的次数。我不知道如何使这项工作,并一直试图找出它。有人能告诉我怎么做吗?我将在下面发布代码

 #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!')

Tags: andifisrandomplayerprintrandintspin1
2条回答

这是一个类似的游戏,规则较少,只是在所有角色中获得最高分数,并根据玩家获得的分数奖励该玩家,并跟踪每个分数和胜利

这是完整的代码,但我建议您逐个复制函数,测试实际执行的操作,了解如何拆分复杂任务

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)
    )


您应该使代码成为具有所需输入变量的函数。一旦您有了这样的功能,在循环中运行,并将每次运行的输出存储在列表中,以供以后评估:

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)

相关问题 更多 >