From 71043085fca9188302085576069d3a9ffd35dcee Mon Sep 17 00:00:00 2001 From: Fred Z Date: Thu, 15 Feb 2018 11:00:12 +0100 Subject: [PATCH] DONE03, DONE04 & TODO11|TP_3.9|ROBOC - DONE03: suppression de __getattr__ (inutile & conflit avec pickle) - DONE08: ajout de la commande `quitter et sauvegarder` - TODO11: affiche la liste des commandes --- roboc/configuration.py | 5 ++++- roboc/map.py | 8 ------- roboc/roboc.py | 51 +++++++++++++++++++++++++----------------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/roboc/configuration.py b/roboc/configuration.py index 89fd739..2a45556 100644 --- a/roboc/configuration.py +++ b/roboc/configuration.py @@ -12,8 +12,10 @@ Ce fichier fait partie du projet `roboc` MAP_DIRECTORY = 'cartes/' # repertoire des fichiers carte MAP_EXTENTION = '.txt' # extention des fichiers carte -SAVED_GAME_FILENAME = '.backup' # fichier de sauvegarde +BACKUP_FILE = '.backup' # fichier de sauvegarde DIRECTIONS = ['N', 'S', 'E', 'O'] # commandes de deplacement +COMMANDS = {'quit': 'Q', # commandes d'interuption + 'help': 'H'} MAZE_ELEMENTS = {'wall': 'O', # elements dispo dans le labyrinthe 'door': '.', 'exit': 'U', @@ -32,6 +34,7 @@ ERR_UNKNOW = ERR_ + "personne n'est censé arriver ici…" MIN_MAP_SIDE = 3 MSG_DISCLAMER = "Bienvenue dans Roboc." +MSG_BACKUP = "Partie sauvegardée." MSG_AVAIBLE_MAP = "Cartes disponible: " MSG_CHOOSE_MAP = "Choississez un numéro de carte: " MSG_CHOOSE_MOVE = "Votre deplacement: " diff --git a/roboc/map.py b/roboc/map.py index 1c205b0..a8e1101 100644 --- a/roboc/map.py +++ b/roboc/map.py @@ -127,14 +127,6 @@ class Map: self.status = False self.status_message = ERR_MAP_FILE.format(map_file) - # TODO03 est-ce utile de conserver cette methode? - def __getattr__(self, name): - """ - Si un attribut manque a l'appel (_robo_position ou - _init_robo_position) - """ - return None - def map_print(self): """ Affiche la carte avec la position de jeu courante """ print(self._data_text) diff --git a/roboc/roboc.py b/roboc/roboc.py index bdd3ca0..d3517c6 100644 --- a/roboc/roboc.py +++ b/roboc/roboc.py @@ -17,9 +17,9 @@ Source: https://openclassrooms.com/courses/apprenez-a-programmer-en-python/exerc """ import os +import pickle from map import Map from configuration import * -# import pickle # DEBUT DU JEU @@ -85,32 +85,41 @@ while current_map.status: # choix du deplacement user_select_move = input(MSG_CHOOSE_MOVE).upper() - - # TODO08 quitter et sauvegarder - - # traitement du deplacement - move_status_id = current_map.move_to(user_select_move) cls() # clear screen - # TODO09 ranger les status dans un dict('ok': MSG_OK, …) - if MOVE_STATUS[move_status_id] == 'ok': - print('MSG_OK') + if user_select_move == COMMANDS['quit']: # quitter et sauvegarder + with open(BACKUP_FILE, 'wb') as backup_file: + pickle.Pickler(backup_file).dump(current_map) - elif MOVE_STATUS[move_status_id] == 'bad': - print('MSG_BAD') + # TODO12 afficher un recap des commandes dispo + # elif user_select_move == COMMANDS['help']: # Affiche les commandes + # print(COMMANDS, DIRECTIONS) - elif MOVE_STATUS[move_status_id] == 'wall': - print('MSG_WALL') - - elif MOVE_STATUS[move_status_id] == 'door': - print('MSG_DOOR') - - elif MOVE_STATUS[move_status_id] == 'exit': current_map.status = False - current_map.status_message = MSG_EXIT + current_map.status_message = MSG_BACKUP - else: # juste au cas ou… - raise NotImplementedError(ERR_UNKNOW) + else: # traitement du deplacement + move_status_id = current_map.move_to(user_select_move) + + # TODO09 ranger les status dans un dict('ok': MSG_OK, …) + if MOVE_STATUS[move_status_id] == 'ok': + print('MSG_OK') + + elif MOVE_STATUS[move_status_id] == 'bad': + print('MSG_BAD') + + elif MOVE_STATUS[move_status_id] == 'wall': + print('MSG_WALL') + + elif MOVE_STATUS[move_status_id] == 'door': + print('MSG_DOOR') + + elif MOVE_STATUS[move_status_id] == 'exit': + current_map.status = False + current_map.status_message = MSG_EXIT + + else: # juste au cas ou… + raise NotImplementedError(ERR_UNKNOW) # TODO10 rester dans la boucle si la carte n'est pas conforme if current_map.status is False: