Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB code: Write a script let to play a simple number guessing game as follows

ID: 3689489 • Letter: M

Question

MATLAB code:

Write a script let to play a simple number guessing game as follows. The script should generate a random integer in the range 1, 2, 3, . . . , 14, 15. It should provide for the player to make repeated guesses of the number, and it should indicate if the player has won or give the player a hint after each wrong guess. The responses and hints are as follows:

“You won” and then stop the game.

“Very close” if the guess is within 1 of the correct number.

“Getting close” if the guess is within 2 or 3 of the correct number.

“Not close” if the guess is not within 3 of the correct number.

Explanation / Answer

Write a script let to play a simple number guessing game as follows. The script should generate a random integer in the range 1, 2, 3, . . . , 14, 15. It should provide for the player to make repeated guesses of the number, and it should indicate if the player has won or give the player a hint after each wrong guess. The responses and hints are as follows:

“You won” and then stop the game.

“Very close” if the guess is within 1 of the correct number.

“Getting close” if the guess is within 2 or 3 of the correct number.

“Not close” if the guess is not within 3 of the correct number.

Solution:

function GuessNumberFeedbackPlayer

    lowVal = input('Lower limit: ');

    highVal = input('Upper limit: ');

    fprintf('Think of your number. Press Enter when ready. ')

    pause

    nGuesses = 1;

    done = false;

    while ~done

        guess = floor(0.5*(lowVal+highVal));

score = input(sprintf( ... 'Is %d Very Close (V), Not Close (N), or Getting Close (G)? ', guess), 's');

        if any(strcmpi(score, {'v' 'V' 'Very' 'Very Close' '+'}))

            highVal = guess-1;

            nGuesses = nGuesses+1;

        elseif any(strcmpi(score, {'n' 'N' 'Not' 'Not Close' '-'}))

            lowVal = guess+1;

            nGuesses = nGuesses+1;

        elseif any(strcmpi(score, {'g' 'G' 'Getting' 'Getting Close ' '='}))

            fprintf('Yay! You won in %d guesses. ', nGuesses)

            done = true;

        else

            fprintf('Unclear response. Try again. ')

        end

        if highVal < lowVal

            fprintf('Incorrect scoring. No further guesses. ')

            done = true;

        end

    end

end

Output:

Lower limit: 0

Upper limit: 50

Think of your number. Press Enter when ready.

Is 25 Very Close (V), Not Close (N), or Getting Close (G)? N

Is 38 Very Close (V), Not Close (N), or Getting Close (G)? V

Is 31 Very Close (V), Not Close (N), or Getting Close (G)? V

Is 28 Very Close (V), Not Close (N), or Getting Close (G)? N

Is 29 Very Close (V), Not Close (N), or Getting Close (G)? G

Yay! You won in 5 guesses.

Lower limit: 0

Upper limit: 10

Think of your number. Press Enter when ready.

Is 5 Very Close (V), Not Close (N), or Getting Close (G)? N

Is 8 Very Close (V), Not Close (N), or Getting Close (G)? hello

Unclear response. Try again.

Is 8 Very Close (V), Not Close (N), or Getting Close (G)? V

Is 6 Very Close (V), Not Close (N), or Getting Close (G)? N

Is 7 Very Close (V), Not Close (N), or Getting Close (G)? V

Incorrect scoring. No further guesses.