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

Ajout des docteurs

parent e40292aa
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
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);
}
}
......@@ -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){
......
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment