patternpythonMinor
Football game simulation
Viewed 0 times
gamefootballsimulation
Problem
I'm working on a text-based football simulation game along the lines of Football Simulator. Below is a subset of my total code, specifically the functions used to create a new player. I also have functions (not shown) to create a new coach, create the teams, create the weekly schedules, etc. I'm hoping to be able to use the feedback I get here to improve those sections as well.
Before anyone suggests storing the data in a database, I started out that way, but ending up opting for dictionaries/lists instead for several reasons, so please try to look past that.
Anyway, here goes. The biggest thing I'm struggling with is having to pass a list (
EDIT: I made a mistake in my original post, I use player_id_index to keep track of how many players have been created, so that the next time I call
```
# python3
import csv
from random import choice, randint, gauss
def create_names_first_data():
'''
create a list of all possible first names using text file as source data
'''
first_names = []
filename_first = 'resources/names_first.txt'
with open(filename_first, 'r') as file_to_open:
for line in file_to_open:
data = line.split()
new_name = data[0]
first_names.append(new_name)
return first_names
def create_names_last_data():
'''
create a list of all possible last names using text file as source data
'''
last_names = []
filename_last = 'resources/names_la
Before anyone suggests storing the data in a database, I started out that way, but ending up opting for dictionaries/lists instead for several reasons, so please try to look past that.
Anyway, here goes. The biggest thing I'm struggling with is having to pass a list (
person_data) of all the parameters needed by create_new_player. I don't feel it's efficient to have to build up a list before calling the function, pass it, then have to deconstruct it inside the function. I know using global variables isn't recommended, so I'm not sure if there are any other options. I have to do similar things (albeit using a list of different parameters) for my other functions. I appreciate all feedback you may have.EDIT: I made a mistake in my original post, I use player_id_index to keep track of how many players have been created, so that the next time I call
create_new_player it starts where the previous one left off, even though it's not shown below.```
# python3
import csv
from random import choice, randint, gauss
def create_names_first_data():
'''
create a list of all possible first names using text file as source data
'''
first_names = []
filename_first = 'resources/names_first.txt'
with open(filename_first, 'r') as file_to_open:
for line in file_to_open:
data = line.split()
new_name = data[0]
first_names.append(new_name)
return first_names
def create_names_last_data():
'''
create a list of all possible last names using text file as source data
'''
last_names = []
filename_last = 'resources/names_la
Solution
You don't need to define and increment the
Just do:
Or reduce it all to this dict comprehension:
Unpack the tuple in the head of this for loop to make it more readable:
Python 3.6 offers you the new
In
Regarding the global variables
player_id_index variable outside of the for loop:player_id_index = 0
player_dict = {}
for _ in range(5000):
player_id_index += 1
new_player = create_new_player(person_data)
player_dict[player_id_index] = new_playerJust do:
for player_id_index in range(1, 5000):
player_dict[player_id_index] = create_new_player(person_data)Or reduce it all to this dict comprehension:
player_dict = {id_: create_new_player(id_) for id_ in range(1, 5000)}Unpack the tuple in the head of this for loop to make it more readable:
for i in zip(positions_list, probability_list):
position_probabilities.extend([i[0]] * i[1])
for position, probability in zip(positions_list, probability_list):
position_probabilities.extend([position] * probability )Python 3.6 offers you the new
random.choices function:positions_list = ['QB', 'RB', 'WR', 'TE', 'OL', 'DL', 'LB', 'DB', 'P', 'K']
probability_list = [5, 6, 10, 5, 15, 15, 12, 13, 2, 2]
position = random.choices(positions_list, probability_list, k=1)[0]In
create_states_data I think this would look a bit more readable (you could also use a dict comprehension here, but it'd look a bit dense):for i in reader:
states[i['state']] = {'abbreviation': i['abbreviation'],
'population': i['population'],
'towns': {}}Regarding the global variables
first_names_list, last_names_list etc., since you don't mutate them later, they are global constants. You can just access them in the create_character function and don't have to pass them. The convention is to use uppercase names for constants to signal to other programmers that they mustn't be changed, e.g. FIRST_NAMES_LIST.Code Snippets
player_id_index = 0
player_dict = {}
for _ in range(5000):
player_id_index += 1
new_player = create_new_player(person_data)
player_dict[player_id_index] = new_playerfor player_id_index in range(1, 5000):
player_dict[player_id_index] = create_new_player(person_data)player_dict = {id_: create_new_player(id_) for id_ in range(1, 5000)}for i in zip(positions_list, probability_list):
position_probabilities.extend([i[0]] * i[1])
for position, probability in zip(positions_list, probability_list):
position_probabilities.extend([position] * probability )positions_list = ['QB', 'RB', 'WR', 'TE', 'OL', 'DL', 'LB', 'DB', 'P', 'K']
probability_list = [5, 6, 10, 5, 15, 15, 12, 13, 2, 2]
position = random.choices(positions_list, probability_list, k=1)[0]Context
StackExchange Code Review Q#159360, answer score: 3
Revisions (0)
No revisions yet.