Skip to content

Commit 1ea12eb

Browse files
committed
fixed error when issuing new parties
1 parent 7a608ae commit 1ea12eb

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

src/entities/MasterThief.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class MasterThief extends Thief {
1212
* Number of active assault parties
1313
*/
1414
private int activeAssaultParties;
15+
private boolean[] partyActive;
1516

1617
/**
1718
* Array of room states
@@ -42,6 +43,7 @@ public MasterThief(String threadName, int thiefID, Museum museum, ConcentrationS
4243
for (int i = 0; i < N_ROOMS; i++)
4344
roomState[i] = FREE_ROOM;
4445
this.repos = repos;
46+
partyActive = new boolean[N_ASSAULT_PARTIES];
4547
}
4648

4749
/**
@@ -60,6 +62,26 @@ public void setActiveAssaultParties(int activeAssaultParties) {
6062
this.activeAssaultParties = activeAssaultParties;
6163
}
6264

65+
public void setPartyActive(int partyID, boolean active) {
66+
partyActive[partyID] = active;
67+
}
68+
69+
public boolean getPartyActive(int partyID) {
70+
return partyActive[partyID];
71+
}
72+
73+
public boolean[] getPartyActive() {
74+
return partyActive;
75+
}
76+
77+
public int getFreeParty() {
78+
for (int i = 0; i < N_ASSAULT_PARTIES; i++) {
79+
if (!partyActive[i])
80+
return i;
81+
}
82+
return -1;
83+
}
84+
6385
/**
6486
* Get the room state
6587
* @return Room state

src/entities/OrdinaryThief.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class OrdinaryThief extends Thief {
3939
* @param reset True if the party should be reset
4040
*/
4141
public void setAssaultParty(int partyID, boolean reset) {
42+
logger(this, "Assault Party: "+partyID);
4243
party = assaultParties[partyID];
4344
if (reset) party.resetAssaultParty();
4445
}

src/sharedRegions/AssaultParty.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public String toString() {
129129
public synchronized void sendAssaultParty() {
130130
MasterThief masterThief = (MasterThief) Thread.currentThread();
131131
masterThief.setActiveAssaultParties(masterThief.getActiveAssaultParties() + 1);
132-
//logger(this, "PARTY SENT");
132+
masterThief.setPartyActive(id, true);
133+
logger(this, "PARTY SENT");
133134
begin = true;
134135
notifyAll();
135136

@@ -198,11 +199,11 @@ public synchronized void crawlIn() {
198199
} catch (InterruptedException e) {e.printStackTrace();}
199200

200201
}
201-
GenericIO.writelnString(getThiefPosition(thiefID)+"is the position of thief "+thiefID);
202+
//GenericIO.writelnString(getThiefPosition(thiefID)+"is the position of thief "+thiefID);
202203
} while(crawl(ordinaryThief, 0, room.getDistance()));
203204

204205
ordinaryThief.setThiefState(OrdinaryThiefStates.AT_A_ROOM);
205-
GenericIO.writelnString(getThiefPosition(ordinaryThief.getThiefID())+"is the position of thief "+ordinaryThief.getThiefID());
206+
//GenericIO.writelnString(getThiefPosition(ordinaryThief.getThiefID())+"is the position of thief "+ordinaryThief.getThiefID());
206207
repos.setOrdinaryThiefState(thiefID, OrdinaryThiefStates.AT_A_ROOM);
207208
//repos.setOrdinaryThiefRoomID(thiefID, room.getID());
208209
loggerCrawl(ordinaryThief, "ARRIVED AT " + room);
@@ -283,7 +284,6 @@ private boolean move(OrdinaryThief ordinaryThief, int beginning, int goal, boole
283284
move = min(move, distanceToGoal);
284285

285286
updateThiefPosition(thiefID, move, backwards);
286-
repos.setOrdinaryThiefPosition(thiefID, move);
287287

288288
// check thieves separation from each other
289289
validMove = !wrongSeparation(backwards) && !checkOverlay(beginning, goal);
@@ -313,6 +313,7 @@ private boolean move(OrdinaryThief ordinaryThief, int beginning, int goal, boole
313313
private void updateThiefPosition(int thiefID, int move, boolean backwards) {
314314
int thiefPos = getThiefPosition(thiefID);
315315
int newPos = !backwards ? thiefPos + move : thiefPos - move;
316+
repos.setOrdinaryThiefPosition(thiefID, newPos);
316317
setThiefPosition(thiefID, newPos);
317318
}
318319

src/sharedRegions/CollectionSite.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class CollectionSite {
6363
/**
6464
* Boolean to signal the closing of the party
6565
*/
66-
private boolean closingParty;
66+
private int closingParty;
6767

6868
/**
6969
* General Repository
@@ -136,8 +136,7 @@ private int numberPartiesInSite() {
136136
*/
137137
public CollectionSite(GeneralRepos repos) {
138138
canvas = 0;
139-
appraisedThief = -1;
140-
closingParty = false;
139+
appraisedThief = closingParty = -1;
141140
registeredThieves = new boolean[N_THIEVES_ORDINARY];
142141
inside = new boolean[N_THIEVES_ORDINARY];
143142
roomState = new int[N_ROOMS];
@@ -160,9 +159,11 @@ public synchronized int appraiseSit(int concentrationSiteOccupancy) {
160159
if (endHeist && occupancy() == 0 && concentrationSiteOccupancy == N_THIEVES_ORDINARY)
161160
return END_HEIST;
162161

163-
logger(this, "Active Parties: "+masterThief.getActiveAssaultParties()+", Concentration Occupancy: "+concentrationSiteOccupancy+", Parties in Site: "+numberPartiesInSite()+", Thief Queue: "+thiefQueue.size());
164-
if (masterThief.getActiveAssaultParties() > 0 || (concentrationSiteOccupancy < N_THIEVES_PER_PARTY && numberPartiesInSite() > 0)
165-
|| thiefQueue.size() > 0)
162+
//logger(this, "Active Parties: "+masterThief.getActiveAssaultParties()+", Concentration Occupancy: "+concentrationSiteOccupancy+", Parties in Site: "+numberPartiesInSite()+", Thief Queue: "+thiefQueue.size());
163+
//logger(this, "Free Party: "+masterThief.getFreeParty());
164+
if ((masterThief.getActiveAssaultParties() > 0 && concentrationSiteOccupancy < N_THIEVES_PER_PARTY && numberPartiesInSite() > 0)
165+
|| thiefQueue.size() > 0
166+
|| masterThief.getFreeParty() == -1)
166167
return WAIT_FOR_CANVAS;
167168

168169
while (occupancy() > 0) {
@@ -229,7 +230,7 @@ public synchronized void handACanvas() {
229230
for (int thiefFromParty : thievesOfParty)
230231
registeredThieves[thiefFromParty] = false;
231232
printRoomState();
232-
closingParty = true;
233+
closingParty = ordinaryThief.getPartyID();
233234
if (roomState[ordinaryThief.getRoomID()] == BUSY_ROOM)
234235
roomState[ordinaryThief.getRoomID()] = FREE_ROOM;
235236
}
@@ -268,16 +269,17 @@ public synchronized void collectACanvas() {
268269

269270
logger(masterThief, "Waking up Ordinary " + nextThief.thiefID + " to leave the collection site.");
270271

271-
notifyAll();
272-
273-
if (closingParty) {
274-
closingParty = false;
272+
if (closingParty != -1) {
275273
masterThief.setActiveAssaultParties(masterThief.getActiveAssaultParties() - 1);
274+
masterThief.setPartyActive(closingParty, false);
275+
closingParty = -1;
276276
}
277277

278278
if (thiefQueue.size() == 0)
279279
appraisedThief = -1;
280280

281+
notifyAll();
282+
281283
masterThief.setThiefState(MasterThiefStates.DECIDING_WHAT_TO_DO);
282284
//repos.updateMasterThiefState(MasterThiefStates.DECIDING_WHAT_TO_DO);
283285
}

src/sharedRegions/ConcentrationSite.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,17 @@ public synchronized int prepareAssaultParty() {
142142

143143
// wakeup thieves to prepare excursion
144144
notifyAll();
145+
nextPartyID = master.getFreeParty();
145146

146147
// wait until thieves are ready
147148
while (makeParty) {
148149
try { wait(); } catch (InterruptedException e) {e.printStackTrace();}
149150
}
150151

151-
int currentPartyID = nextPartyID;
152-
// setup nextPartyID
153-
nextPartyID = (nextPartyID + 1) % N_ASSAULT_PARTIES;
152+
master.setPartyActive(nextPartyID, true);
154153

155154
// if thieves are ready
156-
return currentPartyID;
155+
return nextPartyID;
157156
}
158157

159158
/**

src/sharedRegions/Museum.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ public String toString() {
5050
public synchronized void rollACanvas() {
5151
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
5252
ordinaryThief.setThiefState(OrdinaryThiefStates.AT_A_ROOM);
53-
//logger(ordinaryThief, "Rolling a canvas");
53+
logger(ordinaryThief, "Rolling a canvas");
5454

5555
Room room = getRoom(ordinaryThief.getRoomID());
5656
if (room.getPaintings() == 0) {
57-
//logger(ordinaryThief, "Left empty handed from " + room);
57+
logger(ordinaryThief, "Left empty handed from " + room);
5858
ordinaryThief.hasCanvas(false);
5959
}
6060
else {
6161
room.setPaintings(room.getPaintings() - 1);
62-
//logger(ordinaryThief, "Rolled a canvas from " + room + ". " + room.getPaintings() + "/"+ room.getTotalPaintings() +" left");
62+
logger(ordinaryThief, "Rolled a canvas from " + room + ". " + room.getPaintings() + "/"+ room.getTotalPaintings() +" left");
6363
ordinaryThief.hasCanvas(true);
6464
}
6565
}

0 commit comments

Comments
 (0)