2016-05-08 14 views
1

フットボールチームのプレイヤーのトレーニング番号を変更しようとしましたが、これを達成するためにforループを使用しようとすると、 、ここでは、プログラムに関連するすべてのコードがリスト内の各オブジェクトの属性をPython用に変更する

import random 
import time 
from operator import itemgetter 


# class that creates each player and manipulates the data associated with the players which is first set here 
class NewPlayer: 
    def __init__(self): 
     # creates a placeholder name for the player then uses the function (create_name) within to fill with a string 
     self.name = "" 
     self.create_name() 
     # creates the initial rating of a player between and including 10 and 20 
     self.rating = random.randint(10, 20) 
     # the amount of times a player has been trained, unique to each instance 
     self.training_number = 0 

    def train_player(self): 
     # if the player gets trained they will get a random rating increase/decrease depending on chance and work level 
     if -1 < self.training_number < 1: 
      self.rating += random.randint(1, 5) 
      # there cant be over excessive training so the more (work) training then the worse the modifier gets as it 
      # takes a different route based on training_number 
      self.training_number += 1 
      print("Training was successful") 
     # if the player gets retrained they use the following route and end up potentially worse 
     elif 1 <= self.training_number < 2: 
      # temporarily store the old value 
      old = self.rating 
      self.rating += random.randint(-2, 2) 
      self.training_number += 1 
      # the old value is used to compare and give the correct output, relaying the outcome 
      if self.rating > old: 
       print("Training was successful") 
      elif self.rating == old: 
       print("Nothing new was achieved in training") 
      else: 
       print("The training was terrible") 
     # by this point the player won't receive a positive boost 
     else: 
      self.rating += random.randint(-5, -1) 
      self.training_number += 1 
      print("The player is too tired and picked up a slight injury") 

    def create_name(self): 
     forenames = ["Peter", "Steven", "Michael", "Ishaa", "Nikesh", "George", "Toby", "Jasdeep"] 
     surnames = ["Chandler", "Evans", "Kelly", "Ward", "Heer", "Trepass", "Gardner", "Walker"] 
     # randomly creates a name for a player 
     self.name = random.choice(forenames) + " " + random.choice(surnames) 


# class that creates opposition teams that are made up of 11 players 
class NewTeam: 
    def __init__(self): 
     # variable that holds the team name, currently a placeholder 
     self.team_name = "" 
     # variable that holds how many points the team has earned this season, number value += 3 when a match is won, 
     # 1 when drew and none when lost 
     self.points = 0 
     # variable that holds how many upgrade points the opposition earns 
     self.upgrade_points = 0 
     # creates the players and the team name for the opposition teams 
     self.create_team() 
     self.create_name() 

    # adds 11 players to any team using the function 
    def create_team(self): 
     self.team = [] 
     # adds a goalkeeper to the team 
     self.team.append(("goalkeeper1:", NewPlayer())) 
     # adds four defenders to the team 
     for i in range(1, 5): 
      self.team.append(("defender" + str(i) + ":", NewPlayer())) 
     # adds four midfielders to the team 
     for i in range(1, 5): 
      self.team.append(("midfielder" + str(i) + ":", NewPlayer())) 
     # adds two attackers to the team 
     for i in range(1, 3): 
      self.team.append(("attacker" + str(i) + ":", NewPlayer())) 

    # chooses a random name for the team 
    def create_name(self): 
     names = ["Birmingham City", "Barnsley", "Accrington Stanley", "Blackburn Rovers", "Blackpool", "AFC Wimbledon", 
       "Bolton Wanderers", "Bradford City", "Barnet", "Bristol City", "Burton Albion", "Bristol Rovers", 
       "Brentford", "Bury", "Cambridge United", "Brighton", "Hove Albion", "Chesterfield", "Carlisle United", 
       "Burnley", "Colchester United", "Crawley Town", "Cardiff City", "Coventry City", "Dagenham", 
       "Redbridge", "Charlton Athletic", "Crewe Alexandra", "Exeter City", "Derby County", "Doncaster Rovers", 
       "Hartlepool United", "Fulham", "Fleetwood Town", "Leyton Orient", "Huddersfield Town", "Gillingham", 
       "Luton Town", "Hull City", "Millwall", "Mansfield Town", "Ipswich Town", "Oldham Athletic", 
       "Morecambe", "Leeds United", "Peterborough United", "Newport County", "Middlesbrough", "Port Vale", 
       "Northampton Town", "Milton Keynes", "Notts County", "Nottingham Forest", "Scunthorpe United", 
       "Oxford United", "Preston North End", "Sheffield United", "Plymouth Argyle", "Queens Park Rangers", 
       "Shrewsbury Town", "Portsmouth", "Reading", "Southend", "United Stevenage", "Rotherham United", 
       "Swindon Town", "Wycombe Wanderers", "Sheffield Wednesday", "Walsall", "Yeovil Town", 
       "Wolverhampton Wanderers", "Wigan Athletic", "York City"] 
     self.team_name = random.choice(names) 


# controls the whole game, incorporates all functions and classes 
class Game: 
    def __init__(self): 
     rebel = True 
     # creates the users team 
     self.my_team = NewTeam() 
     # allows user customisation of team name 
     while rebel: 
      self.my_team.team_name = input("What is your teams name? ") 
      if len(self.my_team.team_name) >= 50: 
       rebel = True 
       print("Too many characters in your team name, please choose one with less than 50 characters - ") 
      else: 
       rebel = False 
     # creating the other teams, using the NewTeam class 
     self.team1 = NewTeam() 
     self.team2 = NewTeam() 
     self.team3 = NewTeam() 
     self.team4 = NewTeam() 
     self.team5 = NewTeam() 
     self.team6 = NewTeam() 
     self.team7 = NewTeam() 
     self.team8 = NewTeam() 
     self.team9 = NewTeam() 
     # an array to hold all the other teams and keep it neat 
     self.other_teams = [self.team1, self.team2, self.team3, self.team4, self.team5, self.team6, self.team7, 
          self.team8, self.team9] 
     # variable to hold which teams will be facing each other, for later printing in one go 
     self.matches = [] 

    def match(self): 
     # creates the five different matches randomly 
     teams = self.other_teams 
     match1 = [self.my_team] 
     match2 = [] 
     match3 = [] 
     match4 = [] 
     match5 = [] 
     random_team = random.choice(teams) 
     teams.remove(random_team) 
     match1.append(random_team) 
     for i in range(2): 
      random_team = random.choice(teams) 
      teams.remove(random_team) 
      match2.append(random_team) 
     for i in range(2): 
      random_team = random.choice(teams) 
      teams.remove(random_team) 
      match3.append(random_team) 
     for i in range(2): 
      random_team = random.choice(teams) 
      teams.remove(random_team) 
      match4.append(random_team) 
     for i in range(2): 
      random_team = random.choice(teams) 
      teams.remove(random_team) 
      match5.append(random_team) 
     # resets the other teams variable as the teams where removed from before 
     self.other_teams = [self.team1, self.team2, self.team3, self.team4, self.team5, self.team6, self.team7, 
          self.team8, self.team9] 
     # assigns the list of matches 
     self.matches = [match1, match2, match3, match4, match5] 
     print("") 
     print("") 
     # prints who will be playing who 
     for match in self.matches: 
      print(match[0].team_name + " will be playing " + match[1].team_name) 
      time.sleep(1) 
     print("") 
     print("") 

    def process_turn(self): 
     # if the player has upgrade points the game asks if they would like to upgrade their players 
     while True: 
      player_upgrade = input("Would you like to upgrade your players? Enter yes or no - ") 
      if player_upgrade == "yes": 
       self.upgrade_player() 
       break 
      if player_upgrade == "no": 
       print("") 
       print("") 
       break 
      else: 
       print("That is not a valid input. Try again") 
       print("") 
     # the player can either start a new match or view the rankings of the teams 
     print("Enter \"rankings\" to see the league tables") 
     print("Enter \"new match\" to play the next match") 
     while True: 
      next_input = input("What would you like to do? ") 
      if next_input == "rankings": 
       self.rankings() 
       break 
      elif next_input == "new match": 
       self.match() 
       self.process_matches() 
       for object in self.my_team.team: 
        object.training_number = 0 
       break 
      else: 
       print("That is not a valid input... Try again") 

    def rankings(self): 
     print("") 
     print("") 
     # creates all the teams by adding your team to the other teams 
     all_teams = [self.my_team] + self.other_teams 
     # creates ranked_teams where the team name and the teams score are sorted into order 
     ranked_teams = [] 
     for team in all_teams: 
      ranked_teams.append((team.points, team.team_name)) 
     # sorts the scores into order 
     ranked_teams = sorted(ranked_teams, key=itemgetter(0), reverse=True) 
     # fails to keep that order with secondary key sort 
     # ranked_teams = sorted(ranked_teams, key=itemgetter(1)) 
     # prints the teams in order of their scores to create the table 
     counter = 1 
     for team in ranked_teams: 
      print(team[1] + " is in " + str(counter) + " place with " + str(team[0]) + " points") 
      counter += 1 
     print("") 
     print("") 

    def upgrade_player(self): 
     # prints the players and their score so you can see who to upgrade 
     for player in self.my_team.team: 
      print(player[0] + ": " + player[1].name + " has a rating of: " + str(player[1].rating)) 
     print("") 
     print("") 
     # loop so that all upgrade points may be used 
     while True: 
      # tells the player how many points they have left and asks which player they would like to upgrade 
      print("Who would you like to upgrade?") 
      upgrading_input = input("Enter player position to upgrade or type 'exit' to quit upgrading your players - ") 
      print("") 
      # each statement checks if a player has been selected for training 
      # if they have then the score is copied of later use 
      # then the player is trained 
      # then the difference in score is printed 
      # the amount of points available to use is decreased 
      if upgrading_input == "goalkeeper1": 
       prev_rating = self.my_team.team[0][1].rating 
       self.my_team.team[0][1].train_player() 
       print(self.my_team.team[0][0] + ": " + self.my_team.team[0][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[0][1].rating)) 
       print(self.my_team.team[0][1].name + " has trained " + str(self.my_team.team[0][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "defender1": 
       prev_rating = self.my_team.team[1][1].rating 
       self.my_team.team[1][1].train_player() 
       print(self.my_team.team[1][0] + ": " + self.my_team.team[1][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[1][1].rating)) 
       print(self.my_team.team[1][1].name + " has trained " + str(self.my_team.team[1][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "defender2": 
       prev_rating = self.my_team.team[2][1].rating 
       self.my_team.team[2][1].train_player() 
       print(self.my_team.team[2][0] + ": " + self.my_team.team[2][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[1][1].rating)) 
       print(self.my_team.team[2][1].name + " has trained " + str(self.my_team.team[0][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "defender3": 
       prev_rating = self.my_team.team[3][1].rating 
       self.my_team.team[3][1].train_player() 
       print(self.my_team.team[3][0] + ": " + self.my_team.team[3][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[3][1].rating)) 
       print(self.my_team.team[3][1].name + " has trained " + str(self.my_team.team[3][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "defender4": 
       prev_rating = self.my_team.team[4][1].rating 
       self.my_team.team[4][1].train_player() 
       print(self.my_team.team[4][0] + ": " + self.my_team.team[4][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[4][1].rating)) 
       print(self.my_team.team[4][1].name + " has trained " + str(self.my_team.team[4][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "midfielder1": 
       prev_rating = self.my_team.team[5][1].rating 
       self.my_team.team[5][1].train_player() 
       print(self.my_team.team[5][0] + ": " + self.my_team.team[5][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[5][1].rating)) 
       print(self.my_team.team[5][1].name + " has trained " + str(self.my_team.team[5][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "midfielder2": 
       prev_rating = self.my_team.team[6][1].rating 
       self.my_team.team[6][1].train_player() 
       print(self.my_team.team[6][0] + ": " + self.my_team.team[6][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[6][1].rating)) 
       print(self.my_team.team[6][1].name + " has trained " + str(self.my_team.team[6][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "midfielder3": 
       prev_rating = self.my_team.team[7][1].rating 
       self.my_team.team[7][1].train_player() 
       print(self.my_team.team[7][0] + ": " + self.my_team.team[7][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[7][1].rating)) 
       print(self.my_team.team[7][1].name + " has trained " + str(self.my_team.team[7][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "midfielder4": 
       prev_rating = self.my_team.team[8][1].rating 
       self.my_team.team[8][1].train_player() 
       print(self.my_team.team[8][0] + ": " + self.my_team.team[8][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[8][1].rating)) 
       print(self.my_team.team[8][1].name + " has trained " + str(self.my_team.team[8][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "attacker1": 
       prev_rating = self.my_team.team[9][1].rating 
       self.my_team.team[9][1].train_player() 
       print(self.my_team.team[9][0] + ": " + self.my_team.team[9][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[9][1].rating)) 
       print(self.my_team.team[9][1].name + " has trained " + str(self.my_team.team[9][1].training_number) + 
         " time(s)") 

      elif upgrading_input == "attacker2": 
       prev_rating = self.my_team.team[10][1].rating 
       self.my_team.team[10][1].train_player() 
       print(self.my_team.team[10][0] + ": " + self.my_team.team[10][1].name + " had a rating of " + 
         str(prev_rating) + " and now has rating of " + str(self.my_team.team[10][1].rating)) 
       print(self.my_team.team[10][1].name + " has trained " + str(self.my_team.team[10][1].training_number) + 
         " time(s)") 
      # if the input was exit then upgrading of players stops 
      elif upgrading_input == "exit": 
       print("quiting") 
       break 
      # if the input was incorrect then it asks the input again 
      else: 
       print("That was an incorrect input. Try again") 
      print("") 

    def process_matches(self): 
     # for each match the game decides who wins based on player ratings 
     for match in self.matches: 
      # obtains the rating of the two teams 
      rating1 = 0 
      rating2 = 0 
      for player in match[0].team: 
       rating1 += player[1].rating 
      for player in match[1].team: 
       rating2 += player[1].rating 
      rating1 += random.randint(-10, 10) 
      rating2 += random.randint(-10, 10) 
      draw = random.randint(1, 20) 
      # compares the rating of the two teams 
      if draw == 20: 
       rating1 = rating2 
      else: 
       pass 
      if rating1 == rating2: 
       print(match[0].team_name + " has drew against " + match[1].team_name) 
       match[0].points += 1 
       match[1].points += 1 
       match[0].upgrade_points += 1 
       match[1].upgrade_points += 1 

      elif rating1 > rating2: 
       print(match[0].team_name + " has won against " + match[1].team_name) 
       match[0].points += 3 
       match[0].upgrade_points += 3 

      elif rating2 > rating1: 
       print(match[1].team_name + " has won against " + match[0].team_name) 
       match[1].points += 3 
       match[1].upgrade_points += 3 
      time.sleep(1) 
      """need to figure out how the next 2 lines work, namely, how the fuck does the instance have the 
      training_number attribute yet i can't get hold of and then change it""" 
      # for player in self.my_team.team: 
      #  player.training_number = 0 
     # the other teams players gets randomly upgraded if they have enough points 
     for team in self.other_teams: 
      for points in range(team.upgrade_points): 
       player = random.choice(team.team) 
       player[1].train_player() 
       team.upgrade_points -= 1 
     print("") 
     print("") 

game = Game() 
while True: 
    game.process_turn() 

# I need to fix the training number, the class has a value of 0, but, the instances/players have their own value 
# accrued which will hopefully be reset to 0 after each match played, but hard to do 

# need a end game, a simple way of after 10 matches or something it ends season and starts again, your team stays the 
# same all others are remade and randomised again 

...(あなたは多分私はとのトラブルを抱えている部分が表示されます部分にそれをスキップすることができます)である私の問題は、そこの考え方にありオーバートレーニングに有害な影響を与えるので、このコードは混乱し、おそらくもっと効率的になることがありますが、時には混乱することもあります。試合後(1週間に相当)、すべてのプレイヤーが0にリセットされ、練習できるようになりますもう一度...だから私は単数のケースで値を変更することができることを確認したが、そうすることができたが、ファンクションで試合が行われた直後に値を変更しようとすると、それはちょうど見えない仕事には、私はここで多くの例のオンラインと他の質問を見てみたが、私が見つけたと思われる最も近いのはthisです。しかし、ここで私は、この部分に含まれるループのために取り組むしようとして立ち往生しています...

while True: 
      next_input = input("What would you like to do? ") 
      if next_input == "rankings": 
       self.rankings() 
       break 
      elif next_input == "new match": 
       self.match() 
       self.process_matches() 
       **for object in self.my_team.team: 
        object.training_number = 0** 
       break 
      else: 
       print("That is not a valid input... Try again") 
+0

@techrafおかげで、私はそれを見て、一体何が起こったのか –

+4

はあなたの問題を再現する最小限のコードを提供することができます不思議ましたか? –

+1

あなたの現在のコードで何が間違っていますか、それは妥当かと思いますか?また、私たちがあなたを助けてくれるようにしたい場合は、あなたが達成しようとしているものの短い実行可能な例を提供する必要があります。 –

答えて

0

あなたのチームのプロパティのタイプは次のとおりです。[文字列* NewPlayer]

あなたは、配列を反復し、しようとしていますあなたのタプルのtraining_numberプロパティを設定するだけで意味がありません。

タプル内のNewPlayerオブジェクトにtraining_numberプロパティを設定する場合は、次のようにします。問題は、タプルが不変なので、これを行うことができないということです。

for object in self.my_team.team: 
    object1[1].training_number = 0 

代わりにあなたが辞書{文字列:NewPerson}であるためにあなたのチームを変更することができ

for role,person in self.my_team.team.iteritems(): 
    person.training_number = 0 

そして、あなたのチームのクラスであなたの代わりに辞書を使用するようにcreate_team機能を変更する必要があると思いますリスト。

self.team = {} 
self.team["goalkeeper1"] = NewPlayer() 
... 
+0

ありがとう、私はコードが嫌なことを知っていますが、短時間で使用されることを意図して、文字列* NewPlayerの問題を見ましたが、どのようにしてそれをすべて置き換えることができる変数にすることができないのか分かりませんでしたNPCのチームに同じ機能を使用することができます。 –

+0

あなたは伝説です –

関連する問題