Robot solution

This commit is contained in:
johanleroy
2025-09-10 14:29:19 +02:00
parent e68e378e7a
commit c1bb7a77eb
5 changed files with 372 additions and 0 deletions

View File

@@ -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()

View File

@@ -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.")

View File

@@ -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()