Task

Write a player for the game that follows the following rules:

The scorer will choose a number between set limits. The computer player will print a guess of the target number. The computer asks for a score of whether its guess is higher than, lower than, or equal to the target. The computer guesses, and the scorer scores, in turn, until the computer correctly guesses the target number.
The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.



Related tasks

   Guess the number/With Feedback
   Bulls and cows/Player


function GuessNumberFeedbackPlayer
 
    lowVal = input('Lower limit: ');
    highVal = input('Upper limit: ');
    fprintf('Think of your number. Press Enter when ready.\n')
    pause
    nGuesses = 1;
    done = false;
    while ~done
        guess = floor(0.5*(lowVal+highVal));
        score = input(sprintf( ...
            'Is %d too high (H), too low (L), or equal (E)? ', guess), 's');
        if any(strcmpi(score, {'h' 'hi' 'high' 'too high' '+'}))
            highVal = guess-1;
            nGuesses = nGuesses+1;
        elseif any(strcmpi(score, {'l' 'lo' 'low' 'too low' '-'}))
            lowVal = guess+1;
            nGuesses = nGuesses+1;
        elseif any(strcmpi(score, {'e' 'eq' 'equal' 'right' 'correct' '='}))
            fprintf('Yay! I win in %d guesses.\n', nGuesses)
            done = true;
        else
            fprintf('Unclear response. Try again.\n')
        end
        if highVal < lowVal
            fprintf('Incorrect scoring. No further guesses.\n')
            done = true;
        end
    end
end