| Code: | function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
myScentMap,opScentMap,myDeathMap,opDeathMap)
current_ant_x=3; current_ant_y=3;
visibility_x=5 ;visibility_y=5;
[trail_exist,dRow,dCol]=trail(myScentMap);
if trail_exist==false
%%% Finding out distances of each ant from the sugar cubes.
% Location of the sugar cubes are given by foodMap
[sug_x,sug_y,sug_num]=find(foodMap);
if length(sug_num)>0
[myant_x,myant_y,myant_val]=find(myAntMap);
[opant_x,opant_y,opant_val]=find(opAntMap);
% as=sug_x+1i*sug_y
% as1=myant_x+1i*myant_y
my_dist=abs(bsxfun(@minus,(sug_x+1i*sug_y).',myant_x+1i*myant_y)); % sugar cubes along x, ants along y
current_ant_dist=abs(bsxfun(@minus,(sug_x+1i*sug_y).',current_ant_x+1i*current_ant_y)); % sugar cubes along x, ants along y
op_dist=abs(bsxfun(@minus,(sug_x+1i*sug_y).',opant_x+1i*opant_y));
% my_dist1=[ones(length(myant_x),1),my_dist]; % +1 for myants
% op_dist1=[-ones(length(opant_x),1),op_dist]; % -1 for opponent ants
my_dist1=my_dist;
op_dist1=op_dist;
ant_dist=[my_dist1;op_dist1];
ant_dist=sort(ant_dist,1,'descend');
ant_required=bsxfun(@minus,ant_dist,current_ant_dist);
ant_required(ant_required<=0)=0;
ant_required(~ant_required)=1;
ant_required1=sum(ant_required,1);
% sug_x
% sug_num
ant_required2=ant_required1-sug_num';
sug_x(ant_required2<=0)=[];
sug_y(ant_required2<=0)=[];
sug_num(ant_required2<=0)=[];%% sug_x,sug_y gives the sugar cubes which are worth accessing by myants
% % Move towards the nearest of such sugar cubes
if length(sug_x)>0
[current_ant_dist,index_min]=min(abs((sug_x+1i*sug_y).'-repmat((current_ant_x+1i*current_ant_y),1,length(sug_y)))); % sugar cubes along x, ants along y
if length(index_min)>0&((sug_x(index_min)-current_ant_x)^2+(sug_y(index_min)-current_ant_y)^2)~=0
dRow=(sug_x(index_min)-current_ant_x)/abs((sug_x(index_min)-current_ant_x));
dCol=(sug_y(index_min)-current_ant_y)/abs((sug_y(index_min)-current_ant_y));
mark=15;
action=1;
end
end
end
end
% if dRow~=0&dRow~=1&dRow~=-1; dRow = round(rand*2) - 1;end
% if dCol~=0&dCol~=1&dCol~=-1; dCol = round(rand*2) - 1;end
action = 1; %-1 attack, 1 carry
mark = 15; % scent
% dRow
% dCol
end
function [trail_exist1,suggested_move_x,suggested_move_y] = trail(scentmap)
x=3;y=3;
surrounding_cells=[x-1,y-1;x,y-1;x+1,y-1;x-1,y;x,y;x+1,y;x-1,y+1;x,y+1;x+1,y+1];
scentmap_temp=abs(scentmap(x,y)-scentmap(surrounding_cells));
[ind_x,ind_y]=find(scentmap_temp==1);
[ind_x_temp,ind_y_temp]=find(scentmap_temp==2);
ind_xy=[ind_x,ind_y;ind_x_temp,ind_y_temp];
ind_x=ind_xy(:,1);
ind_y=ind_xy(:,2);
if length(ind_x)>0
x=ind_x(1);y=ind_y(1);
surrounding_cells1=[x-1,y-1;x,y-1;x+1,y-1;x-1,y;x,y;x+1,y;x-1,y+1;x,y+1;x+1,y+1];
% removing zeros from index values
surrounding_cells2=surrounding_cells1;
surrounding_cells2(surrounding_cells2==0)=NaN;
surrounding_cells2(:,1)=surrounding_cells2(:,2)+surrounding_cells2(:,1)-surrounding_cells2(:,2);
surrounding_cells2(:,2)=surrounding_cells2(:,1)+surrounding_cells2(:,2)-surrounding_cells2(:,1);
surrounding_cells1=~isnan(surrounding_cells2).*surrounding_cells1;
[d,f]=find(surrounding_cells1==0);
surrounding_cells1(d,:)=[];
scentmap_temp=abs(scentmap(x,y)-scentmap(surrounding_cells1));
[ind_x1,ind_y1]=find(scentmap_temp==1);
[ind_x1_temp,ind_y1_temp]=find(scentmap_temp==2);
ind_xy_temp=[ind_x1,ind_y1;ind_x1_temp,ind_y1_temp];
ind_x1=ind_xy_temp(:,1);
ind_y1=ind_xy_temp(:,2);
if length(ind_x1)>0
suggested_move_x=-(ind_x1(1)-3)/abs((ind_x1(1)-3)); % be careful here. the 3 is used as reference for the middle cell
suggested_move_y=-(ind_y1(1)-3)/abs((ind_y1(1)-3));
trail_exist1=true;
else
suggested_move_x=0;
suggested_move_y=0;
trail_exist1=false;
end
else
suggested_move_x=0;
suggested_move_y=0;
trail_exist1=false;
end
end
|