
% writeMsg(Msg)
% -------------
% simple routine to display a text message
writeMsg([]).
writeMsg([H|T]) :- put(H), writeMsg(T).


% getBetween(M, N, Answer)
% ------------------------
% routine to get a user to enter a number in the range M to N,
%    they must keep trying until they provide something value
getBetween(M, N, Answer) :- 
   % make sure M and N are valid
   number(M), number(N), M =< N,
   % get them to give a response
   writeMsg("please enter a number from "),
   write(M), writeMsg(" to "), write(N), nl,
   read(Answer),
   % check that it is valid
   number(Answer), Answer =< N, Answer >= M, 
   % if it is valid then this is their final answer
   !.

% if the above failed it is because they entered something invalid,
%    so display an error message and try again (recursive)
getBetween(M, N, Answer) :-
   writeMsg("That answer was not valid, please try again"), nl,
   getBetween(M, N, Answer).


% guess
% -----
% has the computer pick a random number from 1-10
%     and allows the user to try to guess it
guess :- % initialize the random number generator and pick a number
         random(1, 10, Mine), 
         % get them to pick a valid number in 1..10
         getBetween(1,10,Theirs), 
         % tell them what the computer choice was
         writeMsg("My number was "), write(Mine), nl, 
         % succeed if they were right, fail otherwise,
         %    but include a cut so that if they were 
         %    wrong it doesn't backtrack to getBetween
         %    and give them another try
         !, Mine = Theirs.

