Robot
This commit is contained in:
69
Robots/Modele/Robot.py
Normal file
69
Robots/Modele/Robot.py
Normal file
@@ -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]
|
||||
Reference in New Issue
Block a user