In: Advanced Math
Create a hot and cold game in Matlab to find a point in an (X,Y) 2D coordinate plane. The user answers two inputs: x=Input('x') y=Input('y'). THE INPUTS WILL ONLY BE INTEGERS FROM 0 TO 100. This means no negative numbers. You write the program so the computer plays the game. The computer must play the game (comparing distances only) and give the same point as the user inputs. To keep it simple the code will check distances until distance = 0 (right on the point). certain section can be eliminated by comparing distances. You will use mostly loops and if-then statements. ONLY PROMPT THE USER TO INPUT THE "SECRET" X AND Y LOCATION once. THE COMPUTER WILL FIND THE POINT. DO NOT KEEP PROMPTING THE USER TO KEEP GUESSING.
%Matlab code for hot and cold game
clear all
close all
%promt for inital x and y
prompt1 = 'What is the value of x? ';
x = input(prompt1);
prompt2 = 'What is the value of y? ';
y = input(prompt2);
%initial search range for x and y
a1=0; b1=100;
a2=0; b2=100;
%initialize loop
dist=10; it=0;
while dist~=0
it=it+1;
x1= randi([a1 b1],1);
y1= randi([a2 b2],1);
dist=sqrt((x-x1)^2+(y-y1)^2);
if x<x1
b1=x1;
else
a1=x1;
end
if y<y1
b2=y1;
else
a2=y1;
end
fprintf('\tAfter %d iteration x=%d and
y=%d\n',it,x1,y1)
end
fprintf('Hence the secret location is %d and %d\n',x1,y1)
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%