Cannibals and missionaries
Assignment
Simulate the problem of transporting cannibals and missionaries across the river. On the bank of a wide river there is a boat carrying passengers to the other side of the bank. The boat has a capacity of 7 passengers. If there are less than three passengers on board, the boat must not make the crossing. The final restriction that must always be observed is that the number of cannibals in the boat cannot be higher if there is a missionary or missionaries in the boat. For example, if there is already one cannibal in the boat, the missionary will go in. But if there are two cannibals in the boat, the missionary will not go in. Similarly, if there is one cannibal and one missionary on board, only a missionary is allowed to go in next.Cannibals and missionaries should arrive to one side of the river, in randomly fashion: one missionary every two seconds, one cannibal each second. After crossing the river, the passengers should move on (they leave simulation). Each missionary and cannibal must be simulated by its own thread. Missionary and cannibal threads should be dynamically created from the start thread (main).
Simulate the boat with its own thread (only one boat in the system), initially set on the right bank. After three (or more) seats are filled in the boat, the boat must wait another second and then move across the river, which takes two seconds in simulation. The boat thread must print who it is transporting at each transition (e.g., "Transporting: missionary, cannibal, missionary, missionary").
Synchronize correctly all threads representing the cannibals, the missionaries, and the boat – with behavior as described above.
Example program output
Legend: M-missionary, C-cannibal, B-boat, LB-left bank, RM-right bank L-left, R-right B: empty on right bank B[R]={} LB={} RB={} M1: arrived on left bank B[R]={} LB={M1} RB={} C1: arrived on right bank B[R]={} LB={M1} RB={C1} C1: entered boat B[R]={C1} LB={M1} RB={} C2: arrived on left bank B[R]={C1} LB={M1 C2} RB={} M2: arrived on right bank B[R]={C1} LB={M1 C2} RB={M2} M2: entered boat B[R]={C1 M2} LB={M1 C2} RB={} C3: arrived on right bank B[R]={C1 M2} LB={M1 C2} RB={C3} M3: arrived on right bank B[R]={C1 M2} LB={M1 C2} RB={C3 M3} M3: entered boat B[R]={C1 M2 M3} LB={M1 C2} RB={C3} C3: entered boat B[R]={C1 M2 M3 C3} LB={M1 C2} RB={} B: three passengers in, departing in one second B[R]={C1 M2 M3 C3} LB={M1 C2} RB={} M4: arrived on right bank B[R]={C1 M2 M3 C3} LB={M1 C2} RB={M4} M4: entered boat B[R]={C1 M2 M3 C3 M4} LB={M1 C2} RB={} C4: arrived on right bank B[R]={C1 M2 M3 C3 M4} LB={M1 C2 C4} RB={} B: transporting from right bank to left: C1 M2 M3 C3 M4 C5: arrived on right bank B[R]={C1 M2 M3 C3 M4} LB={M1 C2 C4} RB={C5} M5: arrived on left bank B[R]={C1 M2 M3 C3 M4} LB={M1 C2 C4 M5} RB={C5} B: transported from right bank to left: C1 M2 M3 C3 M4 B: empty on left bank B[L]={} LB={M1 C2 C4 M5} RB={C5} M1: entered boat B[L]={M1} LB={C2 C4 M5} RB={C5} C2: entered boat B[L]={M1 C2} LB={C4 M5} RB={C5} M5: entered boat B[L]={M1 C2 M5} LB={C4} RB={C5} B: three passengers in, departing in one second C5: entered boat B[L]={M1 C2 M5 C4} LB={} RB={C5} B: transporting from left bank to right: M1 C2 M5 C4 ...