Retrieve attributes definitions in __init__()

TODO pylint warning:
map.py:17: [R0902(too-many-instance-attributes), Map] Too many
instance attributes (10/7)
This commit is contained in:
Fred Z 2018-03-30 12:38:07 +02:00
parent a01fd836ab
commit f624d23a45
1 changed files with 61 additions and 65 deletions

126
map.py
View File

@ -29,7 +29,7 @@ class Map:
:var int status: move status (what append after a move command)
:var str status_message: feedback message for player
:var lst map_in_a_list: splited map in a list
:var lst splited_map: splited map in a list
:var str _map_in_a_string: map string
:var str _element_under_player: Element under player
:var int _player_position: Player index in _map_in_a_string
@ -39,14 +39,69 @@ class Map:
:return: None
"""
# Loading map file
if os.path.isfile(map_file) is True:
with open(map_file, "r") as map_data:
self.check_file(map_data.read().splitlines())
else:
if os.path.isfile(map_file) is False:
self.status = False
print(ERR_MAP.format(':'.join(["mapfile", map_file])))
else:
with open(map_file, "r") as map_data:
splited_map = map_data.read().splitlines()
self.check_file(splited_map)
# Builds a square map (if end-line spaces are missing in the file)
self._map_in_a_string = '\n'.join(
(self.check_line(line) for line in splited_map)
)
# Gets player initial position
self._player_position = self._map_in_a_string.find(
elmt_val('symbol', 'name', 'player', 0)
)
# Defines Element under player at start
self._element_under_player = elmt_val('symbol', 'name', 'void', 0)
# Place collectables on the map
for symbol_to_place in elmt_val('symbol', 'collect', True):
position = random.choice(
[idx for (idx, value) in enumerate(
self._map_in_a_string
) if value == elmt_val(
'symbol', 'name', 'void', 0
)])
self.place_element(symbol_to_place, pos=position)
self.MAX_ITEMS = sum(1 for _ in elmt_val('name', 'collect', True))
self._COLUM = MAZE_SIZE + 1 # List starts to zero
self._MAXIM = (self._COLUM * MAZE_SIZE) - 1
self.status = True
self.collected_items = []
self.collected_items_num = 0
self.status_message = {}
self.status_message['title'] = HEAD_MESSAGES['title']
self.status_message['status'] = HEAD_MESSAGES['status']
self.status_message['items'] = HEAD_MESSAGES['items'].format(
self.collected_items_num, self.MAX_ITEMS
)
def check_file(self, splited_map):
"""
Checks the map conformity before starting the game
:param list/str splited_map: Map splited in a list (line = index)
"""
self.status = False
if len(splited_map) != MAZE_SIZE:
print(ERR_MAP.format("maplines: " + str(len(splited_map))))
# ++Add other checks here: elements inside, exit possible, etc++
else:
self.status = True
def map_print(self):
""" Return a string of the map state """
return self._map_in_a_string
@ -76,9 +131,6 @@ class Map:
elif pressed_key == K_LEFT:
next_position = self._player_position - 1
# TODO nove instruction to __init__
self._element_under_player = elmt_val('symbol', 'name', 'void', 0)
# Next position treatment
if next_position >= 0 and next_position <= self._MAXIM:
next_char = self._map_in_a_string[next_position]
@ -151,62 +203,6 @@ class Map:
self._map_in_a_string = txt[:pos] + element + txt[pos + 1:]
def check_file(self, map_string):
"""
Checks the map conformity before starting the game
:param list/str map_string: One mazes line on each index
"""
# Map line number
if len(map_string) == MAZE_SIZE:
self._COLUM = MAZE_SIZE + 1
self._LINES = MAZE_SIZE
self._MAXIM = (self._COLUM * self._LINES) - 1
# ++ Add other map checks here ++
# Constructing square map
map_string = [self.check_line(line) for line in map_string]
self._map_in_a_string = '\n'.join(map_string)
# Player's initial position
self._player_position = self._map_in_a_string.find(
elmt_val('symbol', 'name', 'player', 0)
)
# Element under player at start
self._element_under_player = elmt_val(
'symbol', 'name', 'void', 0
)
# Place collectables on the map
for symbol_to_place in elmt_val('symbol', 'collect', True):
position = random.choice(
[idx for (idx, value) in enumerate(
self._map_in_a_string
) if value == elmt_val(
'symbol', 'name', 'void', 0
)])
self.place_element(symbol_to_place, pos=position)
self.status = True
self.collected_items = []
self.collected_items_num = 0
self.MAX_ITEMS = sum(
1 for _ in elmt_val('name', 'collect', True)
)
self.status_message = {}
self.status_message['title'] = HEAD_MESSAGES['title']
self.status_message['status'] = HEAD_MESSAGES['status']
self.status_message['items'] \
= HEAD_MESSAGES['items'].format(
self.collected_items_num, self.MAX_ITEMS
)
else:
self.status = False
print(ERR_MAP.format("maplines: " + str(len(map_string))))
@staticmethod
def check_line(line):
"""