From c1bb7a77ebe0835aa1e4610d3c13b05bfe3377be Mon Sep 17 00:00:00 2001 From: johanleroy Date: Wed, 10 Sep 2025 14:29:19 +0200 Subject: [PATCH] Robot solution --- Robots/solution/M06_TP02 - Test_Robots.py | 100 ++++++++++++++++++++++ Robots/solution/Modele/Aspirateur.py | 80 +++++++++++++++++ Robots/solution/Modele/Robot.py | 60 +++++++++++++ Robots/solution/Modele/RobotMobile.py | 32 +++++++ Robots/solution/Test_Robots.py | 100 ++++++++++++++++++++++ 5 files changed, 372 insertions(+) create mode 100644 Robots/solution/M06_TP02 - Test_Robots.py create mode 100644 Robots/solution/Modele/Aspirateur.py create mode 100644 Robots/solution/Modele/Robot.py create mode 100644 Robots/solution/Modele/RobotMobile.py create mode 100644 Robots/solution/Test_Robots.py diff --git a/Robots/solution/M06_TP02 - Test_Robots.py b/Robots/solution/M06_TP02 - Test_Robots.py new file mode 100644 index 0000000..d689b02 --- /dev/null +++ b/Robots/solution/M06_TP02 - Test_Robots.py @@ -0,0 +1,100 @@ +from Modele.Aspirateur import Aspirateur, AspirateurRobot +from Modele.Robot import Robot +from Modele.RobotMobile import RobotMobile + +r1 = Robot() +r2 = Robot("Mécanique") +r3 = Robot("Electrique") + +print("------------------------------------") +print("------- TP : PREMIERE PARTIE -------") +print("------------------------------------") + +print("-------- CREATION DE ROBOTS --------") +print(r1) +print('_'*36) +print(r2) +print('_'*36) +print(r3) +print('_'*36) +print("Nombre de robots créés au total : ", Robot.nb_robot) +print("------------------------------------") + +print("--------- TEST SETTER TYPE ---------") +r4 = Robot("T") # Doit afficher un message d'erreur +print(r4) # Doit afficher un type Générique +print('_'*36) +r2.robot_type = "K" # Doit afficher un message d'erreur +print(r2) # Le type ne doit pas avoir été modifié +print("------------------------------------") + +print("--------- TEST STATUT ---------") +r2.statut = 2 +print(r2) +print('_'*36) +r2.statut = 5 # Doit afficher un message d'erreur +print(r2) # Le statut ne doit pas avoir été modifié +print("------------------------------------") + +print("--------- TEST TOURNER ---------") +r3.tourner(1) +print(r3) +print('_'*36) +r3.tourner(-1) +print(r3) +print('_'*36) +r3.tourner(12) # Doit afficher un message d'erreur +print(r3) # Le robot ne doit pas avoir tourné +print("------------------------------------") + +print("------------------------------------") +print("------- TP : DEUXIEME PARTIE -------") +print("------------------------------------") + +r5 = RobotMobile() +r6 = RobotMobile(ord=4) +r7 = RobotMobile(robot_type="Electronique", abs=2, ord=5) + +print("---- CREATION DE ROBOTS MOBILES ----") +print(r5) +print('_'*36) +print(r6) +print('_'*36) +print(r7) +print('_'*36) +print("Nombre de robots créés au total : ", Robot.nb_robot) +print("------------------------------------") + +print("--- DEPLACEMENT DE ROBOTS MOBILES --") +print(r5.afficher_position()) +r5.avancer(4) +print(r5.afficher_position()) +print('_'*36) +print(r6.afficher_position()) +r6.tourner(1) +r6.avancer(4) +print(r6.afficher_position()) +print('_'*36) +print(r7.afficher_position()) +r7.tourner(-1) +r7.avancer(6) +print(r7.afficher_position()) +print("------------------------------------") + +print("------------------------------------") +print("------- TP : TROISIEME PARTIE ------") +print("------------------------------------") + +print("----- CREATION D'UN ASPIRATEUR -----") +a1 = Aspirateur('Rowenta', 4500) +print(a1) +print("------------------------------------") + +print("--- CREATION D'UN ASPIRATEUR ROBOT --") +ar1 = AspirateurRobot('Rowenta', 4500, 115) +print(ar1) +print("------------------------------------") + +print("--- TEST PARCOURS --") +ar1.parcours(10, 15) +print("------------------------------------") diff --git a/Robots/solution/Modele/Aspirateur.py b/Robots/solution/Modele/Aspirateur.py new file mode 100644 index 0000000..b6c47a9 --- /dev/null +++ b/Robots/solution/Modele/Aspirateur.py @@ -0,0 +1,80 @@ +from Modele.RobotMobile import RobotMobile + + +class Aspirateur: + + def __init__(self, marque, puissance): + self.marque = marque + self.puissance = puissance + + @property + def marque(self): + return self._marque + + @marque.setter + def marque(self, value): + self._marque = value + + @property + def puissance(self): + return self._puissance + + @puissance.setter + def puissance(self, value): + self._puissance = value + + def __str__(self): + return "Aspirateur {}, puissance={}W".format(self._marque, self._puissance) + + +class AspirateurRobot(RobotMobile, Aspirateur): + def __init__(self, marque, puissance, distance_max): + Aspirateur.__init__(self, marque, puissance) + RobotMobile.__init__(self, robot_type="Aspirateur Robot") + self.distance_max = distance_max + + @property + def distance_max(self): + return self._distance_max + + @distance_max.setter + def distance_max(self, value): + self._distance_max = value + + def __str__(self): + return RobotMobile.__str__(self) + "\nPuissance : {}W".format(self._puissance) + + def parcours(self, abs_max, ord_max): + grid = [['-' for i in range(abs_max)] for j in range(ord_max)] + grid[0][0] = "*" + # Se diriger vers l'Est + while self.orientation != 'EST': + self.tourner(1) + distance = 0 + + while distance < self._distance_max and self.abs < abs_max and self.ord < ord_max: + self.avancer(1) + distance += 1 + grid[self.ord][self.abs] = '*' + # En bout de ligne on va vers le SUD puis l'OUEST + if self._abs == abs_max - 1: + self.tourner(-1) + if distance < self._distance_max and self.abs < abs_max and self.ord < ord_max: + self.avancer(1) + distance += 1 + grid[self.ord][self.abs] = '*' + self.tourner(-1) + # En début de ligne on va vers le SUD puis l'EST + if self._abs == 0: + self.tourner(1) + if distance < self._distance_max and self.abs < abs_max and self.ord < ord_max: + self.avancer(1) + distance += 1 + grid[self.ord][self.abs] = '*' + self.tourner(1) + + # Affichage du parcours + for i in range(len(grid)): + for j in range(len(grid[i])): + print(grid[i][j], end='') + print() diff --git a/Robots/solution/Modele/Robot.py b/Robots/solution/Modele/Robot.py new file mode 100644 index 0000000..73aa76b --- /dev/null +++ b/Robots/solution/Modele/Robot.py @@ -0,0 +1,60 @@ +import random +import string + + +class Robot: + nb_robot = 0 + ORIENTATIONS = ["NORD", "EST", "SUD", "OUEST"] + STATUTS = {1: "En service", 2: "Hors Service", 3: " En réparation"} + DEFAULT_TYPE = "Générique" + + def __init__(self, robot_type=DEFAULT_TYPE): + self.robot_type = robot_type + self._numero_serie = ''.join([random.choice(string.ascii_uppercase) for i in range(2)]) \ + + ("%4d" % random.randint(0, 1000000000)) + self._orientation = Robot.ORIENTATIONS[0] + self._statut = 1 + + Robot.nb_robot += 1 + + @property + def robot_type(self): + return self._robot_type + + @robot_type.setter + def robot_type(self, value): + if len(value) >= 2: + self._robot_type = value + else: + self._robot_type = Robot.DEFAULT_TYPE + print("Le type du robot doit comporter au minimum 2 caractères.") + + @property + def numero_serie(self): + return self._numero_serie + + @property + def orientation(self): + return self._orientation + + @property + def statut(self): + return self.statut + + @statut.setter + def statut(self, value): + if value in Robot.STATUTS.keys(): + self._statut = value + else: + self._robot_type = Robot.DEFAULT_TYPE + print("Statut invalide ! Valeurs acceptées : ", Robot.STATUTS) + + def __str__(self): + return "Robot {} - {}\nStatut : {}\nOrientation : {}".format(self._numero_serie, self._robot_type, Robot.STATUTS[self._statut], + self._orientation) + + def tourner(self, value): + if value in [1, -1]: + self._orientation = Robot.ORIENTATIONS[(Robot.ORIENTATIONS.index(self._orientation) + value)%len(Robot.ORIENTATIONS)] + else: + print("Un robot peut tourner de 1 ou de -1.") diff --git a/Robots/solution/Modele/RobotMobile.py b/Robots/solution/Modele/RobotMobile.py new file mode 100644 index 0000000..5012a77 --- /dev/null +++ b/Robots/solution/Modele/RobotMobile.py @@ -0,0 +1,32 @@ +from Modele.Robot import Robot + + +class RobotMobile(Robot): + def __init__(self, robot_type='Générique', abs=0, ord=0): + Robot.__init__(self, robot_type) + self._abs = abs + self._ord = ord + + @property + def abs(self): + return self._abs + + @property + def ord(self): + return self._ord + + def afficher_position(self): + return 'Position : [abs={} ; ord={}]'.format(self.abs, self.ord) + + def avancer(self, value): + if self.orientation == "EST": + self._abs += value + elif self.orientation == "OUEST": + self._abs -= value + elif self.orientation == "NORD": + self._ord += value + else: + self._ord -= value + + def __str__(self): + return Robot.__str__(self) + "\n" + self.afficher_position() \ No newline at end of file diff --git a/Robots/solution/Test_Robots.py b/Robots/solution/Test_Robots.py new file mode 100644 index 0000000..d689b02 --- /dev/null +++ b/Robots/solution/Test_Robots.py @@ -0,0 +1,100 @@ +from Modele.Aspirateur import Aspirateur, AspirateurRobot +from Modele.Robot import Robot +from Modele.RobotMobile import RobotMobile + +r1 = Robot() +r2 = Robot("Mécanique") +r3 = Robot("Electrique") + +print("------------------------------------") +print("------- TP : PREMIERE PARTIE -------") +print("------------------------------------") + +print("-------- CREATION DE ROBOTS --------") +print(r1) +print('_'*36) +print(r2) +print('_'*36) +print(r3) +print('_'*36) +print("Nombre de robots créés au total : ", Robot.nb_robot) +print("------------------------------------") + +print("--------- TEST SETTER TYPE ---------") +r4 = Robot("T") # Doit afficher un message d'erreur +print(r4) # Doit afficher un type Générique +print('_'*36) +r2.robot_type = "K" # Doit afficher un message d'erreur +print(r2) # Le type ne doit pas avoir été modifié +print("------------------------------------") + +print("--------- TEST STATUT ---------") +r2.statut = 2 +print(r2) +print('_'*36) +r2.statut = 5 # Doit afficher un message d'erreur +print(r2) # Le statut ne doit pas avoir été modifié +print("------------------------------------") + +print("--------- TEST TOURNER ---------") +r3.tourner(1) +print(r3) +print('_'*36) +r3.tourner(-1) +print(r3) +print('_'*36) +r3.tourner(12) # Doit afficher un message d'erreur +print(r3) # Le robot ne doit pas avoir tourné +print("------------------------------------") + +print("------------------------------------") +print("------- TP : DEUXIEME PARTIE -------") +print("------------------------------------") + +r5 = RobotMobile() +r6 = RobotMobile(ord=4) +r7 = RobotMobile(robot_type="Electronique", abs=2, ord=5) + +print("---- CREATION DE ROBOTS MOBILES ----") +print(r5) +print('_'*36) +print(r6) +print('_'*36) +print(r7) +print('_'*36) +print("Nombre de robots créés au total : ", Robot.nb_robot) +print("------------------------------------") + +print("--- DEPLACEMENT DE ROBOTS MOBILES --") +print(r5.afficher_position()) +r5.avancer(4) +print(r5.afficher_position()) +print('_'*36) +print(r6.afficher_position()) +r6.tourner(1) +r6.avancer(4) +print(r6.afficher_position()) +print('_'*36) +print(r7.afficher_position()) +r7.tourner(-1) +r7.avancer(6) +print(r7.afficher_position()) +print("------------------------------------") + +print("------------------------------------") +print("------- TP : TROISIEME PARTIE ------") +print("------------------------------------") + +print("----- CREATION D'UN ASPIRATEUR -----") +a1 = Aspirateur('Rowenta', 4500) +print(a1) +print("------------------------------------") + +print("--- CREATION D'UN ASPIRATEUR ROBOT --") +ar1 = AspirateurRobot('Rowenta', 4500, 115) +print(ar1) +print("------------------------------------") + +print("--- TEST PARCOURS --") +ar1.parcours(10, 15) +print("------------------------------------")