canmiss(Liste_Etats) :- 
	etat_initial(E),
	solve([E],Liste_Etats).

solve([Ec|E],[Ec|E]):-
	etat_succes(Ec).

solve([[Mc,Cc,Rc]|S],L):-
	genere_etat_valide([Mc,Cc,Rc],[Mn,Cn,Rn]),
	\+ member([Mn,Cn,Rn],S),
	solve([[Mn,Cn,Rn],[Mc,Cc,Rc]|S],L).

genere_etat_valide([Mc,Cc,Rc],[Mn,Cn,Rn]):-
	select(M,C,Mc,Cc),
	Mo is Mc-M,
	Co is Cc -C,
	ok(Mo,Co),
	Mn is 3 - Mo,
	Cn is 3 - Co,
	ok(Mn,Cn),
	succ(Rc,Rn).

select(M,C,Mc,Cc):-
	enum(M,0,Mc),
	enum(C,0,Cc),
	MC is M+C,
	MC >= 1,
	MC =< 2.

etat_succes([3,3,droite]).
etat_initial([3,3,gauche]).

succ(gauche,droite).
succ(droite,gauche).

ok(0,C) :- C > 0.
ok(M,C) :- M >= C.


/* Utilitaires */

enum(X,_,X).
enum(X,Y,Z):- Z > Y,Z1 is Z-1, enum(X,Y,Z1).





