function [dy,dx,mark,carry] = solver(main,food,ants,scent)
DEFAULT = 5;
toward_x = [ -1 round(rand(1) * -1) 0 round(rand(1) * 1) 1 ; ...
-1 -1 0 1 1 ; ...
-1 -1 0 1 1 ; ...
-1 -1 0 1 1 ; ...
-1 round(rand(1) * -1) 0 round(rand(1) * 1) 1];
toward_y = [ -1 -1 -1 -1 -1 ; ...
round(rand(1) * -1) -1 -1 -1 round(rand(1) * -1) ; ...
0 0 0 0 0 ; ...
round(rand(1) * 1) 1 1 1 round(rand(1) * 1) ; ...
1 1 1 1 1];
away_x = [ 1 round(rand(1) * 1) 0 round(rand(1) * -1) -1 ; ...
1 1 0 -1 -1 ; ...
1 1 0 -1 -1 ; ...
1 1 0 -1 -1 ; ...
1 round(rand(1) * 1) 0 round(rand(1) * -1) -1];
away_y = [ 1 1 1 1 1 ; ...
round(rand(1) * 1) 1 1 1 round(rand(1) * 1) ; ...
0 0 0 0 0 ; ...
round(rand(1) * -1) -1 -1 -1 round(rand(1) * -1) ; ...
-1 -1 -1 -1 -1];
toward_x(find(main(:) == -1)) = away_x(find(main(:) == -1));
toward_y(find(main(:) == -1)) = away_y(find(main(:) == -1));
inner_ring = [7 12 17 8 18 9 14 19];
outer_ring = [1 6 11 16 21 2 22 3 23 4 24 5 10 15 20 25];
dx = round(rand(1)*2) - 1;
dy = round(rand(1)*2) - 1;
mark = DEFAULT;
carry = logical(0);
% don't go for food that is home
food(find(main == 1)) = 0;
% Ignore self %(and any like me)
ants(13) = ants(13) - 1;
% Don't chase food that is being carried (except by me)
food = food - ants;
% Always carry food unless at home %unless there's a party going on
if( food(13) >= 1 & main(13) ~= 1 ) %& sum(find(ants(2:4,2:4))) < 2)
carry = logical(1);
end
% If there is scent then someone has seen home so endorse
if( sum(scent(:)) ~= 0)
smelliness = sum(scent(:));
mark = round(smelliness / 25);
if(mark > 100)
mark = 100;
end
end
% Full marks if I can see home
if( ~isempty(find(main == 1)) )
mark = 100;
end
x = 0;
y = 0;
if carry
mark = 0;
index = find(scent(:) > 0);
if(~isempty(index))
index = find(scent(:) == max(scent(:)));
index_ptr = ceil(rand(1) * length(index));
x = toward_x(index(index_ptr));
y = toward_y(index(index_ptr));
end
index = find(main(:) == 1);
if(~isempty(index))
x = toward_x(index(1));
y = toward_y(index(1));
end
else
index = find(scent(:) > 0);
if(~isempty(index))
index = find(scent(:) == min(scent(:)));
index_ptr = ceil(rand(1) * length(index));
x = toward_x(index(index_ptr));
y = toward_y(index(index_ptr));
end
index = find(food(:) >= 1);
if(~isempty(index))
index_ptr = ceil(rand(1) * length(index));
x = toward_x(index(index_ptr));
y = toward_y(index(index_ptr));
end
end
if( (x~=0 | y~=0) & main(3+y,3+x) ~= -1 & (ants(3+y,3+x) <= ants(13)) )
dx = x;
dy = y;
end
|