Create a hot and cold game in Matlab to find a point in a 20 by 20 matrix. The u
ID: 3578478 • Letter: C
Question
Create a hot and cold game in Matlab to find a point in a 20 by 20 matrix. 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 the computer plays the game. To do so, start at 0,0. Then pick the next point at the center (10,10) and calculate the distance. Then move slightly left and compare distance. If the distance is larger (further away from the point chosen by the user), delete half of the grid (that is colder-hence, eliminating areas where the point chosen by user will not be). Next, same as left to right, move up a small fraction. If the distance or radius is larger, then eliminate the top half of the grid. Keep going around and eliminating areas until distance is less than .5 of the point chosen by the user. Once the point is within a .5 distance, output the point along with the number of steps it took the program to find the point. You will use mostly loops and if-then statements.
Explanation / Answer
PROGRAM CODE
%find a point in 20*20 matrix
secretXval = randi([0 20], 1, 1)
secretYval = randi([0 20], 1, 1)
% enter the location of object.
pointXval = input('Enter x: ');
pointYval = input('Enter y: ');
% distanceval
distanceval = sqrt((secretXval - pointXval).^2 + (secretYval - pointYval).^2);
% using if condition to check the secret point
if pointXval == secretXval && pointYval == secretYval
printf('Good you got the secret point')
else
% while loop.
while true
%print the message.
printf("Not the secret point! Try again later! ")
% getx and y values
pointXval = input('Enter x value: ');
pointYval = input('Enter y value: ');
% if condition to check secret point x and y
if pointXval == secretXval && pointYval == secretYval
%print message.
printf('Good you got the secret point')
break;
end
%to store the value in temp variable
tempValue123 = sqrt((secretXval - pointXval)^2 + (secretYval - pointYval)^2);
% Check the condition of distanceval and temp value
if tempValue123 > distanceval
printf(' colder! ')
else
printf(' hotter! ')
end
distanceval = tempValue123;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.