% bottles4.pl

% Solution based on search in prolog database
%
bottles(X) :-
	retractall( bottle(_) ),
	create_bottles(X),
	( take_bottles;
 	  format("Now, they are all gone.")
	).

create_bottles(0).
create_bottles(X) :-
	asserta( bottle(X) ),
	X1 is X - 1,
	create_bottles(X1).

find_bottle(X) :-
	bottle(X),
	not( (bottle(Y), Y > X) ).

take_bottles :-
	repeat,
	find_bottle(X),
 	retract(bottle(X)),
	(
	  X = 1,  
	    format("1 bottle of beer on the wall. "), !;
	  X > 1,
	    format("~d bottles of beer on the wall. ", X)
	),
	format("Take one down, and pass it around.\n"),
	fail.


