def main(): """ Voici le début do mon programe pour jouer au labyrintheS""" # On charge les cartes existantes clear() cartes = [] for nom_fichier in os.listdir("cartes"): if nom_fichier.endswith(".txt"): chemin = os.path.join("cartes", nom_fichier) nom_carte = nom_fichier[:-3].lower() #charger la carte venant d'un fichier cartes.append(Carte.carte_from_file(chemin, nom_carte)) # On affiche les cartes existantes print("Labyrinthes existants :") for i, carte in enumerate(cartes): print(" {} - {}".format(i + 1, carte.nom)) #on Choisi la carte while True: resultat = input( "Entrez un numéro de labyrinthe pour commencer à jouer : ") if resultat.isdigit() == True: if int(resultat) > 0 and int(resultat) <= len(cartes): break clear() #charge la carte séléctionné carte = cartes[(int(resultat) - 1)] jeux = Labyrinthe(carte) #si une partie encours/ a été sauvé chemin = os.path.join("cartes", (carte.nom + "pre")) if os.path.exists(chemin): key = "" exp = r"^[O]|[N]$" reg = re.compile(exp) while reg.search(key) is None: key = (input("Voulez continer la partie précédente(O/N)") ).upper() or 'O' if key == 'O': clear() jeux = jeux.restaurer_labyrinthe() #Début du jeux jeux.start()
def main(): """Debut du serveur """ # Initialisation du serveur - Mise en place du socket : my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: my_socket.bind((HOST, PORT)) except socket.error: print("La liaison du socket à l'adresse choisie a échoué.") sys.exit() print("Serveur prêt, en attente de requêtes ...") my_socket.listen(5) #choisir la carte. # On charge les cartes existantes CLEAR() cartes = [] for nom_fichier in os.listdir("cartes"): if nom_fichier.endswith(".txt"): chemin = os.path.join("cartes", nom_fichier) nom_carte = nom_fichier[:-3].lower() #charger la carte venant d'un fichier cartes.append(Carte.carte_from_file(chemin, nom_carte)) # On affiche les cartes existantes print("Labyrinthes existants :") for i, carte in enumerate(cartes): print(" {} - {}".format(i + 1, carte.nom)) #on Choisi la carte while True: resultat = input( "Entrez un numéro de labyrinthe pour commencer à jouer : ") if resultat.isdigit(): if int(resultat) > 0 and int(resultat) <= len(cartes): break CLEAR() #charge la carte séléctionné carte = cartes[(int(resultat) - 1)] carte.clean_robot() jeux = Labyrinthe(carte) #si une partie encours/ a été sauvé chemin = os.path.join("cartes", (carte.nom + "pre")) if os.path.exists(chemin): key = "" exp = r"^[O]|[N]$" reg = re.compile(exp) while reg.search(key) is None: key = (input("Voulez continer la partie précédente(O/N)") ).upper() or 'O' if key == 'O': CLEAR() jeux = jeux.restaurer_labyrinthe() #Début du jeux # Attente et prise en charge des connexions demandées par les clients : print( "" " __ \n" + "/ _\ ___ _ __ __ __ ___ _ _ _ __ /\ /\ _ __ \n" + "\ \ / _ \| '__|\ \ / // _ \| | | || '__| / / \ \| '_ \ \n" + "_\ \| __/| | \ V /| __/| |_| || | \ \_/ /| |_) | \n" + "\__/ \___||_| \_/ \___| \__,_||_| \___/ | .__/ \n" + "_ \n" + " On attend les clients.\n") while 1: #attendre de la connexion connexion, adresse = my_socket.accept() if jeux.partie_commencee == True: #message que la partie est commencé est que personne ne peux se rajouter msg = 'Sorry, partie est commencée FIN ' connexion.send(msg.encode("Utf8")) continue #choix du symbole du symbole du robot #jeux.ajouter_robot("P") th_client = ThreadClient(connexion, jeux) thread_name = th_client.getName() # identifiant du thread flag_created = False nombre_de_robot = 1 if bool(jeux.robots.robots): for item in list(jeux.robots.robots): print('************* robot ****************') robot = jeux.robots.get_robot_name(item) print('nom du robot {}'.format(robot.name)) print('symbole du robot {}'.format(robot.symbole)) print('thread du robot {}'.format(robot.thread_name_r)) #test si toujours valide connexion or robot if CONN_CLIENT[robot.thread_name_r].fileno() < 0: jeux.enlever_robot(item) jeux.ajouter_robot(robot.symbole, item, thread_name) flag_created = True break nombre_de_robot += 1 #si pas de place on fait un nouveau robot if not flag_created: joueur = "joueur" + str(nombre_de_robot) #identifier le jouer jeux.ajouter_robot("x", joueur, thread_name) else: #si on a pas de collection de robot on rajoute joueur = "joueur" + str(nombre_de_robot) jeux.ajouter_robot("x", joueur, thread_name) # Créer un nouvel objet thread pour gérer la connexion : th_client.joueur = joueur th_client.start() # Mémoriser la connexion dans le dictionnaire : CONN_CLIENT[thread_name] = connexion print("Client %s connecté, adresse IP %s, port %s." %\ (thread_name, adresse[0], adresse[1])) # Dialogue avec le client : msg = "Bienveun joueur {} avec symbole {} \n Vous êtes connecté au serveur. \nAppuyé sur C pour commencer\n".format( joueur, jeux.robots.get_robot_name(joueur).symbole) connexion.send(msg.encode("Utf8"))