| Code: | function [dRow,dCol,action,mark] = solver(mainMap, foodMap, myAntMap, opAntMap, myScentMap, opScentMap, myDeathMap, opDeathMap)
rand(1,2);
mainMap = mainMap(:);
foodMap = foodMap(:);
myAntMap = myAntMap(:);
opAntMap = opAntMap(:);
myScentMap = myScentMap(:);
% opScentMap = []; %#ok<NASGU>
% myDeathMap = []; %#ok<NASGU>
% opDeathMap = []; %#ok<NASGU>
r = rand(9,1);
setSearchDirected = 0.2;
setSearchExpansionistic = 0.5;
action = -1;
home = mainMap == 1;
myScentMap(home) = 1e7;
foodMap(home) = 0;
even = [true false true false true false true false true false true false true false true false true false true false true false true false true];
odd = [false true false true false true false true false true false true false true false true false true false true false true false true false];
% smalleye = [false; false; false; false; false; false; true; true; true; false; false; true; false; true; false; false; true; true; true; false; false; false; false; false; false];
smallsquare = [false; false; false; false; false; false; true; true; true; false; false; true; true; true; false; false; true; true; true; false; false; false; false; false; false];
bigeye = [true; true; true; true; true; true; true; true; true; true; true; true; false; true; true; true; true; true; true; true; true; true; true; true; true];
% smalleyetodir = [ 7 6 5 0 4 1 2 3 ];
bigeyetodir = [ 7 7 6 5 5 7 7 6 5 5 0 0 4 4 1 1 2 3 3 1 1 2 3 3];
% --- SNIP ---
% shameless copy from cyclist's winning submission for the last ants contest
BAD = isnan(mainMap);
if BAD(7)||BAD(9)||BAD(17)||BAD(19)
if BAD(7) BAD(1)=1; BAD(2)=BAD(2)||BAD(8); BAD(3)=BAD(3)||(BAD(8)&&BAD(9)); BAD(6)=BAD(6)||BAD(12); BAD(11)=BAD(11)||(BAD(12)&&BAD(17)); end
if BAD(9) BAD(4)=BAD(4)||BAD(8); BAD(5)=1; BAD(10)=BAD(10)||BAD(14); end
if BAD(17) BAD(16)=BAD(16)||BAD(12); BAD(21)=1; BAD(22)=BAD(22)||BAD(18); end
if BAD(19) BAD(15)=BAD(15)||(BAD(9)&&BAD(14)); BAD(20)=BAD(20)||(BAD(14)); BAD(23)=BAD(23)||(BAD(17)&&BAD(18)); BAD(24)=BAD(24)||(BAD(18)); BAD(25)=1; end
% modify current board based on reachable regions
mainMap(BAD) = nan;
foodMap(BAD) = nan;
myScentMap(BAD) = nan;
%opScentMap(BAD) = nan;
end
% --- SNIP ---
mes = max(myScentMap(even));
mos = max(myScentMap(odd));
scentbase = myScentMap;
scentfood = myScentMap;
if mes > mos
scentbase(odd) = 0;
scentfood(even) = 0;
oneven = true;
ismellfood = mos > 0;
maxfoodscent = mos;
maxhomescent = mes;
iamlost = false;
elseif mos > mes
scentbase(even) = 0;
scentfood(odd) = 0;
oneven = false;
ismellfood = mes > 0;
maxfoodscent = mes;
maxhomescent = mos;
iamlost = false;
else
oneven = true;
ismellfood = false;
maxfoodscent = 0;
maxhomescent = 0;
iamlost = true;
end
mark = 0;
if oneven
if any(mainMap(:) == 1, 1) mark = 100;
else mark = floor((mes-myScentMap(13)-1) ./ myAntMap(13));
end
else % ~oneven
if any((foodMap-myAntMap)>0, 1)
if maxhomescent > 50 foodscentcap = 50;
else foodscentcap = maxhomescent-1;
end
mark = floor((foodscentcap - myScentMap(13)-1) ./ myAntMap(13));
elseif ismellfood % && ~any(home)
mark = floor((maxfoodscent - myScentMap(13)-1) ./ myAntMap(13));
end
end
% anyAntAround = sum(opAntMap(smallsquare), 1);
if opAntMap(13)
action = -1;
moveDirection = -1;
% elseif any(opAntMap(:), 1) % hunt ants
% action = -1;
% if opAntMap(13)
% moveDirection = -1;
% elseif any(opAntMap(smalleye), 1)
% [dummy, smalleyedir] = max(opAntMap(smalleye)); %#ok<ASGLU>
% moveDirection = smalleyetodir(smalleyedir);
% else % opAnt in bigeye
% [dummy, bigeyedir] = max(opAntMap(bigeye)); %#ok<ASGLU>
% moveDirection = bigeyetodir(bigeyedir);
% end
elseif foodMap(13) && r(5) < ((foodMap(13)+3) / myAntMap(13)) % food here, and we are a carrier -> MOVE HOME
action = 1;
[maxscent, bigeyedir] = max(scentbase(bigeye)); %#ok<ASGLU>
if maxscent > 0
moveDirection = bigeyetodir(bigeyedir);
else
% moveDirection = ceil(r(7)*8)-1;
moveDirection = randMove();
end
elseif any((foodMap(bigeye) - myAntMap(bigeye)) > 0) % if no opAnts are around, move to food
[dummy, bigeyedir] = max(foodMap(bigeye)); %#ok<ASGLU>
moveDirection = bigeyetodir(bigeyedir);
% elseif any(opScentMap(bigeye) > 0, 1) && r(2) < 0.5 % opponent's scent seen & rand
% [dummy, bigeyedir] = max(opScentMap(bigeye)); %#ok<ASGLU>
% moveDirection = bigeyetodir(bigeyedir);
% doTumble = true;
else
% SEARCH
if ismellfood && ~any(home)
[maxscent, bigeyedir] = max(scentfood(bigeye)); %#ok<ASGLU>
if maxscent > 0
moveDirection = bigeyetodir(bigeyedir);
else
% moveDirection = ceil(r(7)*8)-1;
moveDirection = randMove();
end
else
if r(1) < setSearchDirected
maxscent = max(scentbase(bigeye));
if maxscent > 0
direction = find(scentbase(bigeye) >= maxscent);
ndirections = numel(direction);
if ndirections > 1
direction = direction(ceil(r(6)*ndirections));
end
moveDirection = mod(bigeyetodir(direction) + 4, 8);
else
moveDirection = randMove();
end
elseif r(1) < setSearchExpansionistic
maxants = max(myAntMap(bigeye));
if maxants > 0
direction = find(myAntMap(bigeye) >= maxants);
ndirections = numel(direction);
if ndirections > 1
direction = direction(ceil(r(8)*ndirections));
end
moveDirection = mod(bigeyetodir(direction) + 4, 8);
else
% moveDirection = ceil(r(7)*8)-1;
moveDirection = randMove();
end
else
% moveDirection = ceil(r(7)*8)-1;
moveDirection = randMove();
end
end
end
% FIXMOVE
if moveDirection >= 0
translateDirectionToMap = [12 17 18 19 14 9 8 7];
target = translateDirectionToMap(moveDirection+1);
if isnan(mainMap(target)) || opAntMap(target)
moveDirection = randMove();
end
end
dRows = [0 -1 -1 0 1 1 1 0 -1];
dCols = [0 0 1 1 1 0 -1 -1 -1];
dRow = dRows(moveDirection+2);
dCol = dCols(moveDirection+2);
function moveDirection = randMove()
if xor(oneven, ismellfood)
tar1 = [17 19 9 7];
tar2 = [12 18 14 8];
mv1 = [1 3 5 7];
mv2 = [0 2 4 6];
else
tar1 = [12 18 14 8];
tar2 = [17 19 9 7];
mv1 = [0 2 4 6];
mv2 = [1 3 5 7];
end
ok1 = ~(isnan(mainMap(tar1)) | opAntMap(tar1));
ok2 = ~(isnan(mainMap(tar2)) | opAntMap(tar2));
if iamlost
targets = [mv1(ok1) mv2(ok2)];
else
% triple chance to move to even field if we don't see sugar, or to
% move to odd field if we see sugar
targets = [mv1(ok1) mv1(ok1) mv1(ok1) mv2(ok2)];
end
if isempty(targets)
moveDirection = -1;
else
moveDirection = targets(ceil(r(9)*numel(targets)));
end
end
end
|