%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Eng 101, Sec 200                                           %
%Assignment 8: Swarm Ball                                   %
%filename: swarmball.m                                      %
%v1.2b                                                      %
%Notes:                                                     %
%                                                           %
%        This program runs BEST on Matlab5.3, 6 is slow     %
%      This implements the player direction using some      %
%      Rand in the direction and a length finding algorithm %
%      so that the player moves in a direction that brings  %
%      it closer to the ball although it may not be the best%
%      direction, which I believe to be a little more like  %
%      the nature of swarm ball, and adds some fun to the   %
%      display																%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%--------------------UI-------------------------%
slow = 1;

%---------------Init and Define ----------------%
%Field Size
fieldWidth = 31;
fieldLength = 41;

%Color Defs
grass = 0.5;
ball = 0.62;
red = 0.8; 
redGoal = 0.7;
blue = 0.2;
blueGoal = 0.4;
boundary = 0;

%Define field
field = zeros(fieldWidth,fieldLength);
field(2:fieldWidth-1,2:fieldLength-1) = grass;
field(10:22,1) = redGoal;
field(10:22,end) = blueGoal;

%Convert Field to boosted size for manipulation & Plotting
field(fieldWidth + 1,:) = 0; %boost rows by one and assign as zeros
field(:,fieldLength + 1) = 1; %boost cols by one and assign as ones (pin high)
figure(1);

%Place Initial characters & ball
ballPos = [21;16]; %The Balls sarting position
field(ballPos(2),ballPos(1)) = ball;
redPos = [17 17 17 11 11 11;
          13 16 19 10 16 22];
bluePos = [25 25 25 31 31 31;
           13 16 19 10 16 22];
for Pos = redPos
   field(Pos(2),Pos(1)) = red;
end
for Pos = bluePos
   field(Pos(2),Pos(1)) = blue;
end
clear Pos

%------------------Display F--------------------%
pcolor(field); %Make the plot itself (test on v 6)
title('Starting Positions')


%----------------Play Game Loop-----------------%
Steps = 0;
playGame = 1;
win = 0;
if (slow == 1)
   pause(0.5)
end

while(Steps < 121 & playGame == 1)
         
   %========Advance Players
   for player = 1:6
      %for each player pair, randomize which team moves first
      if (rand > 0.5)
         teamOrder = [1 2]; %red team first
      else 
         teamOrder = [2 1]; %blue team first
      end
      for team = teamOrder 
         %input position of player into algorithm of appropiate team and #
         if (team == 1) %red team
            Pos = redPos(:,player);
            field(Pos(2),Pos(1)) = grass;
         else
            Pos = bluePos(:,player);
            field(Pos(2),Pos(1)) = grass;
         end
         
         %find Direction to ball
         dirtemp = ballPos - Pos;
         dista = abs(dirtemp(1)+i*dirtemp(2)); %distance from current position to ball 
         distb = 1000;    
         count = 1;
         Dirs = [1 1 1 -1 -1 -1 0 0 0;
                 0 -1 1 1 -1 0 1 -1 0];
         while(distb>dista)
            dz=Dirs(:,int32(rand*8+1));
            PosTemp = [Pos(1)+dz(1);Pos(2)+dz(2)];
            dirtempb = ballPos - PosTemp;
            distb = abs(dirtempb(1)+i*dirtempb(2)); %distance from current position to ball 
            count = count + 1;
         end
         
         dy = dz(2);
         dx = dz(1);
         
         %check for other players & boundries and whatnot
         if (field(dy+Pos(2),dx+Pos(1)) ~= grass)
            if (field(Pos(2),dx+Pos(1)) == grass)
               dy = 0;
            elseif (field(dy+Pos(2),Pos(1)) == grass)
               dx = 0;
            else
               dx = 0;
               dy = 0; 
            end
         end
         			         
         
         %Move Player to new location
         Pos = [dx+Pos(1);dy+Pos(2)];
   
         
         %output position of player to position matrix of appropiate team and #
         if (team == 1) %red team
            redPos(:,player) = Pos;
            field(Pos(2),Pos(1)) = red;
         else
            bluePos(:,player) = Pos;
            field(Pos(2),Pos(1)) = blue;
         end
     
      end
   end
   clear Pos
   clear team
   clear dx
   clear dy 
   
      
   %========Count players around ball
   redKick = 0;
   blueKick = 0;
   for dx = -1:1
      for dy = -1:1
         team = field(ballPos(2)+dy,ballPos(1)+dx);
         if (team == red)
            redKick = redKick + 1;
         elseif (team == blue)
            blueKick = blueKick + 1;
         end
      end
   end
   %========Determine Kicking Team
   if (redKick == 0 & blueKick ==0)
      %disp('No Kick')
      kick = 0;
   elseif(rand<redKick/(redKick+blueKick))
      %disp('Red Kicks')
      kick = 1;
   else
      %disp('Blue Kicks')
      kick = -1;
   end

     
   %========Pick Ball Direction (w/ boundry checking)
   if(kick)
      %randomize the y direction
      randKick = rand;
      if (randKick <= 0.3333)
         kicky = -1;
      elseif (randKick <= 0.6667)
         kicky = 0;
      else
         kicky = 1;
      end
      %slightly randomize the x direction
      if (rand < 0.75)
         kickx = kick;
      else
         kickx = 0;
      end
      %check for a complete miss
      if (kickx == 0 & kicky == 0)
         kick = 0;
      end
   end
   
   %========Redisplay field
   if (slow == 1)
      pcolor(field);
      titleMsg = sprintf('Field after %d steps.',Steps+1);
		title(titleMsg)
      pause(0.5)
   end
   %========Attempt to kick ball
   attemptKick=1;
   while(attemptKick & kick)
      coordX=ballPos(1)+kickx*attemptKick;
      coordY=ballPos(2)+kicky*attemptKick;     
      tempPos = field(coordY,coordX);
      if (tempPos == red | tempPos == blue)
         attemptKick = attemptKick + 1;
      elseif(tempPos == boundary)
         attemptKick = 0; %Miss (instead of throw-in or goal-kick)
      else %kick is valid so move ball and update field
         field(coordY,coordX) = ball;
         field(ballPos(2),ballPos(1)) = grass;
         ballPos(1)=coordX;
         ballPos(2)=coordY;
         attemptKick = 0;
      end
   end
           
   
   %========Score? & count steps
   Steps = Steps + 1;
   if (ballPos(1) == 1) %score blue team
      win = -1;
      playGame = 0;
   end
   if (ballPos(1) == 41) %score red team
      win = 1;
      playGame = 0;      
   end
      
   %========Display Field
   if (slow == 1)
      pcolor(field);
      titleMsg = sprintf('Kicking Team: %d',kick);
		title(titleMsg)
      pause(0.5)
   end
      
end %of main game loop

%---------------------END----------------------%

pcolor(field);
if (win == -1)
   title('Blue team scores!!!')
elseif(win == 1)
   title('Red team scores!!!!')
else
   title('No Winner in alloted time')
end
