diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 8fed944ea6595e898a761957b448121a1ac51670..6bb91838774df84d60437425152140133ed39715 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -23,6 +23,7 @@ import model.Board; import model.EntityFactory; import model.Model; import model.Square; +import model.doctorviruspatient.Doctor; import model.doctorviruspatient.DoctorVirusPatientScenario; import model.doctorviruspatient.Patient; import model.doctorviruspatient.Virus; @@ -147,6 +148,7 @@ public class Controller { entityCounts.put((pos, b) -> new Patient(pos), 10); entityCounts.put((pos, b) -> new Virus(pos), 1); + entityCounts.put((pos, b) -> new Doctor(pos), 3); Model model = new DoctorVirusPatientScenario(columnCount, rowCount, entityCounts); this.setModel(model); diff --git a/src/main/java/model/doctorviruspatient/Doctor.java b/src/main/java/model/doctorviruspatient/Doctor.java new file mode 100644 index 0000000000000000000000000000000000000000..82f4df7324e1e5259b8ec7578999329f2a0fddcd --- /dev/null +++ b/src/main/java/model/doctorviruspatient/Doctor.java @@ -0,0 +1,110 @@ +package model.doctorviruspatient; + +import java.util.ArrayList; +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 Doctor implements Entity { + private final int priority = 0; + Position position; + private int age; + private final Color viewColor = Color.RED; + + public Doctor(Position p) { + this.position = p; + } + + public Doctor(Position p, int age) { + this.position = p; + this.age = age; + } + + @Override + public List<Position> nextTurn(Board<Square> board) { + interactWithAdjacentPositions(PositionUtil.generateAdjacentPositions(position, board), board); + return List.of(); + } + + + + +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); + } + } + return result; +} + +private void handleEncounterWithPatient(Position p, Board<Square> board) { + if(board.doesSquareContainEntity(p, Patient.class)){ + Patient patient = (Patient) board.getStates(p).getEntities().stream().filter(e -> e instanceof Patient).findFirst().get(); + if(patient.isInfected()){ + patient.cure(); + } + } +} + + + @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; + } + + + @Override + public boolean equals(Object obj) { + if(obj instanceof Doctor){ + Doctor other = (Doctor) obj; + return this.position == other.getPosition(); + }else{ + return false; + } + + } + + @Override + public int hashCode() { + return Objects.hash(position); + } +} diff --git a/src/main/java/model/doctorviruspatient/Patient.java b/src/main/java/model/doctorviruspatient/Patient.java index 3d1c90efa13cd92986f563f782d809404e250920..9e623990b3edac790afb7ee8db5d4155b62778ab 100644 --- a/src/main/java/model/doctorviruspatient/Patient.java +++ b/src/main/java/model/doctorviruspatient/Patient.java @@ -43,6 +43,9 @@ public class Patient implements Entity { @Override public List<Position> nextTurn(Board<Square> board) { + if(isInfected()){ + return moveToDoctor(); + } updateWanderingStatus(board); if (wandering) { @@ -79,7 +82,9 @@ public class Patient implements Entity { wandering = false; } } - +private List<Position> moveToDoctor(){ + return List.of(); +} private List<Position> performWandering(Board<Square> board) { List<Position> adjacentPositions = PositionUtil.generateAdjacentPositions(position, board); Collections.shuffle(adjacentPositions); @@ -239,6 +244,12 @@ private void handleEncounterWithPatient(Position p, Board<Square> board) { return this.patientID; } + public void cure(){ + if(this.carriedVirus.tryToKill()){ + this.carriedVirus = null; + } + } + @Override public boolean equals(Object obj) { if(obj instanceof Patient){ diff --git a/src/main/java/model/doctorviruspatient/Virus.java b/src/main/java/model/doctorviruspatient/Virus.java index 1dd4399ce65aa8257e022046f69d9bccea4db623..4c66b0ee6196985d8ef91b1b5d785a359949b652 100644 --- a/src/main/java/model/doctorviruspatient/Virus.java +++ b/src/main/java/model/doctorviruspatient/Virus.java @@ -20,8 +20,10 @@ public class Virus implements Entity { private int pathIndex; private static final int MAX_STEPS_IN_DIRECTION = 5; private static final int MINIMUM_ROAD_LENGTH = 10; + private int health; public Virus(Position p) { + this.health = 8; this.position = p; this.path = new ArrayList<>(); this.age = 0; @@ -29,6 +31,7 @@ public class Virus implements Entity { } public Virus(Position p, int age) { + this.health = 8; this.position = p; this.age = age; this.path = new ArrayList<>(); @@ -120,4 +123,12 @@ public class Virus implements Entity { public int getPriority() { return this.priority; } + + public boolean tryToKill(){ + if(health <= 0){ + return true; + } + health--; + return false; + } }