44import entities .MasterThiefStates ;
55import entities .OrdinaryThief ;
66import entities .OrdinaryThiefStates ;
7+ import genclass .GenericIO ;
78import utils .MemException ;
89import 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}
0 commit comments