Newer
Older
Yanis O
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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) {
Yanis O
committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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> moveToDoctor(){
return List.of();
}
Yanis O
committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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(isInfected()){
patient.infect(carriedVirus);
}
Yanis O
committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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 isInfected() ? Color.CYAN : this.viewColor;
Yanis O
committed
}
@Override
public int getPriority() {
return this.priority;
}
public boolean isInfected(){
return this.carriedVirus != null;
}
public void infect(Virus virus){
if(this.carriedVirus != null) return;
Yanis O
committed
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;
}
public void cure(){
if(this.carriedVirus.tryToKill()){
this.carriedVirus = null;
}
}
Yanis O
committed
@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);
}
}