/* turn this clause on to see that it makes the call to strategy.
 strategy(Coins, Weighings, DecisionTree) :- 
	write([Weighings,Coins]), nl, 2=3.*/

/*strategy(C, 0, s) :- 
	write(instrategy), nl, write(C), nl,
	C = coins(Normal, PossiblyHeavy, PossiblyLight, UnKnown),
	write(C), nl,
	Total is  PossiblyHeavy+ PossiblyLight+ UnKnown, 
	Total < 2.
*/
strategy(C,  Weighings, s) :-
	write(instrategy2), nl,  
	C = coins(Normal, PossiblyHeavy, PossiblyLight, UnKnown),
	plausible(Weighings, coins(Normal, PossiblyHeavy, PossiblyLight, UnKnown)),
	Weighings > 0,
	MaxSize is (Normal+  PossiblyHeavy+ PossiblyLight+ UnKnown ) / 2,
	between(1,MaxSize, Size),
	Left = coins(N1, H1, L1, U1), N1 = 0,
	pick(Size, coins(Normal, PossiblyHeavy, PossiblyLight, UnKnown),Left, Coins2),
	pick(Size, Coins2, Right, CoinsUnWeighed),
	Right = coins(N2, H2, L2, U2),
	CoinsUnWeighed = coins(N3, H3, L3, U3),

/* Now, you have to solve three subproblems for each of the 3 possible outcomes */
	Weighings1 is Weighings -1,
/* Check the plausibility of all the three subproblems before calling them */

  /* If the pans are Equal, All weighed coins are normal. Suspect in remainder. */

	N4 is Normal + H1+L1+U1 + H2+L2+U2,
	ProbEq = coins(N4, H3, L3, U3),
	plausible(Weighings1, ProbEq),

 /* If the Left pan was light, the UnWeighed ones are normal.
	The possibly_heavy on left and possibly_light on right are normal, But
	possibly_heavy on right and possibly_light on left are still suspect,
	and the unknowns on each side are possibly heavy/light */

	N5 is Normal 	+ H3+L3+U3 /* Unweighed */ + H1 + L2,
	H5 is H2 + U2,
	L5 is L1 + U1,
	U5 is UnKnown - (U1+U2+U3), /* i.e. 0 */
	ProbLeft = coins(N5, H5, L5, U5),
	plausible(Weighings1, ProbLeft),

 /* If the Right pan was light, the UnWeighed ones are normal.
	The possibly_heavy on right and possibly_light on left are normal, But
	The possibly_heavy on left and possibly_light on right are still suspect,
	and the unknowns on each side are possibly heavy/light */

	N6 is Normal 	+ H3+L3+U3 /* Unweighed */ + H2 + L1,
	H6 is H1 + U1,
	L6 is L2 + U2,
	U6 is UnKnown - (U1+U2+U3), /* i.e. 0 */
	ProbRight = coins(N6, H6, L6, U6),
	plausible(Weighings1, ProbRight).

between(M,N,M) :- N >= M.
between(M,N,X) :- N>M, M1 is M+1, between(M1,N,X).

pick(Size, coins(N1, H1, L1, U1), coins(N2, H2, L2, U2), coins(N3, H3, L3, U3)).

plausible(N,M) :- write(plaus(N,M)), nl. /* dummy */

?- strategy(coins(0,0,3,0), 1, T), write(hm), nl.
