% A number guessing game. User specifies an interval within which to
% generate a random integer and then has to guess the right number.

clc
% Level of difficulty. MUST BE POSITIVE.
difficulty= 10; % High-->difficult; low-->easy

disp('An integer guessing game...')
lo= input('Enter a value: ');
hi= input('Enter a larger value: ');
% Make sure that lo<hi before creating an interval
while hi<=lo
hi= input('Please enter a larger value: ');
end

% Create the interval with integer end points and generate a random integer
%%%%%%%%
inter= Interval(floor(lo),ceil(hi)); %%%% An Interval object is created
%%%%%%%%
answer= floor(rand()*(inter.right - inter.left + 1)) + inter.left;

% Start the guessing game
numGuesses= ceil((inter.right - inter.left)/difficulty);
fprintf('You have %d guess(es) to guess the generated integer', numGuesses)
fprintf(' in [%d,%d]\n', inter.left, inter.right)
guess= input('What is your guess? ');
k= 1;
while k < numGuesses && guess ~= answer
if guess < answer
fprintf('Your guess was too low. ')
else
fprintf('Your guess was too hi. ')
end
fprintf('The interval is [%d,%d].\n', inter.left, inter.right)
guess= input('What is your guess? ');
k= k+1;
end
if guess == answer
disp('Correct! You won!')
else % exhausted allowed number of guesses
fprintf('Better luck next time. The answer was %d.\n', answer)
end