mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-31 03:01:38 +00:00
Ajout de la partie view du projet
This commit is contained in:
118
model/Car.java
Normal file
118
model/Car.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package model;
|
||||
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 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;
|
||||
|
||||
/**
|
||||
* Construit une nouvelle voiture.
|
||||
*
|
||||
* @param loop nombre de positions par boucle (doit être > 0)
|
||||
* @throws IllegalArgumentException si {@code loop <= 0}
|
||||
*/
|
||||
public Car(int loop, State state)
|
||||
{
|
||||
this.state = state;
|
||||
|
||||
if (loop <= 0)
|
||||
throw new IllegalArgumentException("loop must be > 0!");
|
||||
this.loop = loop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fait avancer la voiture d'une position.
|
||||
* <p>Si la position atteint la fin de la boucle, un nouveau tour est compté.</p>
|
||||
*
|
||||
* @return cette même instance (pour chaînage fluide)
|
||||
*/
|
||||
public Car makeMove(int move)
|
||||
{
|
||||
pos += move;
|
||||
if (pos == loop)
|
||||
{
|
||||
round++;
|
||||
pos = 0;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return la position actuelle dans la boucle
|
||||
*/
|
||||
public int getPosition()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return le score, calculé comme (nombre de tours + progression du tour) × 100
|
||||
*/
|
||||
public int getScore()
|
||||
{
|
||||
return (int) ((round + (float) pos / loop) * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return le nombre de tours complétés
|
||||
*/
|
||||
public int getRound()
|
||||
{
|
||||
return round;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return la taille de la boucle (nombre de positions)
|
||||
*/
|
||||
public int getLoop()
|
||||
{
|
||||
return loop;
|
||||
}
|
||||
|
||||
public int getFuel()
|
||||
{
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public Car consumeFuel()
|
||||
{
|
||||
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;
|
||||
}
|
||||
/** Retourne l'état de la voiture (State) */
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user