/* Men's Public Toilet Advise Expert System */

go:-helpme.
run:-helpme.
helpme:- 
	write('* Welcome to the PUBLIC TOILET EXPERT SYSTEM (R)(TM) :-) *'), nl,
	write('Please input the toilet situation in the following form:'), nl,
	write('      empty - o'), nl,
	write('      ocupied - x, in the form of a list.'), nl,
	write('for example  [o,x,o,x,o,o,o,o,x,o,o].'), nl,
	read(Toilet_Layout), nl,
	getadvice(Toilet_Layout).

ask_for_more:-
	write('Do you want more suggestions? y./n. '),
	read(X),nl,
	X \= y.

/* standing next to one of the walls with no-one next to you is the best option. Even if more people come, you can't have more than one person standing next to you. */
left_wall([H1,H2|_]) :- H1=o, H2=o, write('Choose the very left urinal, this way, even if more people come, you can  have only one man standing next to you.'), nl, ask_for_more.

right_wall([]):- fail.
right_wall([H1,H2|T]) :- H1=o,H2=o,T=[], write('Choose the very right, this way, even if more people come, you can have only one man standing next to you'), nl,ask_for_more.
right_wall([_|T]) :- right_wall(T).

/* if that is not possible, the second best is to choose the middle one of three empty. This way, you also don't have anyone next to you, but it may happen that if more people come, you'll have two people standing next to you. */
middle_urinal(L) :- find_middle_urinal(L,Adv), 
	write('Choose urinal number '),write(Adv),write(', you will not have anyone standing next to you'),nl,ask_for_more.

find_middle_urinal([],_):-fail.
find_middle_urinal([H1,H2,H3|_],X):-H1=o,H2=o,H3=o, X=2.
find_middle_urinal([_|T], FinalAdvice) :- find_middle_urinal(T,X), FinalAdvice is X+1.


/* if there are not three empty in a row, than look for empty first or last. This way you'll have one man next to you, but you will be safe from the side of the wall, so it can't get any worse */

first_urinal([H1|[H2|_]]) :- H1=o, H2=x, write('Choose the first, you will have one man standing next to you, but you will be safe from the left.'), nl, ask_for_more.

last_urinal([]):-fail.
last_urinal([H1,H2|[]]):-H1=x,H2=o.
last_urinal([_|T]) :- last_urinal(T), write('Choose the last, you will have one man standing next to you, but you will be safe from the right.'), nl, ask_for_more.


/* if all above fails, choose any one of a pair of empty urinals, this way, you'll be standing next to a man a and if more people come, you may end up surrounded */
any_urinal(L) :- find_any_urinal(L, X), 
	write('Choose urinal number '),write(X),write(', you will have only one man standing next to you'), nl,ask_for_more.

find_any_urinal([],_):-fail.
find_any_urinal([H1,H2,H3|_],X):-(H1=o,H2=o;H2=o,H3=o), not(H1=H3), X=2.
find_any_urinal([_|T], FinalAdvice) :- find_any_urinal(T,X), FinalAdvice is X+1.


getadvice(L):-
	left_wall(L). /*Two empty at either end? The best choice! */
getadvice(L):-
	right_wall(L).
getadvice(L):-
	middle_urinal(L). /* if not, three empty in the middle are also good */
getadvice(L):-
	first_urinal(L). /* if not, the first one or the last one will do */
getadvice(L):-
	last_urinal(L).
getadvice(L):-
	any_urinal(L). /* if not, then any one in a pair of empty will have to do */
getadvice(_):-
	write( 'Go for a cabin'), nl. /* when all above fails, rather choose to go into a cabin then to stand between two men */


/* 2001 Jan Strojil, an ERASMUS student with perhaps too much time to spare.... */
/* TO THE FULLEST EXTENT PERMITTED AT LAW, I AM PROVIDING THIS SYTEM AND ITS CONTENTS ON AN "AS IS" BASIS AND MAKE NO (AND EXPRESSLY DISCLAIM ALL) REPRESENTATIONS OR WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, WITH RESPECT TO THIS SYSTEM OR THE INFORMATION, CONTENT, MATERIALS INCLUDED IN THIS SYSTEM INCLUDING, WITHOUT LIMITATION, WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN ADDITION, I DO NOT REPRESENT OR WARRANT THAT THE INFORMATION ACCESSIBLE VIA THIS SYSTEM IS ACCURATE, COMPLETE OR CURRENT. I WILL NOT BE LEGALY HELD LIABLE FOR ANY LOSS, DAMAGE OR DEATH CAUSED BY THE USE OF THIS SYSTEM. THANK YOU. :-))) */