```% BULLIES & DWEEBS
% copyright by R.J. Marks II "My software ain't purdy but she works".
%
% There are bullies and dweebs.  Bullies (red diamonds) try to kill the Dweebs (blue squares).
% White +'s correspond to dead Dweebs.
% There are two simple rules and an option.
%		RULE 1: Each Dweeb determines the closest bully and takes a step away.
%		RULE 2: Each Bully determines the closest Dweeb and takes a step towards.
%		Both Bullies and Dweebs can have "twiddle" (or jitter). At a specified probabability
% Many emerging features occur:
% 1. Dweeb flocking
% 2a. Bully stacking (occurs with no Bully twiddle.  This makes the bullies less effective).
% 2b. Bully Mobs (occurs with Bully twiddle).
% 3. Team Work - two or more Bullies work effectively when closing in on a flock of Dweebs from differenct sides.
% 4. Cornering - Bullies get Dweebs by cornering them - or backing them against a wall.
% 5. Voronoi Partitioning emerges naturally.  Set the variable "vor" to 1 to keep the Bullies stationary.
%		The Dweebs then line up on the Voronoi partitioning boundaries.
% 6. Corner massacres (especially if there is no Dweeb twiddle.)  Mid field massacres nomally require "cooperating"
%		Bullies.
%
% PARAMETERS (must be changed in m-file)
% 		NX, NY = dimensions of the playground.
% 		bullies = number of Bullies;
%		dweebs = number of Dweebs
% 		ann = probability of a Dweeb random step up, down, left and right.  If =1/2, there is always a random step.
%		btwidle = probability of a Bully random step up, down, left and right
%		bcorx & bcory = if either is >0 all bullies start at one point at percentage of field.
% 			Example: Setting both = 0.5 stacks all the Bullies in the field middle.
%			Otherwise, the bully spacing is random.
%		dcorx & dcory 	= same for Dweebs.
% 		flicks = if greater than zero, a movie is made and played "flicks" times. 1/2 makes a movie that doesn't play
% 		generations = number of steps in the bullies-dweeb game.
%		mpeg = 1 makes an MPG of the movie.  Requires the Matlab software mpgwrite.m
% 		vor = a zero flag, when set to one, paralyzes the bullies.  Voronoi partitioning results.
%		bins = number of bins in a log-log histogram of Dweeb deaths.  If set to zero, no histogram is made.
%		dweebplots = 1 gives plots of Dweeb count (in 'dweebcount') and Dweeb deaths (in 'D').
close all
clear all
%
% PARAMETERS
NX=50; NY=50;		% This is the size of the matrix where the Bullies & Dweebs game takes place.
generations=75;		% The number of steps each Dweeb and each Bully take.
double=0;    			% Leave at 0.  If double =1, then each dweeb and each bully update is viewed.
bullies=25;			   % Number of Bullies
dweebs=1500;			% Number of Dweebs
ann=1/6;				   % dweeb twiddle (< 0.5)
btwidle=1/6;			% bully twiddle (<0.5)
bcorx=0;bcory=0;		% if >0 starts all bullies at one point at percentage of field. Otherwise, randomly spacing.
% Example: Setting both = 0.5 stacks all the Bullies in the middle.
dcorx=0;dcory=0;	   % if >0 starts all dweebs at one point at percentage of field. Otherwise, they are random.
flicks=3;				% when the program is over, this is how many times the movie is played.
mpeg=0;					% generates and mpg file when done.  Requires the code mpgwrite.m
bins=0;
vor=0;  					% set to 1 to immobilize the bullies.  Voronoi Partitioning results.
dweebplots=1;			% gives dreeplots at the end
%
% INITIALIZE
Playground=ones(NX+5,NY);
frame=1;
dweebstatus=ones(dweebs,1);
if(bcorx+bcory==0); bullyx=ceil(NX*rand(bullies,1)); bullyy=ceil(NY*rand(bullies,1));
else
bullyx=(floor(bcory*NX)+1)*ones(bullies,1); bullyy=floor((bcory*NY)+1)*ones(bullies,1);
end
if(dcorx+dcory==0);dweebx=ceil(NX*rand(dweebs,1)); dweeby=ceil(NY*rand(dweebs,1));
else
dweebx=floor((dcorx*NX)+1)*ones(dweebs,1); dweeby=floor((dcory*NY)+1)*ones(dweebs,1);
end
dweeprepx=repmat(dweebx,1,bullies);dweeprepy=repmat(dweeby,1,bullies);
dweebcount=zeros(generations,1);
%
% GENERATIONS
for g=1:generations
bullrepx=repmat(bullyx,1,dweebs);bullrepy=repmat(bullyy,1,dweebs);
ddx=bullrepx-dweeprepx';   ddy=bullrepy-dweeprepy';
distMAT=ddx.*ddx+ddy.*ddy;
[y,j]=min(distMAT);% j is index vector of closest bully
for i=1:dweebs   %dweebs running
if(dweebstatus(i)==1)
for n=1:bullies	% Checking for new capture
if(bullyx(n)==dweebx(i))
if(bullyy(n)==dweeby(i))
dweebstatus(i)=0;
dweebx(i)=10*NX; dweeby(i)==10*NY;
break
end
if(dweebstatus(i)==0); break; end
end
end
if(dweebstatus(i)==1)
vecx=sign(dweebx(i)-bullyx(j(i)));vecy=sign(dweeby(i)-bullyy(j(i)));
rr=rand;dweebx(i)=dweebx(i)+vecx+ceil(ann-rr)-ceil(rr-1+ann);
rr=rand; dweeby(i)=dweeby(i)+vecy+ceil(ann-rr)-ceil(rr-1+ann);
if(dweebx(i)>NX);dweebx(i)=NX; end; if(dweebx(i)<1);dweebx(i)=1; end
if(dweeby(i)>NY);dweeby(i)=NY; end; if(dweeby(i)<1);dweeby(i)=1; end
end
end
end
%display
if(double==1)
imagesc(Playground);
hold on
for n=1:dweebs;
if(dweebstatus(n)==1);
plot(dweebx(n),dweeby(n),'bs');hold on;
else
end;
end
for n=1:bullies; plot(bullyx(n),bullyy(n),'rd');hold on; end
title(g);
if(flicks==0)
M=getframe;
else
M(frame)=getframe;
end
hold off;
frame=frame+1;
end
dweeprepx=repmat(dweebx,1,bullies);dweeprepy=repmat(dweeby,1,bullies);
ddx=bullrepx-dweeprepx';   ddy=bullrepy-dweeprepy';
distMAT=ddx.*ddx+ddy.*ddy;
[y,jj]=min(distMAT');% jj is index vector of closest dweeb
if(vor==0)
for i=1:bullies
vecx=sign(dweebx(jj(i))-bullyx(i));vecy=sign(dweeby(jj(i))-bullyy(i));
rr=rand;bullyx(i)=bullyx(i)+vecx+ceil(btwidle-rr)-ceil(rr-1+btwidle);
rr=rand;bullyy(i)=bullyy(i)+vecy+ceil(btwidle-rr)-ceil(rr-1+btwidle);
if(bullyx(i)>NX);bullyx(i)=NX; end; if(bullyx(i)<1);bullyx(i)=1; end
if(bullyy(i)>NY);dweeby(i)=NY; end; if(bullyy(i)<1);dweeby(i)=1; end
end
end
imagesc(Playground);
hold on
for n=1:dweebs;
if(dweebstatus(n)==1);
plot(dweebx(n),dweeby(n),'bs');hold on;
else
end;
end
for n=1:bullies; plot(bullyx(n),bullyy(n),'rd');hold on; end
title(g);
M(frame)=getframe;
hold off
frame=frame+1;
% Checking for captures
if(min(jj)==0)
for n=1:dweebs
if(jj(n)==0)
dweebstatus(n)=0;
end
end
end
dweebcount(g)=sum(dweebstatus);
if(dweebcount(g)==0); break; end
end %generations
figure
title('Bullies & Dweebs')
if(flicks>=1)
movie(M,flicks)
end
if(mpeg==1)
mpgwrite(M,colormap,'Dweebs & Bullies')
end
if(dweebplots==1)
figure
plot(dweebcount);titie('Dweeb Count')
figure
D=-diff(dweebcount)
plot(D);titie('Dweeb Deaths Each Generation')
end
% Generating log-log histogram
if(bins>5)
fell=-diff(dweebcount);
histlow=min(fell)+3;				% Low Point of log-log histogram
histhi=max(fell)+1;				% High Point of log-log histogram
rati=histhi/histlow;
histog=ones(bins+1,1);
for g=1:generations-1
for b=1:bins+1
if(and(histlow*(rati^((b-1)/bins))<fell(g),fell(g)<=histlow*(rati^(b/bins))))
histog(b)=histog(b)+1;
break
end
end
end
figure 