Skip to content

Commit 96d8ec7

Browse files
committed
heist is finishing most of the time now
1 parent 398b160 commit 96d8ec7

File tree

6 files changed

+121
-29
lines changed

6 files changed

+121
-29
lines changed

src/entities/MasterThief.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ public void run() {
5757
break lifecycle;
5858
}
5959
}
60+
concentrationSite.endOperations();
6061
}
6162
}

src/entities/OrdinaryThief.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class OrdinaryThief extends Thief {
1515
private boolean hasCanvas;
1616
private Museum.Room[] rooms;
1717

18-
public void setAssaultParty(int partyID) {
18+
public void setAssaultParty(int partyID, boolean reset) {
1919
party = assaultParties[partyID];
20+
if (reset) party.resetAssaultParty();
2021
}
2122

2223
public void setRoomOfParty(int roomID) {

src/main/Assault.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import sharedRegions.*;
88
import utils.MemException;
99

10+
import java.util.concurrent.TimeUnit;
11+
1012
import static utils.Parameters.*;
1113
import static utils.Utils.logger;
1214

@@ -15,6 +17,10 @@ public class Assault {
1517
public static void main(String[] args) throws MemException {
1618
GenericIO.writelnString("Starting program with " + N_THIEVES_ORDINARY + " Ordinary Thieves");
1719

20+
// check proportions
21+
if (N_THIEVES_ORDINARY % N_ASSAULT_PARTIES != 0)
22+
throw new IllegalArgumentException("N_THIEVES_ORDINARY must be a multiple of N_ASSAULT_PARTIES");
23+
1824
MasterThief masters[];
1925
OrdinaryThief thieves[];
2026

@@ -61,6 +67,9 @@ public static void main(String[] args) throws MemException {
6167
for (int i = 0; i < N_THIEVES_ORDINARY; i++)
6268
thieves[i] = new OrdinaryThief("Ordinary_" + i, i, museum, concentrationSite, collectionSite, assaultParties);
6369

70+
// start counting elapsed time
71+
long start = System.currentTimeMillis();
72+
6473
// start threads
6574
for (int i = 0; i < N_THIEVES_MASTER; i++)
6675
masters[i].start();
@@ -71,14 +80,22 @@ public static void main(String[] args) throws MemException {
7180
for (int i = 0; i < N_THIEVES_MASTER; i++) {
7281
try {
7382
masters[i].join();
74-
} catch (InterruptedException e) {}
83+
} catch (InterruptedException e) {e.printStackTrace();}
7584
logger(masters[i], "has terminated!");
7685
}
7786
for (int i = 0; i < N_THIEVES_ORDINARY; i++) {
7887
try {
7988
thieves[i].join();
80-
} catch (InterruptedException e) {}
89+
} catch (InterruptedException e) {e.printStackTrace();}
8190
logger(thieves[i], "has terminated!");
8291
}
92+
93+
// end counting elapsed time
94+
long end = System.currentTimeMillis();
95+
long elapsedTime = end - start;
96+
long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTime); // get the number of seconds
97+
long millis = elapsedTime % 1000; // get the number of milliseconds (mod 1000 to get the remainder)
98+
String readable = String.format("%d.%ds", seconds, millis);
99+
GenericIO.writelnString("The Heist took " + readable + " to complete.");
83100
}
84101
}

src/sharedRegions/AssaultParty.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public synchronized void reverseDirection() {
107107
}
108108

109109
private boolean sleep(int thiefID) {
110+
boolean atGoal;
111+
try {
112+
atGoal = getThief(thiefID).isAtGoal();
113+
} catch (NullPointerException e) {
114+
e.printStackTrace();
115+
GenericIO.writelnString("ERROR: "+ this + " Ordinary_" + thiefID + " thieves " + Arrays.toString(thieves));
116+
System.exit(1);
117+
}
110118
return !getThief(thiefID).isAtGoal() && (!begin || nextThiefID != thiefID || size(thieves) < N_THIEVES_PER_PARTY);
111119
}
112120

src/sharedRegions/CollectionSite.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import entities.MasterThiefStates;
55
import entities.OrdinaryThief;
66
import entities.OrdinaryThiefStates;
7+
import genclass.GenericIO;
78
import utils.MemException;
89
import utils.MemFIFO;
910

@@ -23,12 +24,34 @@ public class CollectionSite {
2324
private int appraisedThief;
2425
private final boolean[] partiesInSite;
2526
private final boolean[] registeredThieves;
27+
private boolean closingParty;
2628

2729
@Override
2830
public String toString() {
2931
return "Collection Site";
3032
}
3133

34+
public void printRoomState() {
35+
String print = "Room State:\n";
36+
for (int i = 0; i < N_ROOMS; i++) {
37+
int state = roomState[i];
38+
print += i+": ";
39+
switch (state) {
40+
case FREE_ROOM:
41+
print += "FREE, ";
42+
break;
43+
case BUSY_ROOM:
44+
print += "BUSY, ";
45+
break;
46+
case EMPTY_ROOM:
47+
print += "EMPTY, ";
48+
break;
49+
}
50+
}
51+
print += "\n";
52+
GenericIO.writelnString(print);
53+
}
54+
3255
public int occupancy() {
3356
int count = 0;
3457
for (int i = 0; i < N_THIEVES_ORDINARY; i++) {
@@ -54,6 +77,7 @@ private int numberPartiesInSite() {
5477
public CollectionSite(GeneralRepos repos) {
5578
canvas = 0;
5679
appraisedThief = -1;
80+
closingParty = false;
5781
registeredThieves = new boolean[N_THIEVES_ORDINARY];
5882
inside = new boolean[N_THIEVES_ORDINARY];
5983
roomState = new int[N_ROOMS];
@@ -104,19 +128,24 @@ public synchronized void takeARest() {
104128
public synchronized void handACanvas() {
105129
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
106130
ordinaryThief.setThiefState(OrdinaryThiefStates.COLLECTION_SITE);
107-
try { thiefQueue.write(ordinaryThief.getThiefID()); } catch (MemException e) {e.printStackTrace();}
131+
try {
132+
thiefQueue.write(ordinaryThief.getThiefID());
133+
} catch (MemException e) {
134+
e.printStackTrace();
135+
}
108136
inside[ordinaryThief.getThiefID()] = true;
109137
registeredThieves[ordinaryThief.getThiefID()] = true;
110138
partiesInSite[ordinaryThief.getParty().getID()] = true;
111-
logger(ordinaryThief, "Entered collection site. Collection Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
139+
//logger(ordinaryThief, "Entered collection site. Collection Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
112140

113141
// register canvas
114142
thiefCanvasState[ordinaryThief.getThiefID()] = ordinaryThief.hasCanvas() ? WITH_CANVAS : WITHOUT_CANVAS;
115-
if (thiefCanvasState[ordinaryThief.getThiefID()] == WITHOUT_CANVAS)
116-
roomState[ordinaryThief.getRoomID()] = EMPTY_ROOM;
117143

118-
// leave collection site
119-
inside[ordinaryThief.getThiefID()] = false;
144+
if (ordinaryThief.getConcentrationSite().getRoomState(ordinaryThief.getRoomID()) != EMPTY_ROOM)
145+
ordinaryThief.getConcentrationSite().setRoomState(ordinaryThief.getRoomID(), ordinaryThief.hasCanvas() ? FREE_ROOM : EMPTY_ROOM);
146+
147+
if (roomState[ordinaryThief.getRoomID()] != EMPTY_ROOM)
148+
roomState[ordinaryThief.getRoomID()] = ordinaryThief.hasCanvas() ? FREE_ROOM : EMPTY_ROOM;
120149

121150
int[] thievesOfParty = ordinaryThief.getParty().getThieves();
122151
int nThievesFromParty = 0;
@@ -126,13 +155,13 @@ public synchronized void handACanvas() {
126155
}
127156
// if last thief of party
128157
if (nThievesFromParty == N_THIEVES_PER_PARTY) {
129-
logger(ordinaryThief, "Last thief from party leaving Collection Site.");
158+
//logger(ordinaryThief, "Last thief from party leaving Collection Site.");
130159
// clear registered thieves from his party
131160
partiesInSite[ordinaryThief.getParty().getID()] = false;
132161
for (int thiefFromParty : thievesOfParty)
133162
registeredThieves[thiefFromParty] = false;
134-
ordinaryThief.getConcentrationSite().setRoomState(ordinaryThief.getRoomID(), ordinaryThief.hasCanvas() ? FREE_ROOM : EMPTY_ROOM);
135-
ordinaryThief.getParty().resetAssaultParty();
163+
printRoomState();
164+
closingParty = true;
136165
}
137166

138167
// wake up master thief
@@ -143,7 +172,9 @@ public synchronized void handACanvas() {
143172
try { wait(); } catch (InterruptedException e) {e.printStackTrace();}
144173
}
145174

146-
logger(ordinaryThief, "Left collection site. Collection Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
175+
// leave collection site
176+
inside[ordinaryThief.getThiefID()] = false;
177+
//logger(ordinaryThief, "Left collection site. Collection Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
147178

148179
}
149180

@@ -169,8 +200,8 @@ public synchronized void collectACanvas() {
169200
thiefCanvasState[nextThiefID] = UNKNOWN;
170201
endHeist = Arrays.stream(roomState).allMatch(roomState -> roomState == EMPTY_ROOM);
171202
masterThief.setRoomState(roomState);
172-
masterThief.setActiveAssaultParties(min(masterThief.getActiveAssaultParties(), numberPartiesInSite()));
173-
logger(masterThief, "Setting active Assault Parties to: " + masterThief.getActiveAssaultParties());
203+
masterThief.getConcentrationSite().setRoomState(roomState);
204+
logger(masterThief, "Parties in site: " + numberPartiesInSite());
174205

175206
// wake up thief
176207
appraisedThief = nextThiefID;
@@ -181,13 +212,19 @@ public synchronized void collectACanvas() {
181212
if (thiefQueue.size() == 0)
182213
appraisedThief = -1;
183214

215+
if (closingParty) {
216+
closingParty = false;
217+
masterThief.setActiveAssaultParties(masterThief.getActiveAssaultParties() - 1);
218+
}
219+
220+
184221
masterThief.setThiefState(MasterThiefStates.DECIDING_WHAT_TO_DO);
185222
}
186223

187224
public synchronized void sumUpResults() {
188225
MasterThief masterThief = (MasterThief) Thread.currentThread();
189226
masterThief.setThiefState(MasterThiefStates.PRESENTING_REPORT);
190227

191-
logger(this, "The heist is over! Were collected " + canvas + " canvases.");
228+
logger(this, "The heist is over! Were collected " + canvas + " canvas.");
192229
}
193230
}

src/sharedRegions/ConcentrationSite.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import genclass.GenericIO;
55
import utils.*;
66

7+
import java.util.Arrays;
8+
79
import static utils.Parameters.*;
810
import static utils.Utils.logger;
911

@@ -15,20 +17,33 @@ public class ConcentrationSite {
1517
private int joinedParty;
1618
private int[] roomState;
1719

18-
public int getFreeRoom() {
20+
public int peekFreeRoom() {
1921
for (int i = 0; i < N_ROOMS; i++) {
20-
if (roomState[i] == FREE_ROOM) {
21-
roomState[i] = BUSY_ROOM;
22+
if (roomState[i] == FREE_ROOM)
2223
return i;
23-
}
2424
}
2525
return -1;
2626
}
2727

28+
public int getFreeRoom() {
29+
int roomID = peekFreeRoom();
30+
if (roomID != -1)
31+
roomState[roomID] = BUSY_ROOM;
32+
return roomID;
33+
}
34+
35+
public void setRoomState(int[] roomState) {
36+
this.roomState = roomState;
37+
}
38+
2839
public void setRoomState(int roomID, int state) {
2940
roomState[roomID] = state;
3041
}
3142

43+
public int getRoomState(int roomID) {
44+
return roomState[roomID];
45+
}
46+
3247
public int occupancy() {
3348
int count = 0;
3449
for (int i = 0; i < N_THIEVES_ORDINARY; i++) {
@@ -60,17 +75,19 @@ public synchronized void startOperations() {
6075
}
6176

6277
public synchronized boolean amINeeded() {
63-
OrdinaryThief thief = (OrdinaryThief) Thread.currentThread();
64-
thief.setThiefState(OrdinaryThiefStates.CONCENTRATION_SITE);
78+
OrdinaryThief ordinaryThief = (OrdinaryThief) Thread.currentThread();
79+
ordinaryThief.setThiefState(OrdinaryThiefStates.CONCENTRATION_SITE);
80+
81+
ordinaryThief.setParty(null);
6582

6683
if (endHeist) {
6784
notifyAll();
6885
return false;
6986
}
7087

7188
// register in concentration site
72-
inside[thief.getThiefID()] = true;
73-
logger(thief, "Entered concentration site. Concentration Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
89+
inside[ordinaryThief.getThiefID()] = true;
90+
logger(ordinaryThief, "Entered concentration site. Concentration Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
7491

7592
// wakeup master to check if there are enough thieves to make a party
7693
notifyAll();
@@ -116,18 +133,22 @@ public synchronized boolean prepareExcursion() {
116133
// wait until master says to prepare excursion
117134
while (!endHeist && (!makeParty || joinedParty >= N_THIEVES_PER_PARTY)) {
118135
try { wait(); } catch (InterruptedException e) {e.printStackTrace();}
136+
/*GenericIO.writelnString("end heist: "+endHeist+", make party: "+makeParty+", joined party: "+joinedParty);*/
119137
}
120138

121-
if (endHeist) {
139+
if (endHeist || peekFreeRoom() == -1) {
140+
inside[ordinaryThief.getThiefID()] = false;
141+
makeParty = false;
122142
notifyAll();
123143
return false;
124144
}
125145

126146
// setup nextPartyID
127-
GenericIO.writelnString("NEXT PARTY ID: "+nextPartyID);
128-
ordinaryThief.setAssaultParty(nextPartyID);
147+
//GenericIO.writelnString("NEXT PARTY ID: "+nextPartyID);
129148
joinedParty++;
149+
ordinaryThief.setAssaultParty(nextPartyID, joinedParty == 1);
130150
logger(ordinaryThief, "Joined Party " + nextPartyID + ". Party Occupancy: " + joinedParty + "/" + N_THIEVES_PER_PARTY);
151+
GenericIO.writelnString(Arrays.toString(roomState));
131152

132153
// if last thief joining, reset variables and wakeup master
133154
if (joinedParty == N_THIEVES_PER_PARTY) {
@@ -136,17 +157,24 @@ public synchronized boolean prepareExcursion() {
136157
notifyAll();
137158

138159
int roomID = getFreeRoom();
139-
if (roomID == -1)
160+
if (roomID == -1) {
161+
GenericIO.writelnString("No more free rooms for " + ordinaryThief);
140162
return false;
163+
}
141164

142165
ordinaryThief.setRoomOfParty(roomID);
143166
}
144167

145168
// leave concentration site
146169
inside[ordinaryThief.getThiefID()] = false;
147-
logger(ordinaryThief, "Left concentration site. Concentration Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
170+
//logger(ordinaryThief, "Left concentration site. Concentration Site Occupancy: " + occupancy() + "/" + N_THIEVES_ORDINARY);
171+
notifyAll();
148172

149173
return true;
150174
}
151175

176+
public synchronized void endOperations() {
177+
endHeist = true;
178+
notifyAll();
179+
}
152180
}

0 commit comments

Comments
 (0)