简体   繁体   English

如何运行我的代码 10,000 次并跟踪每个玩家获胜的次数?

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

I am trying to make my code run 10,000 times.我试图让我的代码运行 10,000 次。 it needs to run 10,000 times.它需要运行 10,000 次。 Keep track of how many times each player wins, how many times each player scores the bonus, and how many times all three players earned the bonus.记录每位玩家获胜的次数、每位玩家获得奖金的次数以及所有三名玩家获得奖金的次数。 I have no clue how to get this to work and have been trying to figure it out.我不知道如何让它工作并且一直试图弄清楚。 can anyone show me how to do this?谁能告诉我如何做到这一点? I will post the code below.我将在下面发布代码。

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

You should make your code a function with the required input variables.您应该使用所需的输入变量使您的代码成为 function。 Once you have such function, run in in a loop and store the output of each run in a list for later evaluation:一旦你有了这样的 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)

This one is a similar game, with less rules, just takes the highest score of all the roles and rewards that player with a value depending on the score they get and keeps track of every score and win.这是一款类似的游戏,规则较少,只取所有角色中的最高分,并根据玩家获得的分数奖励该玩家,并跟踪每个分数和胜利。

This is the complete code but i will suggest you to copy function by function and testing what is actually doing and get an idea of how to split a complex task.这是完整的代码,但我建议您将 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.

相关问题 如何使用 python 使这个骰子游戏在模拟中运行 10,000 次? - How do I make this dice game run in a simulation 10,000 times using python? 井字游戏的python代码,但需要添加一个可见的记分牌,显示玩家X和O获胜的次数以及平局的次数 - python code for tic tac toe, but need to add a visible scoreboard that shows how many times player X and O wins, and how many ties 如何使用随机模块 10,000 次并将结果作为键和值添加到字典中 - How to use the random module 10,000 times and add results to a dictionary as key and values 我如何跟踪一个数字滚动了多少次? - how do i keep track of how many times a number is rolled? 如何跟踪特定条件发生的次数 - How to keep track of how many times a unique condition occurs 滚动 4 个骰子 10,000 次的打印结果 - Printing outcomes for rolling 4 dice 10,000 times 如何跟踪python脚本的先前运行时间 - How to keep track of previous run times for a python script 获取后如何在for循环中每10,000条记录后创建多个文件? - How to create multiple files after each 10,000 records in for loop after fetch? 这个递归函数如何跟踪它已经执行了多少次? - How does this recursive function keep track of how many times it has been executed? 我如何跟踪在 discord 中输入了多少次命令 - How can i keep track of how many times has a command been typed in discord
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM