Skip to content

Commit aa6c22d

Browse files
committed
working on changes to make this 'project 2 friendly' as per teacher's guides
1 parent 123e588 commit aa6c22d

File tree

8 files changed

+351
-540
lines changed

8 files changed

+351
-540
lines changed

src/entities/MasterThief.java

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@
1111
*/
1212

1313
public class MasterThief extends Thief {
14-
15-
/**
16-
* Number of active assault parties
17-
*/
18-
private int activeAssaultParties;
19-
private boolean[] partyActive;
20-
21-
/**
22-
* Array of room states
23-
*/
24-
private int[] roomState;
25-
2614
/**
2715
* General repository
2816
*/
@@ -42,90 +30,43 @@ public class MasterThief extends Thief {
4230
public MasterThief(String threadName, int thiefID, Museum museum, ConcentrationSite concentrationSite, CollectionSite collectionSite, AssaultParty[] assaultParties, GeneralRepos repos) throws MemException {
4331
super(threadName, thiefID, museum, concentrationSite, collectionSite, assaultParties,repos);
4432
setThiefState(MasterThiefStates.PLANNING_HEIST);
45-
activeAssaultParties = 0;
46-
roomState = new int[N_ROOMS];
47-
for (int i = 0; i < N_ROOMS; i++)
48-
roomState[i] = FREE_ROOM;
4933
this.repos = repos;
50-
partyActive = new boolean[N_ASSAULT_PARTIES];
51-
}
52-
53-
/**
54-
* Get the number of active assault parties
55-
* @return Number of active assault parties
56-
*/
57-
public int getActiveAssaultParties() {
58-
return activeAssaultParties;
59-
}
60-
61-
/**
62-
* Set the number of active assault parties
63-
* @param activeAssaultParties Number of active assault parties
64-
*/
65-
public void setActiveAssaultParties(int activeAssaultParties) {
66-
this.activeAssaultParties = activeAssaultParties;
67-
}
68-
69-
/**
70-
* Set a party as active or inactive
71-
* @param partyID
72-
* @param active
73-
*/
74-
public void setPartyActive(int partyID, boolean active) {
75-
partyActive[partyID] = active;
7634
}
7735

78-
/**
79-
* Get the first free party
80-
* @return Free party ID
81-
*/
82-
public int getFreeParty() {
83-
for (int i = 0; i < N_ASSAULT_PARTIES; i++) {
84-
if (!partyActive[i])
85-
return i;
86-
}
87-
return -1;
88-
}
89-
90-
/**
91-
* Get the room state
92-
* @return Room state
93-
*/
94-
public int[] getRoomState() {
95-
return roomState;
96-
}
97-
98-
/**
99-
* Set the room state
100-
* @param roomState Room state
101-
*/
102-
public void setRoomState(int[] roomState) {
103-
this.roomState = roomState;
104-
}
105-
106-
10736
/**
10837
* Life cycle of the Master Thief
10938
*/
11039
@Override
11140
public void run() {
112-
int concentrationSiteOccupancy;
11341
concentrationSite.startOperations();
11442
lifecycle: while(true) {
115-
concentrationSiteOccupancy = concentrationSite.getOccupancy();
116-
switch (collectionSite.appraiseSit(concentrationSiteOccupancy)) {
43+
switch (collectionSite.appraiseSit(concentrationSite.occupancy(), concentrationSite.nActiveParties(), concentrationSite.getFreeParty())) {
11744
case CREATE_ASSAULT_PARTY:
118-
//logger(this, "CREATE_ASSAULT_PARTY");
45+
logger(this, "CREATE_ASSAULT_PARTY");
11946
repos.updateMasterThiefState(MasterThiefStates.PLANNING_HEIST);
12047
int assaultPartyID = concentrationSite.prepareAssaultParty();
121-
if (assaultPartyID >= 0)
48+
if (assaultPartyID >= 0) {
49+
concentrationSite.setPartyActive(assaultPartyID, true);
12250
assaultParties[assaultPartyID].sendAssaultParty();
51+
}
12352
break;
12453
case WAIT_FOR_CANVAS:
125-
//logger(this, "WAIT_FOR_CANVAS");
54+
logger(this, "WAIT_FOR_CANVAS");
12655
repos.updateMasterThiefState(MasterThiefStates.WAITING_ARRIVAL);
12756
collectionSite.takeARest();
128-
collectionSite.collectACanvas();
57+
58+
int[] partyState = collectionSite.collectACanvas();
59+
int partyID = partyState[0];
60+
int roomID = partyState[1];
61+
int roomState = partyState[2];
62+
63+
if (roomState != BUSY_ROOM)
64+
concentrationSite.setPartyActive(partyID, false);
65+
66+
// only need to update room state if it's not empty already
67+
if (concentrationSite.getRoomState(roomID) != EMPTY_ROOM)
68+
concentrationSite.setRoomState(roomID, roomState);
69+
12970
break;
13071
case END_HEIST:
13172
logger(this, "END_HEIST");

src/entities/OrdinaryThief.java

Lines changed: 23 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -10,144 +10,18 @@
1010
*/
1111

1212
public class OrdinaryThief extends Thief {
13-
14-
/**
15-
* Object the represents the party of the thief
16-
*/
17-
private AssaultParty party;
18-
13+
private int partyID;
14+
private int roomID;
1915
/**
2016
* Displacement of the thief
2117
*/
2218
private int displacement;
2319

24-
/**
25-
* True if the thief has a canvas
26-
*/
27-
private boolean hasCanvas;
28-
29-
/**
30-
* True if ordinary thief is in a party
31-
*/
32-
private boolean inParty;
33-
34-
/**
35-
* Array of Museum Rooms
36-
*/
37-
private Museum.Room[] rooms;
38-
3920
/**
4021
* General repository
4122
*/
4223
private GeneralRepos repos;
4324

44-
45-
/**
46-
* Set AssaultParty array
47-
* @param partyID AssaultParty ID
48-
* @param reset True if the party should be reset
49-
*/
50-
public void setAssaultParty(int partyID, boolean reset) {
51-
logger(this, "Assault Party: "+partyID);
52-
party = assaultParties[partyID];
53-
inParty = true;
54-
repos.setOrdinaryThiefSituation(getThiefID(), isInParty());
55-
if (reset){
56-
party.resetAssaultParty();
57-
58-
}
59-
}
60-
61-
/**
62-
* Set the room of the party
63-
* @param roomID Room ID
64-
*/
65-
public void setRoomOfParty(int roomID) {
66-
party.setRoom(rooms[roomID]);
67-
repos.setApRId(getPartyID(), roomID);
68-
}
69-
70-
/**
71-
* Get the displacement of the thief
72-
* @return Displacement
73-
*/
74-
public int getDisplacement() {
75-
return displacement;
76-
}
77-
78-
/**
79-
* Get thief in party
80-
* @return True if the thief is in a party
81-
*/
82-
public boolean isInParty() {
83-
return inParty;
84-
}
85-
86-
/**
87-
* Set thief in party
88-
* @param inParty True if the thief is in a party
89-
*/
90-
public void setInParty(boolean inParty) {
91-
this.inParty = inParty;
92-
}
93-
94-
95-
/**
96-
* Get the room of the thief
97-
* @return True if the thief has a canvas
98-
*/
99-
public boolean hasCanvas() {
100-
return hasCanvas;
101-
}
102-
103-
/**
104-
* Set if the thief has a canvas
105-
* @param hasCanvas True if the thief has a canvas
106-
*/
107-
public void hasCanvas(boolean hasCanvas) {
108-
this.hasCanvas = hasCanvas;
109-
}
110-
111-
/**
112-
* Set the party of the thief
113-
* @param party Party
114-
*/
115-
public void setParty(AssaultParty party) {
116-
this.party = party;
117-
}
118-
119-
/**
120-
* Get the party of the thief
121-
* @return Party
122-
*/
123-
public AssaultParty getParty() {
124-
return party;
125-
}
126-
127-
/**
128-
* Get the party ID of the thief
129-
* @return Party ID
130-
*/
131-
public int getPartyID() {
132-
return party.getID();
133-
134-
}
135-
136-
/**
137-
* Get the thieves from the party
138-
* @return Array of thieves
139-
*/
140-
public int[] getThievesFromParty() {
141-
return party.getThieves();
142-
}
143-
/**
144-
* Get the room ID of the party
145-
* @return Room ID
146-
*/
147-
public int getRoomID() {
148-
return party.getRoom().getID();
149-
}
150-
15125
/**
15226
* Ordinary Thief Constructor
15327
* @param threadName Thread name
@@ -163,27 +37,38 @@ public OrdinaryThief(String threadName, int thiefID, Museum museum, Concentratio
16337
super(threadName, thiefID, museum, concentrationSite, collectionSite, assaultParties,repos);
16438
thiefState = OrdinaryThiefStates.CONCENTRATION_SITE;
16539
displacement = random(MIN_DISPLACEMENT, MAX_DISPLACEMENT);
166-
hasCanvas = false;
167-
inParty = false;
168-
rooms = museum.getRooms();
16940
this.repos = repos;
41+
partyID = roomID = -1;
17042
repos.setOrdinaryThiefDisplacement(thiefID, displacement);
17143
}
17244

45+
@Override
46+
public String toString() {
47+
return super.toString() + " [Party: " + partyID + ", Room: " + roomID + "]";
48+
}
49+
17350
/**
17451
* Life cycle of the Ordinary Thief
17552
*/
17653
@Override
17754
public void run() {
17855
while (concentrationSite.amINeeded()) {
179-
if (concentrationSite.prepareExcursion()) {
180-
party.crawlIn();
181-
museum.rollACanvas();
56+
int[] assaultData = concentrationSite.prepareExcursion();
57+
if (assaultData != null) {
58+
partyID = assaultData[0];
59+
roomID = assaultData[1];
60+
Museum.Room room = museum.getRoom(roomID);
61+
AssaultParty party = assaultParties[partyID];
62+
party.crawlIn(room.getDistance(), displacement);
63+
64+
boolean hasCanvas = museum.rollACanvas(room.getID());
65+
if (hasCanvas)
66+
room.setPaintings(room.getPaintings() - 1);
18267
party.reverseDirection();
183-
party.crawlOut();
184-
collectionSite.handACanvas();
185-
setInParty(false);
186-
repos.setOrdinaryThiefSituation(getThiefID(), isInParty());
68+
party.crawlOut(room.getDistance(), displacement);
69+
70+
collectionSite.handACanvas(partyID, roomID, hasCanvas);
71+
repos.setOrdinaryThiefSituation(getThiefID(), false);
18772
} else break;
18873
}
18974
}

0 commit comments

Comments
 (0)