Skip to content
Snippets Groups Projects
Commit e40292aa authored by Yanis O's avatar Yanis O
Browse files

Ajout de l'infection Virus-Patient et Patient-Patient

parent 4be08dcb
Branches
No related tags found
No related merge requests found
Pipeline #41120 passed
...@@ -13,8 +13,8 @@ import javafx.stage.Stage; ...@@ -13,8 +13,8 @@ import javafx.stage.Stage;
public class SimulatorApplication extends javafx.application.Application { public class SimulatorApplication extends javafx.application.Application {
private static final String VIEW_RESOURCE_PATH = "/view/view.fxml"; private static final String VIEW_RESOURCE_PATH = "/view/view.fxml";
private static final String APP_NAME = "Firefighter simulator"; private static final String APP_NAME = "Firefighter simulator";
private static final int ROW_COUNT = 40; private static final int ROW_COUNT = 50;
private static final int COLUMN_COUNT = 40; private static final int COLUMN_COUNT = 50;
private static final int BOX_WIDTH = 15; private static final int BOX_WIDTH = 15;
private static final int BOX_HEIGHT = 15; private static final int BOX_HEIGHT = 15;
public static final int INITIAL_FIRE_COUNT = 8; public static final int INITIAL_FIRE_COUNT = 8;
......
...@@ -25,7 +25,7 @@ import model.Model; ...@@ -25,7 +25,7 @@ import model.Model;
import model.Square; import model.Square;
import model.doctorviruspatient.DoctorVirusPatientScenario; import model.doctorviruspatient.DoctorVirusPatientScenario;
import model.doctorviruspatient.Patient; import model.doctorviruspatient.Patient;
import model.virus.Virus; import model.doctorviruspatient.Virus;
import util.Position; import util.Position;
import view.Grid; import view.Grid;
import view.ViewElement; import view.ViewElement;
...@@ -137,13 +137,17 @@ public class Controller { ...@@ -137,13 +137,17 @@ public class Controller {
entityCounts.put((pos, b) -> new Mountain(pos), initialmountaincount); entityCounts.put((pos, b) -> new Mountain(pos), initialmountaincount);
entityCounts.put((pos, b) -> new Rockery(pos), 3); entityCounts.put((pos, b) -> new Rockery(pos), 3);
*/ */
/* /*
entityCounts.put((pos, b) -> new Rock(pos), 10); entityCounts.put((pos, b) -> new Rock(pos), 10);
entityCounts.put((pos, b) -> new Cisor(pos), 10); entityCounts.put((pos, b) -> new Cisor(pos), 10);
entityCounts.put((pos, b) -> new Paper(pos), 10); entityCounts.put((pos, b) -> new Paper(pos), 10);
*/ */
entityCounts.put((pos, b) -> new Patient(pos), 10); entityCounts.put((pos, b) -> new Patient(pos), 10);
entityCounts.put((pos, b) -> new Virus(pos), 1); entityCounts.put((pos, b) -> new Virus(pos), 1);
Model model = new DoctorVirusPatientScenario(columnCount, rowCount, entityCounts); Model model = new DoctorVirusPatientScenario(columnCount, rowCount, entityCounts);
this.setModel(model); this.setModel(model);
repaintGrid(); repaintGrid();
......
...@@ -15,7 +15,7 @@ public class Scenario implements Board<Square>{ ...@@ -15,7 +15,7 @@ public class Scenario implements Board<Square>{
protected int step; protected int step;
protected int turnsToSpawnAirTanker; protected int turnsToSpawnAirTanker;
private Map<EntityFactory, Integer> initialMap; protected Map<EntityFactory, Integer> initialMap;
public Scenario(int columns, int rows, Map<EntityFactory, Integer> initialMap) { public Scenario(int columns, int rows, Map<EntityFactory, Integer> initialMap) {
this.matrix = new Matrix<Square>(columns, rows); this.matrix = new Matrix<Square>(columns, rows);
...@@ -91,6 +91,9 @@ public class Scenario implements Board<Square>{ ...@@ -91,6 +91,9 @@ public class Scenario implements Board<Square>{
public Position getNearestEntity(Position fromPos, Class<?> entityType, List<Entity> exclusionList) { public Position getNearestEntity(Position fromPos, Class<?> entityType, List<Entity> exclusionList) {
int rows = matrix.getRows(); int rows = matrix.getRows();
int cols = matrix.getColumns(); int cols = matrix.getColumns();
if(exclusionList == null){
exclusionList = List.of();
}
// Définir la distance maximale possible // Définir la distance maximale possible
int maxDistance = rows + cols; int maxDistance = rows + cols;
// Parcourir les distances croissantes à partir de 1 // Parcourir les distances croissantes à partir de 1
......
...@@ -168,7 +168,9 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) { ...@@ -168,7 +168,9 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) {
.filter(entity -> entity instanceof Patient) .filter(entity -> entity instanceof Patient)
.findFirst() .findFirst()
.orElse(null); .orElse(null);
if(isInfected()){
patient.infect(carriedVirus);
}
if (patient != null) { if (patient != null) {
this.ennemy = patient.getPosition(); this.ennemy = patient.getPosition();
visitedPatients.add(patient); visitedPatients.add(patient);
...@@ -204,7 +206,7 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) { ...@@ -204,7 +206,7 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) {
@Override @Override
public Color getViewColor() { public Color getViewColor() {
return this.viewColor; return isInfected() ? Color.CYAN : this.viewColor;
} }
@Override @Override
...@@ -217,6 +219,7 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) { ...@@ -217,6 +219,7 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) {
} }
public void infect(Virus virus){ public void infect(Virus virus){
if(this.carriedVirus != null) return;
this.carriedVirus = virus; this.carriedVirus = virus;
} }
......
...@@ -7,30 +7,85 @@ import javafx.scene.paint.Color; ...@@ -7,30 +7,85 @@ import javafx.scene.paint.Color;
import model.Board; import model.Board;
import model.Entity; import model.Entity;
import model.Square; import model.Square;
import model.rockpapercisor.Rock; import util.PathGenerator;
import util.Position; import util.Position;
import util.PositionUtil; import util.PositionUtil;
public class Virus implements Entity { public class Virus implements Entity {
private final int priority = 0; private final int priority = 0;
Position position; private Position position;
private int age; private int age;
private final Color viewColor = Color.LIMEGREEN; private final Color viewColor = Color.LIMEGREEN;
private List<Position> path;
private int pathIndex;
private static final int MAX_STEPS_IN_DIRECTION = 5;
private static final int MINIMUM_ROAD_LENGTH = 10;
public Virus(Position p) { public Virus(Position p) {
this.position = p; this.position = p;
this.path = new ArrayList<>();
this.age = 0;
this.pathIndex = 0;
} }
public Virus(Position p, int age) { public Virus(Position p, int age) {
this.position = p; this.position = p;
this.age = age; this.age = age;
this.path = new ArrayList<>();
this.pathIndex = 0;
} }
@Override @Override
public List<Position> nextTurn(Board<Square> board) { public List<Position> nextTurn(Board<Square> board) {
// Génère un nouveau chemin si nécessaire
if (path == null || path.isEmpty() || pathIndex >= path.size()) {
generateNewPath(board);
if (path.isEmpty()) {
return List.of();
}
}
List<Position> adjacentPosition = PositionUtil.generateAllAdjacentPositions(position, board);
for(Position p : adjacentPosition){
if(board.doesSquareContainEntity(p, Patient.class)){
Patient patient = (Patient) board.getStates(p).getEntities().stream().filter(e -> e instanceof Patient).findFirst().get();
patient.infect(this);
}
}
// Se déplace vers la prochaine position du chemin
Position nextPosition = path.get(pathIndex);
if (board.doesPositionExist(nextPosition)) {
// Vérifier si la position est libre
if (board.isPositionFree(nextPosition, priority)) {
Position oldPosition = position;
position = nextPosition;
board.clearCaseFrom(this, oldPosition);
board.addEntityAtSquare(this, position);
pathIndex = pathIndex + 1;
return List.of(oldPosition, position);
} else {
// Si la position n'est pas libre, génère un nouveau chemin
generateNewPath(board);
return List.of();
}
} else {
// Si la prochaine position n'existe pas, génère un nouveau chemin
generateNewPath(board);
return List.of();
}
}
private void generateNewPath(Board<Square> board) {
PathGenerator pathGenerator = new PathGenerator(board, position, MAX_STEPS_IN_DIRECTION, MINIMUM_ROAD_LENGTH);
this.path = pathGenerator.generate();
// Supprime la position actuelle du chemin si elle est présente au début
if (!path.isEmpty() && path.get(0).equals(position)) {
path.remove(0);
}
this.pathIndex = 0; // Réinitialise l'index du chemin
} }
// Les autres méthodes restent inchangées
@Override @Override
public Position getPosition() { public Position getPosition() {
return this.position; return this.position;
...@@ -39,7 +94,6 @@ public class Virus implements Entity { ...@@ -39,7 +94,6 @@ public class Virus implements Entity {
@Override @Override
public void setPosition(Position p) { public void setPosition(Position p) {
this.position = p; this.position = p;
} }
@Override @Override
......
...@@ -25,11 +25,8 @@ public class FireFighterScenario extends Scenario implements Model{ ...@@ -25,11 +25,8 @@ public class FireFighterScenario extends Scenario implements Model{
ArrayList<Position> changedPositions = new ArrayList<>(); ArrayList<Position> changedPositions = new ArrayList<>();
Iterator<Square> iterator = getMatrix().iterator(); Iterator<Square> iterator = getMatrix().iterator();
List<Entity> updatedEntities = new ArrayList<Entity>(); List<Entity> updatedEntities = new ArrayList<Entity>();
int i = 0;
while (iterator.hasNext()) { while (iterator.hasNext()) {
Square s = iterator.next(); Square s = iterator.next();
System.out.println("i : " + i);
i++;
if (s.isEmpty()) if (s.isEmpty())
continue; continue;
if (s.getMaxAge() == 0) { if (s.getMaxAge() == 0) {
...@@ -142,4 +139,12 @@ public class FireFighterScenario extends Scenario implements Model{ ...@@ -142,4 +139,12 @@ public class FireFighterScenario extends Scenario implements Model{
return this; return this;
} }
public void reset() {
step = 0;
super.getMatrix().clear();
initScenario(super.getMatrix());
placeInitialEntities(initialMap);
generateRoads();
}
} }
package model.virus;
import java.util.List;
import javafx.scene.paint.Color;
import model.Board;
import model.Entity;
import model.Square;
import util.Position;
public class Doctor implements Entity{
private Position position;
private final Color VIEW_COLOR = Color.RED;
private int age;
private final int PRIORITY = 1;
public Doctor(Position position){
this.position = position;
this.age = 0;
}
@Override
public List<Position> nextTurn(Board<Square> board) {
return List.of();
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public void incrementAge() {
this.age = age + 1;
}
@Override
public Color getViewColor() {
return this.VIEW_COLOR;
}
@Override
public int getPriority() {
return this.PRIORITY;
}
}
package model.virus;
import java.util.List;
import javafx.scene.paint.Color;
import model.Board;
import model.Entity;
import model.Square;
import util.Position;
public class Patient implements Entity{
private Position position;
private final Color VIEW_COLOR = Color.BLUE;
private int age;
private final int PRIORITY = 1;
private final boolean isSick;
public Patient(Position position){
this.position = position;
this.age = 0;
this.isSick = false;
}
@Override
public List<Position> nextTurn(Board<Square> board) {
if(isSick){
return List.of();
}else{
return List.of();
}
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public void incrementAge() {
this.age = age + 1;
}
@Override
public Color getViewColor() {
return this.VIEW_COLOR;
}
@Override
public int getPriority() {
return this.PRIORITY;
}
}
package model.virus;
import java.util.List;
import javafx.scene.paint.Color;
import model.Board;
import model.Entity;
import model.Square;
import util.Position;
public class Virus implements Entity{
private Position position;
private final Color VIEW_COLOR = Color.LIMEGREEN;
private int age;
private final int PRIORITY = 1;
public Virus(Position position){
this.position = position;
this.age = 0;
}
@Override
public List<Position> nextTurn(Board<Square> board) {
return List.of();
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public void incrementAge() {
this.age = age + 1;
}
@Override
public Color getViewColor() {
return this.VIEW_COLOR;
}
@Override
public int getPriority() {
return this.PRIORITY;
}
}
...@@ -6,6 +6,7 @@ import java.util.Iterator; ...@@ -6,6 +6,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import model.Square; import model.Square;
import model.doctorviruspatient.Patient;
import model.firefighterscenario.Fire; import model.firefighterscenario.Fire;
import model.firefighterscenario.FireFighter; import model.firefighterscenario.FireFighter;
...@@ -83,8 +84,11 @@ public class Matrix<E> implements Iterable<E> { ...@@ -83,8 +84,11 @@ public class Matrix<E> implements Iterable<E> {
else if(s.getEntities().stream().anyMatch(p -> p instanceof FireFighter)){ else if(s.getEntities().stream().anyMatch(p -> p instanceof FireFighter)){
System.out.print(" ff | "); System.out.print(" ff | ");
} }
else if(s.getEntities().stream().anyMatch(p -> p instanceof FireFighter)){ else if(s.getEntities().stream().anyMatch(p -> p instanceof Patient)){
System.out.print(" A | "); System.out.print(" P | ");
}
else if(s.getEntities().stream().anyMatch(p -> p instanceof model.doctorviruspatient.Virus)){
System.out.print(" V | ");
}else{ }else{
System.out.print(" | "); System.out.print(" | ");
} }
......
...@@ -10,7 +10,7 @@ import java.util.Set; ...@@ -10,7 +10,7 @@ import java.util.Set;
import model.Board; import model.Board;
public class PathGenerator { public class PathGenerator {
private final Board<?> board; private Board<?> board;
private final Random random = new Random(); private final Random random = new Random();
private final int maxStepsInDirection; private final int maxStepsInDirection;
private final int minimumRoadLength; private final int minimumRoadLength;
...@@ -59,7 +59,9 @@ public class PathGenerator { ...@@ -59,7 +59,9 @@ public class PathGenerator {
} }
return roadLength >= minimumRoadLength ? path : regeneratePath(); return roadLength >= minimumRoadLength ? path : regeneratePath();
} }
public void setBoard(Board<?> board){
this.board = board;
}
/** Initialise le chemin avec l'ancre et choisit la première direction. */ /** Initialise le chemin avec l'ancre et choisit la première direction. */
private void initializePath() { private void initializePath() {
path.clear(); path.clear();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment