Skip to content

Commit c0f7184

Browse files
committed
crawl in and out working fine
1 parent 1b5cbc8 commit c0f7184

File tree

4 files changed

+142
-70
lines changed

4 files changed

+142
-70
lines changed

src/entities/OrdinaryThief.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public void hasCanvas(boolean hasCanvas) {
3939
this.hasCanvas = hasCanvas;
4040
}
4141

42+
public AssaultParty getParty() {
43+
return party;
44+
}
45+
46+
public void setParty(AssaultParty party) {
47+
this.party = party;
48+
}
49+
50+
public int getRoomID() {
51+
return party.getRoom().getID();
52+
}
53+
4254
public OrdinaryThief(String threadName, int thiefID, Museum museum, ConcentrationSite concentrationSite, CollectionSite collectionSite, AssaultParty[] assaultParties) {
4355
super(threadName, thiefID, museum, concentrationSite, collectionSite, assaultParties);
4456
thiefState = OrdinaryThiefStates.CONCENTRATION_SITE;

src/sharedRegions/AssaultParty.java

Lines changed: 104 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class AssaultParty {
1919
private final GeneralRepos repos;
2020
private boolean begin;
2121
private int nextThiefID;
22+
private int inRoom;
2223
private Mapping[] thieves;
2324
private Museum.Room room;
2425

@@ -49,6 +50,7 @@ public void setRoom(Museum.Room room) {
4950
GenericIO.writelnString(room + " has distance " + room.getDistance() + " and paintings " + room.getPaintings());
5051
this.room = room;
5152
room.setAssaultPartyID(id);
53+
inRoom = 0;
5254
}
5355

5456
public AssaultParty(int id, GeneralRepos repos) {
@@ -76,90 +78,126 @@ public synchronized void sendAssaultParty() {
7678
notifyAll();
7779
}
7880

79-
public synchronized void reverseDirection() {
80-
}
81+
public synchronized void reverseDirection() {
82+
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
83+
ordinaryThief.setThiefState(OrdinaryThiefStates.CRAWLING_OUTWARDS);
84+
getThief(ordinaryThief.getThiefID()).isAtGoal(false);
85+
inRoom++;
86+
87+
// if last thief reaching room, set begin to true and notify all
88+
if (inRoom == N_THIEVES_PER_PARTY) {
89+
begin = true;
90+
notifyAll();
91+
}
92+
}
93+
94+
private boolean sleep(int thiefID) {
95+
return !getThief(thiefID).isAtGoal() && (!begin || nextThiefID != thiefID || size(thieves) < N_THIEVES_PER_PARTY);
96+
}
8197

8298
public synchronized void crawlIn() {
8399
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
84100
ordinaryThief.setThiefState(OrdinaryThiefStates.CRAWLING_INWARDS);
85101
int thiefID = ordinaryThief.getThiefID();
86-
87102
addThief(thiefID);
88103

89-
if (nextThiefID == -1)
90-
nextThiefID = thiefID;
104+
if (nextThiefID == -1) nextThiefID = thiefID;
91105

92-
while (true) {
106+
do {
93107
// wait until master says to begin
94-
while (!getThief(thiefID).isAtGoal() && (!begin || nextThiefID != thiefID || size(thieves) < N_THIEVES_PER_PARTY)) {
108+
while (sleep(thiefID)) {
95109
try {
96110
wait();
97111
} catch (InterruptedException e) {e.printStackTrace();}
98112
}
113+
} while(crawl(ordinaryThief, 0, room.getDistance()));
99114

100-
// if thief is at goal, stop crawling
101-
if (getThief(thiefID).isAtGoal()) {
102-
ordinaryThief.setThiefState(OrdinaryThiefStates.AT_A_ROOM);
103-
break;
104-
}
115+
ordinaryThief.setThiefState(OrdinaryThiefStates.AT_A_ROOM);
116+
loggerCrawl(ordinaryThief, "ARRIVED AT ROOM");
117+
}
105118

106-
crawl(ordinaryThief);
119+
public synchronized void crawlOut() {
120+
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
121+
ordinaryThief.setThiefState(OrdinaryThiefStates.CRAWLING_OUTWARDS);
122+
int thiefID = ordinaryThief.getThiefID();
107123

108-
// wake up next thief
109-
nextThiefID = getNextThief(thiefID);
110-
GenericIO.writelnString("Next Thief: " + nextThiefID);
111-
if (nextThiefID == -1) {
112-
begin = false;
113-
return;
124+
do {
125+
// wait until master says to begin
126+
while (sleep(thiefID)) {
127+
try {
128+
wait();
129+
} catch (InterruptedException e) {e.printStackTrace();}
114130
}
131+
} while(crawl(ordinaryThief, room.getDistance(), 0));
115132

116-
notifyAll();
117-
}
133+
inRoom--;
134+
ordinaryThief.setThiefState(OrdinaryThiefStates.COLLECTION_SITE);
135+
loggerCrawl(ordinaryThief, "ARRIVED AT COLLECTION SITE");
118136
}
119137

120-
private void crawl(OrdinaryThief ordinaryThief) {
138+
private boolean crawl(OrdinaryThief ordinaryThief, int beginning, int goal) {
121139
printPositions();
122140

123-
int move, distanceToGoal;
124141
int thiefID = ordinaryThief.getThiefID();
125-
int goal = room.getDistance();
126-
boolean canMove = true;
127-
while (canMove) {
128-
move = ordinaryThief.getDisplacement();
129-
boolean validMove;
130-
do {
131-
distanceToGoal = Math.abs(goal - getThiefPosition(thiefID));
132-
move = min(move, distanceToGoal);
133-
134-
setThiefPosition(thiefID, getThiefPosition(thiefID) + move);
135-
loggerCrawl(ordinaryThief, "MOVE " + move + " POS " + getThiefPosition(thiefID));
136-
137-
// check thieves separation from each other
138-
validMove = !wrongSeparation() && !checkOverlay(goal);
139-
if (!validMove) {
140-
setThiefPosition(thiefID, getThiefPosition(thiefID) - move);
141-
loggerCrawl(ordinaryThief, "REVERTED TO POS "+getThiefPosition(thiefID));
142-
move--;
143-
} else {
144-
if (getThiefPosition(thiefID) == goal) {
145-
getThief(thiefID).isAtGoal(true);
146-
loggerCrawl(ordinaryThief, "ARRIVED AT GOAL");
147-
return;
148-
}
149-
}
142+
boolean backwards = goal < beginning;
150143

151-
if (move == 0) {
152-
canMove = false;
153-
break;
144+
while(move(ordinaryThief, beginning, goal, backwards));
145+
146+
// wake up next thief
147+
nextThiefID = getNextThief(thiefID, backwards);
148+
GenericIO.writelnString("Next Thief: " + nextThiefID);
149+
150+
notifyAll();
151+
152+
// if next thief is at goal, then this thief was the last one to reach goal
153+
if (getThiefPosition(nextThiefID) == goal) begin = false;
154+
155+
if (getThief(thiefID).isAtGoal()) return false;
156+
157+
return true;
158+
}
159+
160+
private boolean move(OrdinaryThief ordinaryThief, int beginning, int goal, boolean backwards) {
161+
boolean validMove;
162+
int thiefID = ordinaryThief.getThiefID();
163+
int distanceToGoal;
164+
int move = ordinaryThief.getDisplacement();
165+
do {
166+
distanceToGoal = Math.abs(goal - getThiefPosition(thiefID));
167+
move = min(move, distanceToGoal);
168+
169+
updateThiefPosition(thiefID, move, backwards);
170+
loggerCrawl(ordinaryThief, "MOVE " + (backwards ? "-" : "+") + move + " TO POS " + getThiefPosition(thiefID));
171+
172+
// check thieves separation from each other
173+
validMove = !wrongSeparation(backwards) && !checkOverlay(beginning, goal);
174+
if (!validMove) {
175+
updateThiefPosition(thiefID, move, !backwards);
176+
loggerCrawl(ordinaryThief, "REVERTED TO POS "+getThiefPosition(thiefID));
177+
move--;
178+
} else {
179+
if (getThiefPosition(thiefID) == goal) {
180+
getThief(thiefID).isAtGoal(true);
181+
return false;
154182
}
155-
} while (!validMove);
156-
}
183+
}
157184

185+
if (move == 0) return false;
186+
187+
} while (!validMove);
188+
189+
return true;
190+
}
191+
192+
private void updateThiefPosition(int thiefID, int move, boolean backwards) {
193+
int thiefPos = getThiefPosition(thiefID);
194+
int newPos = !backwards ? thiefPos + move : thiefPos - move;
195+
setThiefPosition(thiefID, newPos);
158196
}
159197

160198
// make sure thieves are only MAX_SEPARATION_LIMIT apart from each other (not circular)
161-
private boolean wrongSeparation() {
162-
orderThieves();
199+
private boolean wrongSeparation(boolean backwards) {
200+
orderThieves(backwards);
163201

164202
boolean valid0 = Math.abs(thieves[0].getPosition() - thieves[1].getPosition()) <= MAX_SEPARATION_LIMIT;
165203
boolean valid1 = Math.abs(thieves[1].getPosition() - thieves[2].getPosition()) <= MAX_SEPARATION_LIMIT;
@@ -168,10 +206,12 @@ private boolean wrongSeparation() {
168206
}
169207

170208
// make sure thieves are not overlapping
171-
private boolean checkOverlay(int goal) {
209+
private boolean checkOverlay(int beginning, int goal) {
172210
for (int i = 0; i < N_THIEVES_PER_PARTY; i++) {
173211
for (int j = i+1; j < N_THIEVES_PER_PARTY; j++) {
174-
if (thieves[i].getPosition() != 0 && thieves[i].getPosition() != goal && thieves[i].getPosition() == thieves[j].getPosition()) {
212+
int currentPos = thieves[i].getPosition();
213+
int nextPos = thieves[j].getPosition();
214+
if (currentPos != beginning && currentPos != goal && currentPos == nextPos) {
175215
GenericIO.writelnString("OVERLAY");
176216
return true;
177217
}
@@ -180,14 +220,9 @@ private boolean checkOverlay(int goal) {
180220
return false;
181221
}
182222

183-
public synchronized void crawlOut() {
184-
185-
}
186-
187223
// get next thief
188-
// TODO: FIX THIS
189-
private int getNextThief(int thiefID) {
190-
orderThieves();
224+
private int getNextThief(int thiefID, boolean backwards) {
225+
orderThieves(backwards);
191226
int nextThiefID = -1;
192227
for (int i = 0; i < N_THIEVES_PER_PARTY; i++) {
193228
if (thieves[i].getThiefID() == thiefID) {
@@ -222,8 +257,11 @@ private int getThiefPosition(int thiefID) {
222257
}
223258

224259
// order thieves by position
225-
private void orderThieves() {
226-
Arrays.sort(thieves, Comparator.comparingInt(Mapping::getPosition));
260+
private void orderThieves(boolean backwards) {
261+
if (backwards)
262+
Arrays.sort(thieves, Comparator.comparingInt(Mapping::getPosition).reversed());
263+
else
264+
Arrays.sort(thieves, Comparator.comparingInt(Mapping::getPosition));
227265
}
228266

229267
private int size(Mapping[] thieves) {
@@ -284,11 +322,9 @@ public void setThiefID(int thiefID) {
284322
public void setPosition(int position) {
285323
this.position = position;
286324
}
287-
288325
public boolean isAtGoal() {
289326
return isAtGoal;
290327
}
291-
292328
public void isAtGoal(boolean atGoal) {
293329
isAtGoal = atGoal;
294330
}

src/sharedRegions/CollectionSite.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import entities.MasterThief;
44
import entities.MasterThiefStates;
5+
import entities.OrdinaryThief;
6+
import entities.OrdinaryThiefStates;
57

68
import static utils.Parameters.*;
9+
import static utils.Utils.all;
710
import static utils.Utils.logger;
811

912
public class CollectionSite {
1013

1114
private int canvas;
1215
private int canvasToCollect;
16+
private boolean[] emptiedRooms;
1317

1418
@Override
1519
public String toString() {
@@ -18,11 +22,15 @@ public String toString() {
1822

1923
public CollectionSite(GeneralRepos repos) {
2024
canvas = canvasToCollect = 0;
25+
emptiedRooms = new boolean[N_ROOMS];
2126
}
2227

2328
public synchronized int appraiseSit() {
2429
MasterThief masterThief = (MasterThief) Thread.currentThread();
2530

31+
if (all(emptiedRooms))
32+
return END_HEIST;
33+
2634
if (masterThief.getActiveAssaultParties() > 0 && masterThief.getConcentrationSite().occupancy() < N_THIEVES_PER_PARTY)
2735
return WAIT_FOR_CANVAS;
2836

@@ -39,18 +47,26 @@ public synchronized void takeARest() {
3947
}
4048

4149
public synchronized void handACanvas() {
50+
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
51+
ordinaryThief.setThiefState(OrdinaryThiefStates.COLLECTION_SITE);
52+
53+
// if ordinary thief doesn't bring a canvas, then the room is empty
54+
if (!ordinaryThief.hasCanvas())
55+
emptiedRooms[ordinaryThief.getRoomID()] = true;
56+
else
57+
canvas++;
58+
4259
canvasToCollect++;
4360
}
4461

4562
public synchronized void collectACanvas() {
46-
canvas++;
4763
canvasToCollect--;
4864
}
4965

5066
public synchronized void sumUpResults() {
5167
MasterThief masterThief = (MasterThief) Thread.currentThread();
5268
masterThief.setThiefState(MasterThiefStates.PRESENTING_REPORT);
53-
69+
5470
logger(this, "The heist is over! Were collected " + canvas + " canvases.");
5571
}
5672
}

src/sharedRegions/Museum.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public void setTotalPaintings(int totalPaintings) {
9090
this.totalPaintings = totalPaintings;
9191
}
9292

93+
public int getID() {
94+
return id;
95+
}
96+
97+
public void setID(int id) {
98+
this.id = id;
99+
}
100+
93101
public Room(int id) {
94102
this.id = id;
95103
distance = random(MIN_DISTANCE, MAX_DISTANCE);

0 commit comments

Comments
 (0)