2018-02-06 16:59:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
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/
|
|
|
|
|
|
|
|
roboc
|
|
|
|
=====
|
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
Jeu permettant de controler un robot dans un labyrinthe.
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
Voir readme.md
|
2018-02-06 16:59:29 +00:00
|
|
|
"""
|
|
|
|
|
2018-02-08 08:54:21 +00:00
|
|
|
import os
|
2018-02-15 10:00:12 +00:00
|
|
|
import pickle
|
2018-02-18 09:19:58 +00:00
|
|
|
from configuration import BACKUP_FILE, choose_maps_menu, cls, COMMANDS, \
|
|
|
|
COMMANDS_LABEL, DIRECTIONS, DIRECTIONS_LABEL, get_msg_list, \
|
2018-02-21 23:45:13 +00:00
|
|
|
MAP_DIRECTORY, MAP_EXTENTION, MAPS_NAME_LIST, MOVE_STATUS, \
|
2018-02-18 09:19:58 +00:00
|
|
|
MOVE_STATUS_MSG, MSG_AVAIBLE_BACKUP, MSG_BACKUP_DONE, MSG_BACKUP_GAME, \
|
2018-02-15 10:00:12 +00:00
|
|
|
MSG_CHOOSE_MOVE, MSG_DISCLAMER, MSG_END_GAME, MSG_HELP, MSG_NO_YES, \
|
2018-02-21 23:45:13 +00:00
|
|
|
MSG_START_GAME, USER_SELECT_BACKUP
|
2018-02-06 16:59:29 +00:00
|
|
|
|
|
|
|
# DEBUT DU JEU
|
|
|
|
|
2018-02-08 08:54:21 +00:00
|
|
|
# Recuperation de la liste des cartes
|
|
|
|
try:
|
2018-02-21 23:45:13 +00:00
|
|
|
MAPS_AVAIBLE = os.listdir(MAP_DIRECTORY)
|
2018-02-08 08:54:21 +00:00
|
|
|
except FileNotFoundError as except_detail:
|
|
|
|
print("FileNotFoundError: «{}»".format(except_detail))
|
|
|
|
else:
|
2018-02-21 23:45:13 +00:00
|
|
|
for map_file in MAPS_AVAIBLE:
|
2018-02-08 08:54:21 +00:00
|
|
|
filename_len = len(map_file) - len(MAP_EXTENTION)
|
|
|
|
|
|
|
|
# garde les fichiers avec la bonne extention
|
|
|
|
if map_file[filename_len:] == MAP_EXTENTION:
|
2018-02-21 23:45:13 +00:00
|
|
|
MAPS_NAME_LIST.append(map_file[: filename_len])
|
2018-02-08 08:54:21 +00:00
|
|
|
|
2018-02-15 10:00:12 +00:00
|
|
|
# Affichage du debut de partie
|
2018-02-18 09:19:58 +00:00
|
|
|
cls() # vide l'ecran de la console
|
2018-02-08 08:54:21 +00:00
|
|
|
print(MSG_DISCLAMER)
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-15 10:00:12 +00:00
|
|
|
# Verif si un fichier de sauvegarde est dispo
|
|
|
|
if os.path.isfile(BACKUP_FILE) is True:
|
|
|
|
with open(BACKUP_FILE, 'rb') as backup_file:
|
2018-02-21 23:45:13 +00:00
|
|
|
BACKUP_MAP = pickle.Unpickler(backup_file).load()
|
2018-02-15 10:00:12 +00:00
|
|
|
|
|
|
|
# On demande si l'utilisateur veut charger la sauvegarde
|
2018-02-21 23:45:13 +00:00
|
|
|
while USER_SELECT_BACKUP not in MSG_NO_YES:
|
|
|
|
USER_SELECT_BACKUP = input(
|
|
|
|
MSG_AVAIBLE_BACKUP.format(*MSG_NO_YES)
|
|
|
|
).lower()
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-15 10:00:12 +00:00
|
|
|
# utilisateur veut la sauvegarde
|
2018-02-21 23:45:13 +00:00
|
|
|
if USER_SELECT_BACKUP == MSG_NO_YES[1]:
|
|
|
|
CURRENT_MAP = BACKUP_MAP
|
2018-02-15 10:00:12 +00:00
|
|
|
else:
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP = choose_maps_menu()
|
2018-02-15 10:00:12 +00:00
|
|
|
else:
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP = choose_maps_menu()
|
2018-02-09 23:29:04 +00:00
|
|
|
|
2018-02-12 21:34:39 +00:00
|
|
|
|
|
|
|
# DEBUT DE BOUCLE DE TOUR DE JEU
|
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
# Affichage de la carte tant que status == True
|
2018-02-21 23:45:13 +00:00
|
|
|
while CURRENT_MAP.status:
|
2018-02-18 09:19:58 +00:00
|
|
|
# Affiche la carte et le message
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.map_print()
|
|
|
|
print(CURRENT_MAP.status_message)
|
2018-02-12 15:01:31 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
# Demande a l'utilisateur son choix du deplacement
|
|
|
|
user_move = input(MSG_CHOOSE_MOVE.format(
|
2018-02-17 17:54:12 +00:00
|
|
|
COMMANDS[1], COMMANDS_LABEL[1])
|
2018-02-21 23:45:13 +00:00
|
|
|
).upper()
|
2018-02-18 09:19:58 +00:00
|
|
|
cls() # vide l'ecran de la console
|
2018-02-14 17:21:37 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
# Traitement de la commande utilisateur
|
2018-02-18 23:48:58 +00:00
|
|
|
if user_move == COMMANDS[0]: # quitter
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status = False
|
2018-02-18 23:48:58 +00:00
|
|
|
|
|
|
|
# Le premier tour n'a pas ete joue, pas de sauvegarde faite
|
2018-02-21 23:45:13 +00:00
|
|
|
if CURRENT_MAP.status_message != MSG_START_GAME:
|
|
|
|
CURRENT_MAP.status_message = MSG_BACKUP_DONE
|
2018-02-18 23:48:58 +00:00
|
|
|
|
|
|
|
else:
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message = ""
|
2018-02-12 21:34:39 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
elif user_move == COMMANDS[1]: # Affiche l'aide
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message = MSG_HELP
|
2018-02-15 10:00:12 +00:00
|
|
|
# liste les directions
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message += get_msg_list(
|
2018-02-18 09:19:58 +00:00
|
|
|
DIRECTIONS, DIRECTIONS_LABEL
|
|
|
|
)
|
2018-02-15 10:00:12 +00:00
|
|
|
# liste les commandes
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message += get_msg_list(COMMANDS, COMMANDS_LABEL)
|
2018-02-15 10:00:12 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
else: # Traitement du deplacement
|
2018-02-21 23:45:13 +00:00
|
|
|
status = CURRENT_MAP.move_to(user_move)
|
2018-02-18 09:19:58 +00:00
|
|
|
message = MOVE_STATUS_MSG[MOVE_STATUS[status]].format(user_move)
|
2018-02-14 16:21:33 +00:00
|
|
|
|
2018-02-15 10:00:12 +00:00
|
|
|
# La sortie est atteinte, fin de la boucle
|
2018-02-18 09:19:58 +00:00
|
|
|
if MOVE_STATUS[status] == 'exit':
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status = False
|
|
|
|
CURRENT_MAP.status_message = message
|
2018-02-14 16:21:33 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
else: # sinon on sauvegarde la partie avant de refaire un tour
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message = MSG_BACKUP_GAME
|
2018-02-17 16:53:04 +00:00
|
|
|
with open(BACKUP_FILE, 'wb') as backup_file:
|
2018-02-21 23:45:13 +00:00
|
|
|
pickle.Pickler(backup_file).dump(CURRENT_MAP)
|
2018-02-17 16:53:04 +00:00
|
|
|
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.status_message = message
|
2018-02-18 08:19:01 +00:00
|
|
|
|
2018-02-18 09:19:58 +00:00
|
|
|
# fin de la boucle de tour
|
2018-02-18 08:19:01 +00:00
|
|
|
|
2018-02-21 23:45:13 +00:00
|
|
|
if CURRENT_MAP.status is False:
|
|
|
|
print(CURRENT_MAP.status_message)
|
2018-02-06 16:59:29 +00:00
|
|
|
|
2018-02-08 14:24:09 +00:00
|
|
|
# Fin de partie
|
2018-02-14 16:21:33 +00:00
|
|
|
print(MSG_END_GAME)
|
2018-02-21 23:45:13 +00:00
|
|
|
CURRENT_MAP.map_print()
|