In: Computer Science
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than 1. The program will consist of mostly loops and if-then statements. ONLY PROMPT THE USER TO INPUT THE "SECRET" X AND Y LOCATION. THE COMPUTER WILL FIND THE POINT. DO NOT KEEP PROMPTING THE USER TO KEEP GUESSING. IT IS THE PROGRAM'S JOB TO FIND THE POINT THE USER HAS CHOSEN. Program is finding a point the user picks in a 20X20 matrix. It also needs to have a counter built in so you know how many steps it takes the program to find the x,y coordinate. Points will be chosen using logic, not random.
The following information has been provided for the question above:
NOTE: I used MATLAB R2017a IDE to execute this program code.
Screen shot of Matlab code:
Output:
Text format code:
%Computer will find the secret pick point by
%using20 x 20 matrix
computerSecretPickforX = randi([0 20], 1, 1)
computerSecretPickforY = randi([0 20], 1, 1)
%Prompt and read the input from the user
x=input('what is the x location of the object?')
y=input('what is the y location of the object?')
%Calculates the distance
calcDistance = sqrt((computerSecretPickforX - x).^2 +
(computerSecretPickforY - y).^2);
% Verify the user guess input and computer pick is same or
not
% If it is matched, then print the success prompt message,
% Otherwise using loop repeat the steps until the find the
% secret points
if x == computerSecretPickforX && y ==
computerSecretPickforY
fprintf('Congrates! You got success')
else
%While execute till condition is true.
while true
%Display message.
fprintf("Not matched
with computer secret point! Try again!\n")
% Prompt the user to get x and
y value.
x=input('what is the x
location of the object?')
y=input('what is the y
location of the object?')
if x ==
computerSecretPickforX && y == computerSecretPickforY
%Display message.
fprintf('Congrates! You got success')
break;
end
counter =
sqrt((computerSecretPickforX - pointX)^2 + (computerSecretPickforY
- pointY)^2);
%Verify the secret point
is very near match to
%computer pick point
then prompt the colder message,
%otherwise prompt the
hotter message
if counter >
calcDistance
fprintf('Going colder...\n')
else
fprintf('Going hotter...\n')
end
calcDistance =
counter;
end
end