function [bomb_out,bomb_stats] = Bomb_Dropper( gridsize,bombs,plot_yes_or_no_1or0 )
%Bomb_Dropper drops bombs bombs on a square grid of size gridsize
%   put a 1 in for the third variable to see the outputs plotted and to get
%   a plot of what the Poisson Distribution predicts

%initializes the grid we're going to drop bombs on
bomb_out=zeros(gridsize);

%gets the number of entries in the grid for later use in computations
length_grid=gridsize^2;

%this loop does the actual bomb dropping one at a time. details are in the loop. we run
%through and drop each bomb individually... and that's why there is a loop.
for i=1:bombs
    %for each bomb, we pick a ranom location in the grid to drop the bomb.
    location=randi(length_grid);
    
    %now we drop it by adding 1 to whatever was previous stored in that
    %location
    bomb_out(location)=bomb_out(location)+1;
end

%if we are plotting the output variables, we plot what we just built in the
%above bomb dropping loop. note that "colorbar" simply inserts a legend for
%the colors that get used for saying how many bombs are in each grid entry.
if plot_yes_or_no_1or0==1
    figure(1);
    imagesc(bomb_out); colorbar;
    title(['bomb_hits']);
end


%to do the statistics study, we first see what is the largest number of
%bombs that hit any one grid spot. we have to apply max twice because of
%the way matlab deals with vectors.
max_num_bombs=max(max(bomb_out));

%initializing the variable we are going to store the statistics in
bomb_stats=zeros(1,max_num_bombs+1);

%run through each number of bombs per grid up to max_num_bombs and see how
%many entries have that many number of bomb hits. note the ith entry holds
%the statistics about "i bombs per grid." bomb_stats(0) represents how many
%grid entries didn't get hit by a single bomb
for i=1:max_num_bombs
    inds=find(bomb_out==i);
    length_i=length(inds);
    bomb_stats(i+1)=length_i;    
end

%just a little script to figure out how many grid spots we have yet to
%count and those all coorespond to how many grids got zero bombs
num_grids_so_far=sum(bomb_stats);
bomb_stats(1)=length_grid-num_grids_so_far;

%creating a variable to facilitate the plotting of the statistics
X=0:max_num_bombs;


%create the poisson expectation
compare_poisson_dist=zeros(1,max_num_bombs+1);
for i=1:max_num_bombs+1
    compare_poisson_dist(i)=length_grid*poisspdf(i-1,bombs/length_grid);
end

%getting the vertical scale we will use for both graphs below
max_pois=max(compare_poisson_dist);
max_observed=max(bomb_stats);
max_both=max(max_pois,max_observed);
    
%plot the stats, and the Poisson Distribution
if plot_yes_or_no_1or0==1
   figure(2); 
   plot(X,bomb_stats);
   title('actual stats');
   axis([0 max_num_bombs 0 max_both]);
   figure(3);
   plot(X,compare_poisson_dist);
   title(['Poisson Prediction']);
   axis([0 max_num_bombs 0 max_both]);
end
    
end

