diff --git a/scenariovirus.txt b/scenariovirus.txt new file mode 100644 index 0000000000000000000000000000000000000000..d43e533bb6bbb2ca17d9d80a418b2e6f13b7b03b --- /dev/null +++ b/scenariovirus.txt @@ -0,0 +1,15 @@ +Patients : +- Interactions et mouvement : Se déplacent et interagissent socialement, augmentant ainsi les chances de transmission du virus. Certains peuvent être asymptomatiques. +- Réaction en cas de maladie : S'ils se rendent compte qu'ils sont malades, ils cherchent un docteur pour se faire soigner. Sinon, ils continuent leur vie normalement, pouvant transmettre le virus sans le savoir. + + +Virus : +- Mouvement et infection : Entité unique sur le plateau qui se déplace aléatoirement, infectant les patients lorsqu'il passe à proximité. +- Le virus infecte les patients lorsqu'il passe à proximité. +- Évolue tous les 30 tours pour donner un nouveau variant, lorsqu'un nouveau variant est détecté, les médecins mettent plus de temps à le soigner au début + + +Docteurs : +- Traitement des patients : Traitent les patients malades qui se trouvent dans des cases adjacentes à leur position. +- Statique : Ne se déplacent pas entre les tours, ce qui nécessite que les patients viennent à eux pour être soignés. + diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 7ebb8ba3c7557ee308d144a866a67af077b7bfd2..7ad1c207ee06150b7fd108898b18f952abf8e6a8 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -23,10 +23,9 @@ import model.Board; import model.EntityFactory; import model.Model; import model.Square; -import model.rockpapercisor.Cisor; -import model.rockpapercisor.Paper; -import model.rockpapercisor.Rock; -import model.rockpapercisor.RockPaperCisorScenario; +import model.doctorviruspatient.DoctorVirusPatientScenario; +import model.doctorviruspatient.Patient; +import model.virus.Virus; import util.Position; import view.Grid; import view.ViewElement; @@ -138,14 +137,18 @@ public class Controller { entityCounts.put((pos, b) -> new Mountain(pos), initialmountaincount); entityCounts.put((pos, b) -> new Rockery(pos), 3); */ + /* entityCounts.put((pos, b) -> new Rock(pos), 10); entityCounts.put((pos, b) -> new Cisor(pos), 10); entityCounts.put((pos, b) -> new Paper(pos), 10); - Model model = new RockPaperCisorScenario(columnCount, rowCount, entityCounts); + */ + entityCounts.put((pos, b) -> new Patient(pos), 10); + entityCounts.put((pos, b) -> new Virus(pos), 1); + Model model = new DoctorVirusPatientScenario(columnCount, rowCount, entityCounts); this.setModel(model); repaintGrid(); } - + public void oneStepButtonAction() { this.pause(); updateBoard(); diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java index be1fcb852db8931ab04e0e0184c57108583563b8..6fc78ddca79a959bf915ed614020208bd9016cc4 100644 --- a/src/main/java/model/Board.java +++ b/src/main/java/model/Board.java @@ -1,5 +1,6 @@ package model; +import java.util.List; import java.util.Map; import util.Position; @@ -63,7 +64,7 @@ public interface Board<S> { public void clearCaseFrom(Entity entity, Position position); - public Position getNearestEntity(Position fromPos, Class<?> entityType) throws EntityNotFoundException; + public Position getNearestEntity(Position fromPos, Class<?> entityType, List<Entity> exclusionList); public boolean doesSquareContainEntity(Position squarePos, Class<?> entityType); diff --git a/src/main/java/model/EntitySpawner.java b/src/main/java/model/EntitySpawner.java index c8f0de6aaa1e51b620125129ff323268c92f6d96..bd6fa6e39196e6ffd798760bc8546e96887694f2 100644 --- a/src/main/java/model/EntitySpawner.java +++ b/src/main/java/model/EntitySpawner.java @@ -12,6 +12,7 @@ import java.util.Set; import util.Direction; import util.Position; +import util.PositionUtil; public class EntitySpawner { private final Board<Square> board; @@ -120,7 +121,7 @@ public class EntitySpawner { int roadLength = 1; // Déterminer la direction interdite (opposée à la direction initiale) - Direction forbiddenDirection = getOppositeDirection(initialDirection); + Direction forbiddenDirection = PositionUtil.getOppositeDirection(initialDirection); // Initialiser la direction courante Direction currentDirection = initialDirection; @@ -163,7 +164,7 @@ public class EntitySpawner { // Mettre à jour la direction courante currentDirection = newDirection; - forbiddenDirection = getOppositeDirection(currentDirection); + forbiddenDirection = PositionUtil.getOppositeDirection(currentDirection); stepsInCurrentDirection = 0; continue; // Recommencer avec la nouvelle direction } else { @@ -185,7 +186,7 @@ public class EntitySpawner { // Mettre à jour la direction courante currentDirection = newDirection; - forbiddenDirection = getOppositeDirection(currentDirection); + forbiddenDirection = PositionUtil.getOppositeDirection(currentDirection); stepsInCurrentDirection = 0; } } @@ -249,15 +250,7 @@ public class EntitySpawner { } } - private static Direction getOppositeDirection(Direction direction) { - switch (direction) { - case NORTH: return Direction.SOUTH; - case SOUTH: return Direction.NORTH; - case EAST: return Direction.WEST; - case WEST: return Direction.EAST; - default: throw new IllegalArgumentException("Direction non supportée : " + direction); - } - } + diff --git a/src/main/java/model/Scenario.java b/src/main/java/model/Scenario.java index 6f04bbc844969968ace0f4d7a6e546e70198c3f4..e58abb80c903b97b5aa327a11a1f94ee4238eb5e 100644 --- a/src/main/java/model/Scenario.java +++ b/src/main/java/model/Scenario.java @@ -88,29 +88,28 @@ public class Scenario implements Board<Square>{ matrix.get(position.x(), position.y()).getEntities().removeIf(element -> element.equals(entity)); } - public Position getNearestEntity(Position fromPos, Class<?> entityType) throws EntityNotFoundException { + public Position getNearestEntity(Position fromPos, Class<?> entityType, List<Entity> exclusionList) { int rows = matrix.getRows(); int cols = matrix.getColumns(); - // Définir la distance maximale possible int maxDistance = rows + cols; // Parcourir les distances croissantes à partir de 1 for (int distance = 1; distance < maxDistance; distance++) { List<Position> positionsAtDistance = PositionUtil.getPositionsAtManhattanDistance(fromPos, distance, rows, cols); - + for (Position currentPos : positionsAtDistance) { + if(isPositionEmpty(currentPos))continue; Square currentSquare = matrix.get(currentPos.x(), currentPos.y()); for (Entity currentEntity : currentSquare.getEntities()) { - if (entityType.isInstance(currentEntity)) { - // Dès qu'une entité est trouvée à cette distance, elle est la plus proche possible + if (entityType.isInstance(currentEntity) && exclusionList != null && !exclusionList.contains(currentEntity)) { + // Vérifie si l'entité actuelle n'est pas dans la liste des exclusions + // Dès qu'une entité éligible est trouvée à cette distance, elle est la plus proche possible return currentPos; } } } } - - // Lever une exception si aucune entité n'est trouvée - throw new EntityNotFoundException("Aucune entité de type " + entityType.getSimpleName() + " trouvée à proximité de " + fromPos); + return null; // Retourne null si aucune entité éligible n'est trouvée } diff --git a/src/main/java/model/doctorviruspatient/DoctorVirusPatientScenario.java b/src/main/java/model/doctorviruspatient/DoctorVirusPatientScenario.java new file mode 100644 index 0000000000000000000000000000000000000000..b22bf7352198b90989490ec5881bb85b3efe5d52 --- /dev/null +++ b/src/main/java/model/doctorviruspatient/DoctorVirusPatientScenario.java @@ -0,0 +1,55 @@ +package model.doctorviruspatient; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import model.Board; +import model.Entity; +import model.EntityFactory; +import model.Model; +import model.Scenario; +import model.Square; +import util.Position; + +public class DoctorVirusPatientScenario extends Scenario implements Model{ + public DoctorVirusPatientScenario(int columns, int rows, Map<EntityFactory, Integer> initialMap) { + super(columns, rows, initialMap); + } + + public List<Position> updateToNextGeneration() { + ArrayList<Position> changedPositions = new ArrayList<>(); + Iterator<Square> iterator = getMatrix().iterator(); + while (iterator.hasNext()) { + Square s = iterator.next(); + if (s.isEmpty()) + continue; + if (s.getMaxAge() == 0) { + s.incrementAllAges(); + continue; + } + if(s.getMaxAge()>stepNumber()+1){ + continue; + } + List<Entity> entities = new ArrayList<>(s.getEntities()); + for (Entity e : entities) { + e.incrementAge(); + changedPositions.addAll(e.nextTurn(this)); + } + } + + // Increment the step counter + this.step = this.step + 1; + return changedPositions; + } + + + + @Override + public Board<Square> getBoard() { + return this; + } + + +} diff --git a/src/main/java/model/doctorviruspatient/Patient.java b/src/main/java/model/doctorviruspatient/Patient.java new file mode 100644 index 0000000000000000000000000000000000000000..0d1759512892a85fd6a02c902148aa38deaf7af6 --- /dev/null +++ b/src/main/java/model/doctorviruspatient/Patient.java @@ -0,0 +1,254 @@ +package model.doctorviruspatient; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +import javafx.scene.paint.Color; +import model.Board; +import model.Entity; +import model.Square; +import util.Position; +import util.PositionUtil; + +public class Patient implements Entity { + private final int priority = 0; + Position position; + private int age; + private final Color viewColor = Color.BLUE; + private Virus carriedVirus; + List<Entity> visitedPatients; + private static int patientsCount = 0; + private int patientID; + private Position ennemy; + private boolean wandering; + + public Patient(Position p) { + this.wandering = true; + patientID = patientsCount; + patientsCount += 1; + this.visitedPatients = new ArrayList<Entity>(); + this.position = p; + } + + public Patient(Position p, int age) { + this.wandering = true; + patientID = patientsCount; + patientsCount += 1; + this.visitedPatients = new ArrayList<Entity>(); + this.position = p; + this.age = age; + } + + @Override + public List<Position> nextTurn(Board<Square> board) { + updateWanderingStatus(board); + + if (wandering) { + return performWandering(board); + } + + resetVisitedPatientsIfNecessary(); + + Position target = determineTarget(board); + + if (target == null) { + visitedPatients.clear(); + return List.of(); + } + + Position nextPos = determineNextPosition(target, board); + + if (nextPos != null && board.doesPositionExist(nextPos)) { + + List<Position> adjacentPositions = PositionUtil.generateAllAdjacentPositions(nextPos, board); + List<Position> result = interactWithAdjacentPositions(adjacentPositions, board); + result.addAll(moveToPosition(nextPos, board)); + result.addAll(adjacentPositions); + result.add(position); + return result; + } + + return List.of(); + } + + + private void updateWanderingStatus(Board<Square> board) { + if (board.getStepNumber() % 8 == 0) { + wandering = false; + } +} + +private List<Position> performWandering(Board<Square> board) { + List<Position> adjacentPositions = PositionUtil.generateAdjacentPositions(position, board); + Collections.shuffle(adjacentPositions); + + Position oldPosition = position; + Position newPosition = adjacentPositions.get(0); + + moveEntityOnBoard(oldPosition, newPosition, board); + return List.of(newPosition, oldPosition); +} + +private void resetVisitedPatientsIfNecessary() { + if (visitedPatients.size() - 1 >= patientsCount) { + visitedPatients.clear(); + } +} + +private Position determineTarget(Board<Square> board) { + return board.getNearestEntity(position, Patient.class, getVisitedPatients()); +} + +private Position determineNextPosition(Position target, Board<Square> board) { + int enemyDistance = PositionUtil.getManhattanDistance(target, position); + + if (ennemy != null && enemyDistance < 2) { + return PositionUtil.getNextPositionAwayFrom(position, target, board); + } else if (target != null) { + return PositionUtil.getNextPositionTowards(position, target, board); + } + return null; +} + +private List<Position> moveToPosition(Position nextPos, Board<Square> board) { + if (!board.isPositionFree(nextPos, priority)) { + nextPos = findAlternativePosition(nextPos, board); + } + + if (nextPos != null) { + Position oldPosition = position; + moveEntityOnBoard(oldPosition, nextPos, board); + } + + return List.of(new Position(this.position.x(), this.position.y()), nextPos); +} + +private Position findAlternativePosition(Position currentPos, Board<Square> board) { + List<Position> adjacentPositions = PositionUtil.generateAdjacentPositions(position, board); + for (Position p : adjacentPositions) { + if (ennemy != null) { + Position awayPos = PositionUtil.getNextPositionAwayFrom(p, ennemy, board); + if (p.equals(awayPos)) { + // Si la position reste la même après avoir tenté de s'éloigner, on continue + continue; + } + } + if (board.isPositionFree(p, priority)) { + return p; + } + } + return null; +} + + +private void moveEntityOnBoard(Position oldPosition, Position newPosition, Board<Square> board) { + board.clearCaseFrom(this, oldPosition); + board.addEntityAtSquare(this, newPosition); + this.position = newPosition; +} + +private List<Position> interactWithAdjacentPositions(List<Position> adjacentPositions, Board<Square> board) { + List<Position> result = new ArrayList<>(); + for (Position p : adjacentPositions) { + if (board.doesSquareContainEntity(p, Patient.class)) { + handleEncounterWithPatient(p, board); + if (isInfected()) { + result.addAll(adjacentPositions); + } + } + } + return result; +} + +private void handleEncounterWithPatient(Position p, Board<Square> board) { + this.wandering = true; + Patient patient = (Patient) board.getStates(p).getEntities().stream() + .filter(entity -> entity instanceof Patient) + .findFirst() + .orElse(null); + + if (patient != null) { + this.ennemy = patient.getPosition(); + visitedPatients.add(patient); + } +} + + + @Override + public Position getPosition() { + return this.position; + } + + @Override + public void setPosition(Position p) { + this.position = p; + + } + + @Override + public int getAge() { + return this.age; + } + + @Override + public void setAge(int age) { + this.age = age; + } + + @Override + public void incrementAge() { + this.age += 1; + } + + @Override + public Color getViewColor() { + return this.viewColor; + } + + @Override + public int getPriority() { + return this.priority; + } + + public boolean isInfected(){ + return this.carriedVirus != null; + } + + public void infect(Virus virus){ + this.carriedVirus = virus; + } + + public Virus getVirus(){ + return this.carriedVirus; + } + + public List<Entity> getVisitedPatients(){ + return this.visitedPatients; + } + + public boolean hasVisited(Patient patient){ + return this.getVisitedPatients().contains(patient); + } + + public int getPatientId(){ + return this.patientID; + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Patient){ + Patient other = (Patient) obj; + return this.patientID == other.getPatientId(); + }else{ + return false; + } + + } + + @Override + public int hashCode() { + return Objects.hash(patientID); + } +} diff --git a/src/main/java/model/doctorviruspatient/Virus.java b/src/main/java/model/doctorviruspatient/Virus.java new file mode 100644 index 0000000000000000000000000000000000000000..8ea8b6b765017246f953b2a89e999a8e0a8e43f9 --- /dev/null +++ b/src/main/java/model/doctorviruspatient/Virus.java @@ -0,0 +1,69 @@ +package model.doctorviruspatient; + +import java.util.ArrayList; +import java.util.List; + +import javafx.scene.paint.Color; +import model.Board; +import model.Entity; +import model.Square; +import model.rockpapercisor.Rock; +import util.Position; +import util.PositionUtil; + +public class Virus implements Entity { + private final int priority = 0; + Position position; + private int age; + private final Color viewColor = Color.LIMEGREEN; + + public Virus(Position p) { + this.position = p; + } + + public Virus(Position p, int age) { + this.position = p; + this.age = age; + } + + @Override + public List<Position> nextTurn(Board<Square> board) { + + } + + @Override + public Position getPosition() { + return this.position; + } + + @Override + public void setPosition(Position p) { + this.position = p; + + } + + @Override + public int getAge() { + return this.age; + } + + @Override + public void setAge(int age) { + this.age = age; + } + + @Override + public void incrementAge() { + this.age += 1; + } + + @Override + public Color getViewColor() { + return this.viewColor; + } + + @Override + public int getPriority() { + return this.priority; + } +} diff --git a/src/main/java/model/firefighterscenario/FireFighter.java b/src/main/java/model/firefighterscenario/FireFighter.java index 8238c6ec511139e863ef76335de8476ba10debb1..37e658f1e779922cb3c0a76ac5bae28de873fedd 100644 --- a/src/main/java/model/firefighterscenario/FireFighter.java +++ b/src/main/java/model/firefighterscenario/FireFighter.java @@ -109,7 +109,7 @@ public class FireFighter implements Entity { // Find the nearest fire Position nearestFirePos; try { - nearestFirePos = b.getNearestEntity(position, Fire.class); + nearestFirePos = b.getNearestEntity(position, Fire.class, null); } catch (Exception e) { return List.of(); } diff --git a/src/main/java/model/firefighterscenario/MotorizedFireFighter.java b/src/main/java/model/firefighterscenario/MotorizedFireFighter.java index 7487e317f4e6984c2bee78e6af73fafee6709539..b8707688057066fbc1ad6c1c6f16801324d78765 100644 --- a/src/main/java/model/firefighterscenario/MotorizedFireFighter.java +++ b/src/main/java/model/firefighterscenario/MotorizedFireFighter.java @@ -56,7 +56,7 @@ public class MotorizedFireFighter extends FireFighter { // Find the nearest fire Position nearestFirePos; try { - nearestFirePos = b.getNearestEntity(getPosition(), Fire.class); + nearestFirePos = b.getNearestEntity(getPosition(), Fire.class, null); } catch (Exception e) { return List.of(); } diff --git a/src/main/java/model/rockpapercisor/Cisor.java b/src/main/java/model/rockpapercisor/Cisor.java index efbcfa74cb9a41f2dd045bd8601decf7639c0089..10566b83a405ab02f81b79343e278ef4539b3c5b 100644 --- a/src/main/java/model/rockpapercisor/Cisor.java +++ b/src/main/java/model/rockpapercisor/Cisor.java @@ -29,18 +29,11 @@ public class Cisor implements Entity { @Override public List<Position> nextTurn(Board<Square> board) { Position target = null; - try { - target = board.getNearestEntity(position, Paper.class); - } catch (Exception e) { - - } + target = board.getNearestEntity(position, Paper.class, null); Position ennemy = null; - try { - ennemy = board.getNearestEntity(position, Rock.class); - } catch (Exception ignored) { - // Si aucune entité Rock n'est trouvée, ennemy reste null. - } + + ennemy = board.getNearestEntity(position, Rock.class, null); if(ennemy == null && target == null){ return List.of(); } diff --git a/src/main/java/model/rockpapercisor/Paper.java b/src/main/java/model/rockpapercisor/Paper.java index 2d28a91f9346a92d90513a2919f953269ba0ff65..1c9117caa03204ea4659d9d367e4c5f0cc8b1da5 100644 --- a/src/main/java/model/rockpapercisor/Paper.java +++ b/src/main/java/model/rockpapercisor/Paper.java @@ -28,18 +28,10 @@ public class Paper implements Entity { @Override public List<Position> nextTurn(Board<Square> board) { Position target = null; - try { - target = board.getNearestEntity(position, Rock.class); - } catch (Exception e) { - - } + target = board.getNearestEntity(position, Rock.class, null); Position ennemy = null; - try { - ennemy = board.getNearestEntity(position, Cisor.class); - } catch (Exception ignored) { - // Si aucune entité Cisor n'est trouvée, ennemy reste null. - } + ennemy = board.getNearestEntity(position, Cisor.class, null); if(ennemy == null && target == null){ return List.of(); } diff --git a/src/main/java/model/rockpapercisor/Rock.java b/src/main/java/model/rockpapercisor/Rock.java index 57a0d3ea42be5aba4a0e244af158daf14ddac5b8..1671a13eb4968187201b905a6e2648614e9b0b24 100644 --- a/src/main/java/model/rockpapercisor/Rock.java +++ b/src/main/java/model/rockpapercisor/Rock.java @@ -28,18 +28,11 @@ public class Rock implements Entity { @Override public List<Position> nextTurn(Board<Square> board) { Position target = null; - try { - target = board.getNearestEntity(position, Cisor.class); - } catch (Exception e) { - - } + target = board.getNearestEntity(position, Cisor.class, null); Position ennemy = null; - try { - ennemy = board.getNearestEntity(position, Paper.class); - } catch (Exception ignored) { - // Si aucune entité Paper n'est trouvée, ennemy reste null. - } + ennemy = board.getNearestEntity(position, Paper.class, null); + if(ennemy == null && target == null){ return List.of(); } diff --git a/src/main/java/util/PositionUtil.java b/src/main/java/util/PositionUtil.java index 39a89106087fb08174ecec0a61d6d6a8de005b7f..096c290d4560c1edbf0d73da67370422515e64da 100644 --- a/src/main/java/util/PositionUtil.java +++ b/src/main/java/util/PositionUtil.java @@ -278,6 +278,47 @@ public class PositionUtil { return nextMove; } - + public static Direction getOppositeDirection(Direction direction) { + switch (direction) { + case NORTH: return Direction.SOUTH; + case SOUTH: return Direction.NORTH; + case EAST: return Direction.WEST; + case WEST: return Direction.EAST; + default: throw new IllegalArgumentException("Direction non supportée : " + direction); + } + } + + /** + * Détermine la direction principale (NORTH, SOUTH, EAST, WEST) de toPos par rapport à fromPos. + * + * @param fromPos la position de départ. + * @param toPos la position de destination. + * @return la direction principale de toPos par rapport à fromPos. + */ +public static Direction getDirectionFromTwoPoints(Position fromPos, Position toPos) { + int deltaX = toPos.x() - fromPos.x(); + int deltaY = toPos.y() - fromPos.y(); + + if (deltaX == 0 && deltaY == 0) { + return null; // Les positions sont identiques + } + + if (Math.abs(deltaX) > Math.abs(deltaY)) { + // Mouvement principalement vers l'Est ou l'Ouest + if (deltaX > 0) { + return Direction.EAST; + } else { + return Direction.WEST; + } + } else { + // Mouvement principalement vers le Nord ou le Sud + if (deltaY > 0) { + return Direction.SOUTH; + } else { + return Direction.NORTH; + } + } +} + } \ No newline at end of file