| Code: | function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
myScentMap,opScentMap,myDeathMap,opDeathMap)
%TrailAnts v0000alpha test
%krejcip@gmail.com
mark = 0;
innerScentMap = myScentMap(2:4, 2:4);
innerMainMap = mainMap(2:4,2:4);
foodMask = mainMap > 0;
foodMask = -foodMask;
foodMask = foodMask +1;
foodMap = foodMap.*foodMask;
dRow = round(rand*2) - 1;
dCol = round(rand*2) - 1;
mark = ceil( (foodMap(3,3) > 0) *20 /myAntMap(3,3));
action = -1; %-1 attack, 1 carry
if opAntMap(3,3) > 0
% if opponen ant on center, kill
%action = -1;
dRow = 0;
dCol = 0;
elseif rand < (foodMap(3,3) / myAntMap(3,3))
% if sees food on center,
action = 1;
[dRow, dCol] = goHome(mainMap,myScentMap);
elseif any(any(foodMap([1:12, 14:25])))
[dRow, dCol] = goGetFood(mainMap, foodMap);
% if food anywhere else, get food
mark = mark + 5;
else
%follow trail or random if none
[dRow, dCol] = goFollow(mainMap, innerScentMap);
%dRow = round(rand*2) - 1;
%dCol = round(rand*2) - 1;
end
%impossible move check
if isnan(mainMap( 3 + dRow , 3 + dCol)) || (rand < 0.15)
%lets do random
dRow = round(rand*2) - 1;
dCol = round(rand*2) - 1;
end
if mark > 100
mark = 100;
end
end
function [dRow, dCol] = goHome(mainMap,ScentMap)
ScentMap(ScentMap ==0) = inf;
if any(any(mainMap == 1));
dX=repmat([ -1 -1 0 1 1 ],5,1);
dY=flipud(rot90(dX));
homePos = find(mainMap == 1);
homePos = homePos(1);
dRow = dY(homePos);
dCol = dX(homePos);
%zdi
else
dX=repmat([ -1 -1 0 1 1],5,1);
dY=flipud(rot90(dX));
%follow scent
maxPos = find(min(min(ScentMap)) == ScentMap);
maxPos = maxPos(ceil(rand*length(maxPos)));
dRow = dY(maxPos);
dCol = dX(maxPos);
end
if dRow ==0 & dCol ==0
dRow = round(rand*2) - 1;
dCol = round(rand*2) - 1;
end
end
function [dRow, dCol] = goFollow(mainMap,ScentMap)
dX=repmat([ -1 0 1 ],3,1);
dY=flipud(rot90(dX));
%follow scent
maxPos = find(max(max(ScentMap)) == ScentMap);
maxPos = maxPos(ceil(rand*length(maxPos)));
dRow = dY(maxPos);
dCol = dX(maxPos);
if dRow ==0 & dCol ==0
dRow = round(rand*2) - 1;
dCol = round(rand*2) - 1;
end
end
function [dRow, dCol] = goGetFood(mainMap, foodMap)
dX=repmat([ -1 -1 0 1 1 ],5,1);
dY=flipud(rot90(dX));
spiralMatrix = [...
24 25 10 11 12;...
23 9 2 3 13;...
22 8 1 4 14;...
21 7 6 5 15;...
20 19 18 17 16];
maskedMap = foodMap;
maskedMap(maskedMap>0) = 1;
maskedMap(maskedMap == 0) = inf;
direction = find(spiralMatrix == min(min( spiralMatrix.*maskedMap)));
dRow = dY(direction);
dCol = dX(direction);
end
|