feat: ajout impl Car et rework State et Game

This commit is contained in:
2025-11-05 20:23:57 +01:00
parent 5121e25e46
commit d2847745ba
6 changed files with 149 additions and 137 deletions

View File

@@ -1,18 +1,24 @@
import java.util.Random;
/**
* <code>Car</code> représente une voiture qui avance sur un circuit en boucles.
* Chaque appel à {@link #makeMove()} avance la voiture d'une position.
* Quand la position atteint la fin de la boucle, un nouveau tour est compté.
*/
public class Car {
public class Car implements GObserver
{
/** Ajout de la classe Random (Evite de le recreer a chaque fois) */
private Random rand = new Random();
/** Position actuelle dans la boucle (entre 0 et loop inclus) */
private int pos = 0;
/** Nombre de tours complétés */
private int round = 0;
/** Nombre total de cases dans une boucle (doit être > 0) */
private final int loop;
private final State state;
/** Nombre de fuel restant */
private int fuel = 60;
@@ -22,8 +28,10 @@ public class Car {
* @param loop nombre de positions par boucle (doit être > 0)
* @throws IllegalArgumentException si {@code loop <= 0}
*/
public Car(int loop)
public Car(int loop, State state)
{
this.state = state;
if (loop <= 0)
throw new IllegalArgumentException("loop must be > 0!");
this.loop = loop;
@@ -85,7 +93,20 @@ public class Car {
public Car consumeFuel()
{
fuel -= State.get().getConsumption();
fuel -= state.getConsumption();
return this;
}
@Override
public boolean apply()
{
if (this.fuel > 0)
{
int[] interval = state.getInterval();
int random = rand.nextInt(interval[0], interval[1]);
makeMove(random).consumeFuel();
}
return true;
}
}