From e68e378e7ab0a6f2872f75f663e8838fadc61fdb Mon Sep 17 00:00:00 2001 From: johanleroy Date: Wed, 10 Sep 2025 08:51:53 +0200 Subject: [PATCH] Robot --- Robots/Modele/Aspirateur.py | 59 ++++++++++++++++++++++++++++++ Robots/Modele/Robot.py | 69 ++++++++++++++++++++++++++++++++++++ Robots/Modele/RobotMobile.py | 31 ++++++++++++++++ Robots/main.py | 30 ++++++++++++++++ TP06.py | 0 5 files changed, 189 insertions(+) create mode 100644 Robots/Modele/Aspirateur.py create mode 100644 Robots/Modele/Robot.py create mode 100644 Robots/Modele/RobotMobile.py create mode 100644 Robots/main.py delete mode 100644 TP06.py diff --git a/Robots/Modele/Aspirateur.py b/Robots/Modele/Aspirateur.py new file mode 100644 index 0000000..1362093 --- /dev/null +++ b/Robots/Modele/Aspirateur.py @@ -0,0 +1,59 @@ +from 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): + if isinstance(value, str) and value: + self._marque = value + else: + print("Erreur : la marque doit être une chaîne non vide.") + self._marque = "Inconnu" + + @property + def puissance(self): + return self._puissance + + @puissance.setter + def puissance(self, value): + if isinstance(value, int) and value > 0: + self._puissance = value + else: + print("Erreur : la puissance doit être un entier positif.") + self._puissance = 1000 + + def __str__(self): + return f"Aspirateur {self.marque}, puissance={self.puissance}W" + + +# --- Aspirateur Robot --- +class AspirateurRobot(Aspirateur, RobotMobile): + def __init__(self, marque, puissance, distance_max): + # Initialisation RobotMobile avec type "Aspirateur robot" + RobotMobile.__init__(self, robot_type="Aspirateur robot", orientation="NORD", statut=1, abscisse=0, ordonnee=0) + Aspirateur.__init__(self, marque, puissance) + self.distance_max = distance_max + self._distance_parcourue = 0 # pour suivre le parcours + + @property + def distance_max(self): + return self._distance_max + + @distance_max.setter + def distance_max(self, value): + if isinstance(value, (int, float)) and value > 0: + self._distance_max = value + else: + print("Erreur : distance_max doit être positive.") + self._distance_max = 100 + + def __str__(self): + return (super(RobotMobile, self).__str__() + f"\nMarque : {self.marque}\n" + f"Puissance : {self.puissance}W") diff --git a/Robots/Modele/Robot.py b/Robots/Modele/Robot.py new file mode 100644 index 0000000..e7dfbe0 --- /dev/null +++ b/Robots/Modele/Robot.py @@ -0,0 +1,69 @@ +import random +import string + +class Robot: + orientations_possibles = ["NORD", "EST", "SUD", "OUEST"] + statuts_possibles = {1: "EN SERVICE", 2: "HORS SERVICE", 3: "EN REPARATION"} + nb_robots_crees = 0 + + def __init__(self, robot_type="Générique", orientation="NORD", statut=1): + self._robot_type = "Générique" + self.robot_type = robot_type # Utilise le setter + self._numero_serie = self._generer_numero_serie() + self._orientation = orientation if orientation in Robot.orientations_possibles else "NORD" + self._statut = statut if statut in Robot.statuts_possibles else 1 + Robot.nb_robots_crees += 1 + + # --- Robot type --- + @property + def robot_type(self): + return self._robot_type + + @robot_type.setter + def robot_type(self, value): + if isinstance(value, str) and len(value) >= 2: + self._robot_type = value + else: + print("Erreur : robot_type doit contenir au moins 2 caractères. Valeur par défaut utilisée.") + self._robot_type = "Générique" + + # --- Numero de série --- + @property + def numero_serie(self): + return self._numero_serie + + def _generer_numero_serie(self): + lettres = ''.join(random.choices(string.ascii_uppercase, k=2)) + chiffres = str(random.randint(0, 999999999)).zfill(10) + return lettres + chiffres + + # --- Orientation --- + @property + def orientation(self): + return self._orientation + + # --- Statut --- + @property + def statut(self): + return self._statut + + @statut.setter + def statut(self, value): + if value in Robot.statuts_possibles: + self._statut = value + else: + print(f"Erreur : statut doit être 1, 2 ou 3. Valeur inchangée ({self._statut}).") + + # --- Affichage --- + def __str__(self): + return (f"Robot {self.numero_serie} – {self.robot_type}\n" + f"Statut : {Robot.statuts_possibles[self.statut]}\n" + f"Orientation : {self.orientation}") + + # --- Tourner --- + def tourner(self, direction): + if direction not in [1, -1]: + print("Erreur : direction doit être 1 (horaire) ou -1 (anti-horaire).") + return + idx = Robot.orientations_possibles.index(self._orientation) + self._orientation = Robot.orientations_possibles[(idx + direction) % 4] diff --git a/Robots/Modele/RobotMobile.py b/Robots/Modele/RobotMobile.py new file mode 100644 index 0000000..7aac43f --- /dev/null +++ b/Robots/Modele/RobotMobile.py @@ -0,0 +1,31 @@ +from Robot import Robot + +class RobotMobile(Robot): + def __init__(self, robot_type="Générique", orientation="NORD", statut=1, abscisse=0, ordonnee=0): + super().__init__(robot_type, orientation, statut) + self._abscisse = abscisse + self._ordonnee = ordonnee + + @property + def abscisse(self): + return self._abscisse + + @property + def ordonnee(self): + return self._ordonnee + + def afficher_position(self): + return f"Position : [abs={self.abscisse} ; ord={self.ordonnee}]" + + def avancer(self, m): + if self.orientation == "EST": + self._abscisse += m + elif self.orientation == "OUEST": + self._abscisse -= m + elif self.orientation == "NORD": + self._ordonnee += m + elif self.orientation == "SUD": + self._ordonnee -= m + + def __str__(self): + return super().__str__() + f"\n{self.afficher_position()}" diff --git a/Robots/main.py b/Robots/main.py new file mode 100644 index 0000000..9be8da4 --- /dev/null +++ b/Robots/main.py @@ -0,0 +1,30 @@ +from Modele.Robot import Robot +from Modele.RobotMobile import RobotMobile +from Modele.Aspirateur import Aspirateur, AspirateurRobot + +def test_robot(): + print("=== Test Robot ===") + r = Robot() + print(r) + r.tourner(1) + print("Après rotation :", r) + +def test_robot_mobile(): + print("\n=== Test RobotMobile ===") + rm = RobotMobile(robot_type="Mobile") + print(rm) + rm.avancer(5) + print("Après avancée de 5 :", rm) + +def test_aspirateur_robot(): + print("\n=== Test AspirateurRobot ===") + ar = AspirateurRobot(marque="Dyson", puissance=7500, distance_max=100) + print(ar) + ar.avancer(10) + ar.tourner(-1) + print("Après déplacement et rotation :", ar) + +if __name__ == "__main__": + test_robot() + test_robot_mobile() + test_aspirateur_robot() diff --git a/TP06.py b/TP06.py deleted file mode 100644 index e69de29..0000000