Use a switch statement to process the users response of high low or correct Use

# Use a switch statement to process the users response

This preview shows page 4 - 6 out of 7 pages.

Use a switch statement to process the user’s response of high, low, or correct Use variables named lowLimit and highLimit to narrow do wn the range of the computer’s guesses %% Problem 5 - Computer Guess Number clear clc % Set parameters and set limits for guess nMax = 20; disp([ 'Think of a number between 1 and ' num2str(nMax), ... ' and I''ll guess it' ]); lowLimit = 1; highLimit = nMax; gotIt = false; % Output guess from the computer % Ask user to hint of too low, high, or correct while ~gotIt nGuess = round((highLimit+lowLimit)/2); disp([ 'I guess ' num2str(nGuess)]); hint = input( 'Too low (l), too high (h), correct (c): ' , 's' ); switch hint case 'l' lowLimit = nGuess; case 'h' highLimit = nGuess; case 'c' gotIt = true; otherwise disp( 'I didn''t understand' ) end end disp( 'Thanks for playing!' ); OUTPUTS Think of a number between 1 and 20 and I'll guess it I guess 11 Too low (l), too high (h), correct (c): l I guess 16 Too low (l), too high (h), correct (c): h I guess 14 Too low (l), too high (h), correct (c): h I guess 13 Too low (l), too high (h), correct (c): c Thanks for playing!
6. RockPaperScissors.m Write a MATLAB program that plays the game “Rock, Paper, Scissors.” The rules of the game are as follows: Two players face off and on the count of 3 simultaneous “throw” a “rock” (fist), paper (o pen hand), or “scissors” (two fingers) to indicate their choice. The winner is determined according to the following rules: o paper covers rock (paper beats rock) o rock breaks scissors (rock beats scissors) o scissors cuts paper (scissors beats paper) o If both players throw the same object, the game is a tie In your program, the computer should take the role of one player and the human user should take the role of the other. The computer should randomly pick one of the three object (but not say which) and then prompt the user to pick one. The program should then analyze the choices and decide who wins.

#### You've reached the end of your free preview.

Want to read all 7 pages?