%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %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