Skip to content

Commit 332e77c

Browse files
committed
removed more references to shared regions
1 parent 879a35f commit 332e77c

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

src/entities/MasterThief.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public void run() {
4646
case CREATE_ASSAULT_PARTY:
4747
logger(this, "CREATE_ASSAULT_PARTY");
4848
int assaultPartyID = concentrationSite.prepareAssaultParty();
49-
if (assaultPartyID == -1) break;
50-
assaultParties[assaultPartyID].sendAssaultParty();
49+
if (assaultPartyID >= 0)
50+
assaultParties[assaultPartyID].sendAssaultParty();
5151
break;
5252
case WAIT_FOR_CANVAS:
5353
logger(this, "WAIT_FOR_CANVAS");

src/entities/OrdinaryThief.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,21 @@ public void hasCanvas(boolean hasCanvas) {
4040
this.hasCanvas = hasCanvas;
4141
}
4242

43+
public void setParty(AssaultParty party) {
44+
this.party = party;
45+
}
46+
4347
public AssaultParty getParty() {
4448
return party;
4549
}
4650

47-
public void setParty(AssaultParty party) {
48-
this.party = party;
51+
public int getPartyID() {
52+
return party.getID();
4953
}
5054

55+
public int[] getThievesFromParty() {
56+
return party.getThieves();
57+
}
5158
public int getRoomID() {
5259
return party.getRoom().getID();
5360
}

src/sharedRegions/AssaultParty.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,11 @@ private boolean move(OrdinaryThief ordinaryThief, int beginning, int goal, boole
250250
move = min(move, distanceToGoal);
251251

252252
updateThiefPosition(thiefID, move, backwards);
253-
//loggerCrawl(ordinaryThief, "MOVE " + (backwards ? "-" : "+") + move + " TO POS " + getThiefPosition(thiefID));
254253

255254
// check thieves separation from each other
256255
validMove = !wrongSeparation(backwards) && !checkOverlay(beginning, goal);
257256
if (!validMove) {
258257
updateThiefPosition(thiefID, move, !backwards);
259-
//loggerCrawl(ordinaryThief, "REVERTED TO POS "+getThiefPosition(thiefID));
260258
move--;
261259
} else {
262260
if (getThiefPosition(thiefID) == goal) {

src/sharedRegions/CollectionSite.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public synchronized int appraiseSit(int concentrationSiteOccupancy) {
109109
public synchronized void takeARest() {
110110
MasterThief masterThief = (MasterThief) Thread.currentThread();
111111
masterThief.setThiefState(MasterThiefStates.WAITING_ARRIVAL);
112+
112113
// sleep until an ordinary thief reaches the collection site and hands a canvas
113114
while (canvasToCollect() == 0) {
114115
try { wait(); } catch (InterruptedException e) {e.printStackTrace();}
@@ -125,7 +126,7 @@ public synchronized void handACanvas() {
125126
}
126127
inside[ordinaryThief.getThiefID()] = true;
127128
registeredThieves[ordinaryThief.getThiefID()] = true;
128-
partiesInSite[ordinaryThief.getParty().getID()] = true;
129+
partiesInSite[ordinaryThief.getPartyID()] = true;
129130
//logger(ordinaryThief, "Entered collection site. Collection Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
130131

131132
// register canvas
@@ -134,7 +135,7 @@ public synchronized void handACanvas() {
134135
if (roomState[ordinaryThief.getRoomID()] != EMPTY_ROOM)
135136
roomState[ordinaryThief.getRoomID()] = ordinaryThief.hasCanvas() ? FREE_ROOM : EMPTY_ROOM;
136137

137-
int[] thievesOfParty = ordinaryThief.getParty().getThieves();
138+
int[] thievesOfParty = ordinaryThief.getThievesFromParty();
138139
int nThievesFromParty = 0;
139140
for (int thiefFromParty : thievesOfParty) {
140141
if (registeredThieves[thiefFromParty])
@@ -144,7 +145,7 @@ public synchronized void handACanvas() {
144145
if (nThievesFromParty == N_THIEVES_PER_PARTY) {
145146
//logger(ordinaryThief, "Last thief from party leaving Collection Site.");
146147
// clear registered thieves from his party
147-
partiesInSite[ordinaryThief.getParty().getID()] = false;
148+
partiesInSite[ordinaryThief.getPartyID()] = false;
148149
for (int thiefFromParty : thievesOfParty)
149150
registeredThieves[thiefFromParty] = false;
150151
printRoomState();
@@ -193,16 +194,16 @@ public synchronized void collectACanvas() {
193194
appraisedThief = nextThiefID;
194195

195196
logger(masterThief, "Waking up Ordinary " + nextThiefID + " to leave the collection site.");
196-
notifyAll();
197197

198-
if (thiefQueue.size() == 0)
199-
appraisedThief = -1;
198+
notifyAll();
200199

201200
if (closingParty) {
202201
closingParty = false;
203202
masterThief.setActiveAssaultParties(masterThief.getActiveAssaultParties() - 1);
204203
}
205204

205+
if (thiefQueue.size() == 0)
206+
appraisedThief = -1;
206207

207208
masterThief.setThiefState(MasterThiefStates.DECIDING_WHAT_TO_DO);
208209
}

src/sharedRegions/ConcentrationSite.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ public synchronized int prepareAssaultParty() {
102102
// update rooms state
103103
roomState = master.getRoomState();
104104

105-
if (peekFreeRoom() == -1)
105+
if (peekFreeRoom() == -1) {
106+
notifyAll();
106107
return -1;
108+
}
107109

108110
// wait until there are enough thieves to make a party
109111
while (occupancy() < N_THIEVES_PER_PARTY && !endHeist ) {
@@ -133,8 +135,6 @@ public synchronized boolean prepareExcursion() {
133135
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
134136
ordinaryThief.setThiefState(OrdinaryThiefStates.CRAWLING_INWARDS);
135137

136-
notifyAll();
137-
138138
// wait until master says to prepare excursion
139139
while (!endHeist && (!makeParty || joinedParty >= N_THIEVES_PER_PARTY)) {
140140
//logger(ordinaryThief, "Waiting for master to prepare excursion");

0 commit comments

Comments
 (0)