MATLAB Code that I have: clear all;close all; izombies = 1; time = 10; ihumans =
ID: 2293246 • Letter: M
Question
MATLAB
Code that I have:
clear all;close all;
izombies = 1;
time = 10;
ihumans = input('how many humans will enter: ');
if (ihumans == 0)||(ihumans > 300)
print('error');
ihumans = 1; %default value
end
zombie_pos = zeros(1,2);
zombie_pos(1,1) = 100*rand();
zombie_pos(1,2) = 100*rand();
human_pos = zeros(ihumans,2);
for i=1:ihumans
human_pos(i,1) = 100*rand();
human_pos(i,2) = 100*rand();
end
while (izombies>0)&&(ihumans>0)
dist = zeros(ihumans,izombies);
for j =1:izombies
for i = 1:ihumans
x_dist = human_pos(i,1) - zombie_pos(j,1);
y_dist = human_pos(i,2) - zombie_pos(j,2);
dist(i,j) = sqrt(x_dist^2+y_dist^2);
end
end
%plot the position of humans and zombies
for i=1:izombies
scatter(zombie_pos(i,1),zombie_pos(i,2),[],'b');
hold on
end
% for j = 1:ihumans
% scatter(human_pos(j,1),human_pos(j,2),[],'r');
% end
scatter(human_pos(:,1),human_pos(:,2),[],'r');
pause(0.1);
hold off;
axis([0,100,0,100]);
xlabel('x position');
ylabel('y position');
%find the indexes with distance less than 1
[k_row,k_col] = find(dist<1);
if(isempty(k_row)) %check if empty
else
[n1,n2] = size(k_row);
for i =1:n1
%convert to zombie
zombie_pos = [zombie_pos;human_pos(k_row(i),:)]; %#ok<AGROW>
izombies = izombies+1;
human_pos(k_row(i),:) = [];
ihumans = ihumans-1;
end
end
time = time-1;
%let the zombies wander
for j = 1:izombies
zombie_pos(j,1) = zombie_pos(j,1)+(-1+2*rand);
zombie_pos(j,2) = zombie_pos(j,2)+(-1+2*rand);
if(zombie_pos(j,1)<=0)
zombie_pos(j,1)=0;
end
if(zombie_pos(j,1)>=100)
zombie_pos(j,1)=100;
end
if(zombie_pos(j,2)<=0)
zombie_pos(j,2)=0;
end
if(zombie_pos(j,2)>=100)
zombie_pos(j,2)=100;
end
end
%let the humans wander
for j = 1:ihumans
human_pos(j,1) = human_pos(j,1)+3*(-1+2*rand);
human_pos(j,2) = human_pos(j,2)+3*(-1+2*rand);
if(human_pos(j,1)<=0)
human_pos(j,1)=0;
end
if(human_pos(j,1)>=100)
human_pos(j,1)=100;
end
if(human_pos(j,2)<=0)
human_pos(j,2)=0;
end
if(human_pos(j,2)>=100)
human_pos(j,2)=100;
end
end
end
How do I define the zombie's lifespan in this code so that they die off after a specific amount of time in code?
How do I make a code on this that makes the program stop after either all the zombies die or all the humans die?
And how to make a code that makes them stay in a 100 meter X 100 meter by bouncing off walls?
Please help
Explanation / Answer
Predefine zombie's lifespan as a variable and decrement that variable for each day.
Write code like to end the program when the above defined variable became zero or izombies=0 or ihumans=0.
Define positions as square form 100mX100m in the code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.