-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterSlavePrisoner.java
More file actions
42 lines (33 loc) · 958 Bytes
/
MasterSlavePrisoner.java
File metadata and controls
42 lines (33 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public final class MasterSlavePrisoner extends Prisoner {
private int number;
// Master (number==0): Counts visited prisoners
// Slave (number!=0): 0 iff never turned light on
private int memory = 0;
@Override
Prisoner create(int number) {
MasterSlavePrisoner newPrisoner = new MasterSlavePrisoner();
newPrisoner.number = number;
return newPrisoner;
}
@Override
public boolean switchLight(long day, boolean[] bulb) {
if (number == 0) {
// The master is called to the room
if (bulb[0]) {
// memorizes if new prisoner visited
memory++;
bulb[0] = false;
}
// returns true when all other prisoners visited
return memory == 99;
} else {
// A slave is called to the room
if (!bulb[0] && memory == 0) {
// Switches light bulb on once, if he never visited before
bulb[0] = true;
memory++;
}
return false;
}
}
}