2018-02-06 16:59:29 +00:00
|
|
|
"""
|
|
|
|
Author: freezed <freezed@users.noreply.github.com> 2018-02-06
|
|
|
|
Version: 0.1
|
|
|
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
Ce fichier fait partie du projet `roboc`
|
|
|
|
|
|
|
|
"""
|
2018-02-11 14:02:26 +00:00
|
|
|
|
2018-02-06 16:59:29 +00:00
|
|
|
import os
|
2018-02-13 14:18:57 +00:00
|
|
|
from configuration import *
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-10 06:54:54 +00:00
|
|
|
|
2018-02-06 16:59:29 +00:00
|
|
|
class Map:
|
|
|
|
"""
|
|
|
|
Classe gerant les cartes disponibles et la carte utilisee en cours
|
|
|
|
de partie.
|
|
|
|
|
|
|
|
:Example:
|
2018-02-12 16:08:03 +00:00
|
|
|
>>> EasyMap = Map("cartes/facile.txt")
|
2018-02-09 23:29:04 +00:00
|
|
|
>>> TestMap = Map("cartes/test.txt")
|
2018-02-13 16:19:16 +00:00
|
|
|
>>> MiniMap = Map("cartes/mini.txt")
|
2018-02-09 23:29:04 +00:00
|
|
|
>>> PrisonMap = Map("cartes/prison.txt")
|
2018-02-12 16:08:03 +00:00
|
|
|
>>> EmptyMap = Map("cartes/vide.txt")
|
|
|
|
>>> TooSmallMap = Map("cartes/trop_petite.txt")
|
|
|
|
>>> NoRoboMap = Map("cartes/sans_robo.txt")
|
|
|
|
|
|
|
|
>>> print(EmptyMap.status_message)
|
|
|
|
#!@?# Oups… carte «cartes/vide.txt», dimensions incorrecte: «0 x 0»
|
|
|
|
>>> print(TooSmallMap.status_message)
|
2018-02-13 14:59:02 +00:00
|
|
|
#!@?# Oups… carte «cartes/trop_petite.txt», dimensions incorrecte: «3 x 2»
|
2018-02-12 16:08:03 +00:00
|
|
|
>>> print(NoRoboMap.status_message)
|
|
|
|
#!@?# Oups… robo est introuvable sur la carte «cartes/sans_robo.txt»!
|
2018-02-09 23:29:04 +00:00
|
|
|
|
|
|
|
>>> print("_column_nb: {}".format(TestMap._column_nb))
|
2018-02-13 14:59:02 +00:00
|
|
|
_column_nb: 6
|
2018-02-09 23:29:04 +00:00
|
|
|
>>> print("_line_nb: {}".format(TestMap._line_nb))
|
|
|
|
_line_nb: 4
|
2018-02-12 14:39:42 +00:00
|
|
|
>>> print("_init_robo_position: {}".format(TestMap._init_robo_position))
|
2018-02-12 15:01:31 +00:00
|
|
|
_init_robo_position: 20
|
2018-02-10 21:38:43 +00:00
|
|
|
|
2018-02-13 14:59:02 +00:00
|
|
|
>>> TestMap.move_to("n3")
|
|
|
|
4
|
|
|
|
>>> TestMap.map_print()
|
|
|
|
01X34
|
|
|
|
abcde
|
|
|
|
ABCDE
|
|
|
|
zy wv
|
|
|
|
>>> TestMap.move_to("o2")
|
|
|
|
4
|
|
|
|
>>> TestMap.move_to("s3")
|
|
|
|
4
|
|
|
|
>>> TestMap.move_to("e4")
|
|
|
|
4
|
|
|
|
|
2018-02-13 16:19:16 +00:00
|
|
|
>>> MiniMap.move_to("o1")
|
|
|
|
1
|
|
|
|
>>> MiniMap.move_to("Z1")
|
|
|
|
0
|
|
|
|
>>> MiniMap.move_to("4")
|
|
|
|
0
|
2018-02-14 14:45:27 +00:00
|
|
|
>>> MiniMap.move_to("n")
|
|
|
|
0
|
2018-02-13 16:19:16 +00:00
|
|
|
>>> MiniMap.move_to("e1")
|
2018-02-14 14:54:20 +00:00
|
|
|
2
|
2018-02-13 16:19:16 +00:00
|
|
|
>>> MiniMap.map_print()
|
2018-02-14 14:32:08 +00:00
|
|
|
OOO
|
|
|
|
O X
|
|
|
|
OOO
|
2018-02-06 16:59:29 +00:00
|
|
|
"""
|
|
|
|
|
2018-02-14 14:32:08 +00:00
|
|
|
|
2018-02-09 15:11:31 +00:00
|
|
|
def __init__(self, map_file):
|
2018-02-06 16:59:29 +00:00
|
|
|
"""
|
2018-02-09 15:11:31 +00:00
|
|
|
Initialisation de la carte utilise
|
|
|
|
:param map_file:
|
2018-02-06 16:59:29 +00:00
|
|
|
"""
|
2018-02-09 15:11:31 +00:00
|
|
|
# Chargement du fichier carte choisi
|
|
|
|
if os.path.isfile(map_file) is True:
|
|
|
|
with open(map_file, "r") as map_data:
|
2018-02-12 15:01:31 +00:00
|
|
|
|
2018-02-10 06:54:54 +00:00
|
|
|
# contenu de la carte en texte
|
|
|
|
self._data_text = map_data.read()
|
2018-02-12 15:01:31 +00:00
|
|
|
|
2018-02-10 06:54:54 +00:00
|
|
|
# contenu de la carte ligne a ligne
|
2018-02-12 14:39:42 +00:00
|
|
|
map_data_list = self._data_text.splitlines()
|
2018-02-12 15:01:31 +00:00
|
|
|
|
2018-02-12 16:08:03 +00:00
|
|
|
# nbre de colonne de la carte (1ere ligne)
|
|
|
|
try:
|
2018-02-13 14:59:02 +00:00
|
|
|
self._column_nb = len(map_data_list[0]) +1
|
2018-02-12 16:08:03 +00:00
|
|
|
except IndexError:
|
|
|
|
self._column_nb = 0
|
|
|
|
|
|
|
|
# nbre de ligne de la carte
|
|
|
|
try:
|
|
|
|
self._line_nb = len(map_data_list)
|
|
|
|
except IndexError:
|
|
|
|
self._line_nb = 0
|
|
|
|
|
|
|
|
# positior du robot
|
|
|
|
self._robo_position = self._data_text.find(MAZE_ELEMENTS['robo'])
|
|
|
|
|
|
|
|
# Quelques controle sur la carte:
|
|
|
|
# - a t elle des dimensions mini?
|
|
|
|
if self._column_nb < MIN_MAP_SIDE or self._line_nb < MIN_MAP_SIDE:
|
|
|
|
self.status = False
|
|
|
|
self.status_message = ERR_MAP_SIZE.format(
|
|
|
|
map_file, self._column_nb, self._line_nb
|
|
|
|
)
|
|
|
|
|
|
|
|
# - a t elle un robo ( TODO : hors mur)?
|
|
|
|
elif self._robo_position == -1:
|
|
|
|
self.status = False
|
|
|
|
self.status_message = ERR_MAP_ROBO.format(map_file)
|
|
|
|
|
|
|
|
# - a t elle une sortie (dans mur) TODO?
|
|
|
|
# - a t elle du mur tout autour TODO?
|
|
|
|
else:
|
2018-02-13 14:59:02 +00:00
|
|
|
# TODO est-ce utile de concerver la pos initiale?
|
2018-02-12 16:08:03 +00:00
|
|
|
self._init_robo_position = self._robo_position
|
|
|
|
self.status = True
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-12 14:39:42 +00:00
|
|
|
# Erreur de chargement du fichier
|
2018-02-09 15:11:31 +00:00
|
|
|
else:
|
2018-02-12 14:39:42 +00:00
|
|
|
self.status = False
|
|
|
|
self.status_message = ERR_MAP_FILE.format(map_file)
|
|
|
|
|
2018-02-13 14:18:57 +00:00
|
|
|
# TODO est-ce utile de conserver cette methode?
|
2018-02-12 14:39:42 +00:00
|
|
|
def __getattr__(self, name):
|
|
|
|
"""
|
|
|
|
Si un attribut manque a l'appel (_robo_position ou
|
|
|
|
_init_robo_position)
|
|
|
|
"""
|
|
|
|
return None
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-09 15:36:48 +00:00
|
|
|
def map_print(self):
|
|
|
|
""" Affiche la carte avec la position de jeu courante """
|
2018-02-09 16:23:10 +00:00
|
|
|
print(self._data_text)
|
2018-02-09 15:36:48 +00:00
|
|
|
|
2018-02-12 21:34:39 +00:00
|
|
|
def move_to(self, move):
|
2018-02-09 15:36:48 +00:00
|
|
|
"""
|
|
|
|
Deplace le «robo» sur la carte
|
|
|
|
|
|
|
|
:param move: mouvement souhaite
|
2018-02-13 14:18:57 +00:00
|
|
|
:return: key in MOVE_STATUS
|
2018-02-09 15:36:48 +00:00
|
|
|
"""
|
2018-02-13 14:18:57 +00:00
|
|
|
# decompose le mouvement
|
2018-02-14 16:21:33 +00:00
|
|
|
try:
|
|
|
|
direction = move[0]
|
|
|
|
except IndexError as except_detail:
|
|
|
|
# print("IndexError: «{}»".format(except_detail))
|
|
|
|
return 0
|
|
|
|
|
2018-02-14 14:45:27 +00:00
|
|
|
try:
|
|
|
|
goal = int(move[1:])
|
|
|
|
except ValueError as except_detail:
|
|
|
|
# print("ValueError: «{}»".format(except_detail))
|
|
|
|
return 0
|
|
|
|
|
2018-02-13 14:59:02 +00:00
|
|
|
steps = 0
|
2018-02-12 21:34:39 +00:00
|
|
|
# direction non conforme
|
|
|
|
if direction not in DIRECTIONS:
|
2018-02-13 14:18:57 +00:00
|
|
|
move_status = 0
|
2018-02-12 21:34:39 +00:00
|
|
|
|
|
|
|
else:
|
2018-02-13 14:59:02 +00:00
|
|
|
# pour chaque pas on recupere la position suivante
|
2018-02-14 14:39:32 +00:00
|
|
|
while steps < goal:
|
2018-02-13 14:59:02 +00:00
|
|
|
steps += 1
|
|
|
|
if direction == DIRECTIONS[0]: # nord
|
|
|
|
next_position = self._robo_position - self._column_nb
|
|
|
|
|
|
|
|
elif direction == DIRECTIONS[1]: # sud
|
|
|
|
next_position = self._robo_position + self._column_nb
|
|
|
|
|
|
|
|
elif direction == DIRECTIONS[2]: # est
|
|
|
|
next_position = self._robo_position + 1
|
|
|
|
|
|
|
|
elif direction == DIRECTIONS[3]: # ouest
|
|
|
|
next_position = self._robo_position - 1
|
|
|
|
|
|
|
|
else: # juste au cas ou
|
|
|
|
raise NotImplementedError(ERR_)
|
|
|
|
|
|
|
|
# verifie quelle est la case du prochain pas
|
2018-02-14 14:54:20 +00:00
|
|
|
# import pdb; pdb.set_trace()
|
2018-02-13 14:59:02 +00:00
|
|
|
next_char = self._data_text[next_position]
|
|
|
|
if next_char == MAZE_ELEMENTS['wall']:
|
|
|
|
move_status = 1
|
2018-02-14 14:39:32 +00:00
|
|
|
steps = goal
|
2018-02-12 21:34:39 +00:00
|
|
|
|
|
|
|
elif next_char == MAZE_ELEMENTS['exit']:
|
2018-02-13 14:59:02 +00:00
|
|
|
self._robo_position = next_position
|
|
|
|
move_status = 2
|
2018-02-14 14:39:32 +00:00
|
|
|
steps = goal
|
|
|
|
|
2018-02-12 21:34:39 +00:00
|
|
|
|
2018-02-13 14:59:02 +00:00
|
|
|
elif next_char == MAZE_ELEMENTS['door']:
|
|
|
|
self._robo_position = next_position
|
|
|
|
move_status = 3
|
2018-02-14 14:39:32 +00:00
|
|
|
steps = goal
|
2018-02-12 21:34:39 +00:00
|
|
|
|
|
|
|
else:
|
2018-02-13 14:59:02 +00:00
|
|
|
self._robo_position = next_position
|
2018-02-14 14:39:32 +00:00
|
|
|
move_status = 4
|
2018-02-13 14:59:02 +00:00
|
|
|
|
|
|
|
# supprime le robo de son emplacement precedent
|
|
|
|
self._data_text = self._data_text.replace(
|
|
|
|
MAZE_ELEMENTS['robo'],
|
|
|
|
MAZE_ELEMENTS['trace']
|
|
|
|
)
|
|
|
|
# place le robo sur sa nouvelle position
|
|
|
|
self._data_text = self._data_text[:self._robo_position] + \
|
|
|
|
MAZE_ELEMENTS['robo'] + \
|
|
|
|
self._data_text[self._robo_position +1:]
|
2018-02-09 15:36:48 +00:00
|
|
|
|
2018-02-13 14:18:57 +00:00
|
|
|
return move_status
|
2018-02-09 15:36:48 +00:00
|
|
|
|
2018-02-13 14:59:02 +00:00
|
|
|
# def restore_backup(self, position):
|
|
|
|
# """ Charge une carte issue d'une sauvegarde """
|
2018-02-09 15:36:48 +00:00
|
|
|
|
|
|
|
|
2018-02-06 16:59:29 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
""" Starting doctests """
|
|
|
|
|
|
|
|
import doctest
|
2018-02-12 14:39:42 +00:00
|
|
|
doctest.testmod()
|