% missionaries and cannibals planning

initial_state([3, 3, -1]).
goal_state([0, 0, _]).

legal_move([M0, C0, B0], cc, [M0, C1, B1]) :-
    C1 is C0 + 2*B0,
    legal_state(M0, C1),
    B1 is -B0.

legal_move([M0, C0, B0], c, [M0, C1, B1]) :-
    C1 is C0 + B0,
    legal_state(M0, C1),
    B1 is -B0.

legal_move([M0, C0, B0], mm, [M1, C0, B1]) :-
    M1 is M0 + 2*B0,
    legal_state(M1, C0),
    B1 is -B0.

legal_move([M0, C0, B0], m, [M1, C0, B1]) :-
    M1 is M0 + B0,
    legal_state(M1, C0),
    B1 is -B0.

legal_move([M0, C0, B0], mc, [M1, C1, B1]) :-
    M1 is M0 + B0,
    C1 is C0 + B0,
    legal_state(M1, C1),
    B1 is -B0.

legal_state(M, C) :-
    between(0, 3, M),
    between(0, 3, C),
    safe(M,C),
    M1 is 3-M,
    C1 is 3-C,
    safe(M1, C1).

safe(0, _).
safe(M, C) :- M >= C.
