@@ -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 }
0 commit comments