Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Guessing Game Your task is to write a Matlab script in which the computer plays a guessing game with the user which goes as follows:

Guessing Game

Your task is to write a Matlab script in which the computer plays a guessing game with the user which goes as follows:

1. The user inputs a positive integer n, promising to think of an integer between 1 and n.

2. The computer then guesses what number the user is thinking of. If the guess is toosmall, the user should respond bigger. If the guess is too large, the user should respond smaller. Otherwise, the computer assumes that its guess is correct. The computer should use binary search to win as fast as possible.

3. If the computer wins, it should announce how many games it has won so far and offer to play again. If the user responds yes, another game starts with a new number n (and repeat as long as the user is willing to play). Otherwise, the program terminates.

4. If the computer detects that the user is cheating, it complains and quits in a huff.

In order to receive full credit, the program must have the following additional properties:

1. The game itself should be organized as a function

function result = playGame(n)

...

end

that takes the number n as input, plays the guessing game, and returns as result a value of 1 if it wins and 0 if it detects cheating.

2. The computer is only allowed to guess integers.

3. The program should have comments describing how it works.

Tips:

The main body of the program should probably be organized as a while

loop, as should most of the function playGame.

The computer should keep track of two numbers Low and High as upper and lower bounds on the possible answer. If the computer ever finds that Low > High, the user must be cheating (or mistaken).

If it is in the same M-file as your main script, the code for the function playGame must come at the end of your program.

Use a counter numWins to keep track of the number of wins.

MAKE SURE to Indent (and comment) the code to make it more legible.

Example runs of a correctly written program:

>> hw2

I want to play a guessing game!

Give me an upper bound: 5

Is the number 3? smaller

Is the number 1? bigger

Is the number 2? yup

I win! That makes 1 so far.

Would you like to play again? yes

Give me an upper bound: 30

Is the number 15? bigger

Is the number 23? bigger

Is the number 27? smaller

Is the number 25? fzrl

I win! That makes 2 so far.

Would you like to play again? yes

Give me an upper bound: 20

Is the number 10? bigger

Is the number 15? smaller

Is the number 12? smaller

Is the number 11? smaller

Cheater!

>>hw2

I want to play a guessing game!

Give me an upper bound: 17

Is the number 9? bigger

Is the number 13? smaller

Is the number 11? sure, why not

I win! That makes 1 so far.

Would you like to play again? no way

Okay, bye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

3. Is there opportunity to improve current circumstances? How so?

Answered: 1 week ago

Question

2. How will you handle the situation?

Answered: 1 week ago