Simulation du déplacement de tondeuses automatiques sur une grille rectangulaire
Programmation orientée objet · Immutabilité · Pattern matching · Scala
Implémentation en Scala d'un algorithme simulant le déplacement de tondeuses automatiques sur une grille rectangulaire.
Objectifs :
- Calculer correctement les positions finales après application d'une séquence d'instructions
- Architecture robuste et modulaire conforme aux principes OOP et fonctionnels Scala
5 5 ← dimensions de la grille
1 2 N ← position + orientation tondeuse 1
GAGAGAGAA ← instructions tondeuse 1
3 3 E ← position + orientation tondeuse 2
AADAADADDA ← instructions tondeuse 2
| Instruction | Action |
|---|---|
G |
Rotation à gauche (90°) |
D |
Rotation à droite (90°) |
A |
Avancer d'une case |
Tondeuse 1 : 1 3 N
Tondeuse 2 : 5 1 E
src/main/scala/
├── model/
│ ├── Position.scala # Coordonnées (x, y) + déplacement
│ ├── Orientation.scala # sealed trait N/E/S/W + rotations
│ └── Mower.scala # État complet (position + orientation)
├── service/
│ ├── Parser.scala # Lecture & validation du fichier
│ └── Simulator.scala # Application des instructions
└── Main.scala # Point d'entrée
Immutabilité - Aucun var. Toutes les entités sont des case classes immuables :
def move: Mower = {
val next = position.forward(orientation)
if (next.x >= 0 && next.x <= maxX && next.y >= 0 && next.y <= maxY)
copy(position = next)
else this
}Pattern matching - sealed trait exhaustif sur les orientations :
orientation match {
case North => copy(orientation = West)
case West => copy(orientation = South)
case South => copy(orientation = East)
case East => copy(orientation = North)
}Collections fonctionnelles - foldLeft pour les instructions :
instructions.foldLeft(mower) { (currentMower, instruction) =>
instruction match {
case 'G' => currentMower.turnLeft
case 'D' => currentMower.turnRight
case 'A' => currentMower.move
}
}| Scénario | Résultat |
|---|---|
| Cas nominal (grille 5×5, 2 tondeuses) | ✅ |
| Dépassement des limites | ✅ |
| Instruction invalide | ✅ |
| Fichier vide ou mal formaté | ✅ |
git clone https://github.com/EKOURAOGO/Scala-data-analysis.git
cd Scala-data-analysis
sbt compile
sbt "run input.txt"
sbt testKOURAOGO Emmanuel
Data Scientist & Data Analyst