function action = EAGER_MC_agent(percept)
% EAGER_tch_agent - Monte Carlo board analysis agent
% On input:
%     percept (1x5 Boolean vector): percept values
%      (1): Stench
%      (2): Breeze
%      (3): Glitters
%      (4): Bumped
%      (5): Screamed
% On output:
%     action (int): action selected by agent
%       FORWARD = 1;
%       ROTATE_RIGHT = 2;
%       ROTATE_LEFT = 3;
%       GRAB = 4;
%       SHOOT = 5;
%       CLIMB = 6;
% Call:
%     a = EAGER_MC_agent([0,0,0,0,0]);
% Author:
%     T. Henderson
%     UU
%     Fall 2017/Spring 2022
%

persistent safe board pit_probs wumpus_probs breezes stenches
persistent visited x y dir gold action_taken escape kill go

FORWARD = 1;
ROTATE_RIGHT = 2;
ROTATE_LEFT = 3;
GRAB = 4;
SHOOT = 5;
CLIMB = 6;

PIT_PROB = 0.2;
NUM_TRIALS = 10000;

if isempty(safe)
    safe = -ones(4,4);
    safe(4,1) = 1;
    board = -ones(4,4);
    board(4,1) = 0;
    visited = zeros(4,4);
    visited(4,1) = 1;
    pit_probs = zeros(4,4);
    wumpus_probs = zeros(4,4);
    breezes = -ones(4,4);
    breezes(4,1) = percept(2);
    stenches = -ones(4,4);
    stenches(4,1) = percept(1);
    x = 1;
    y = 1;
    dir = 0;
    action_taken = 0;
    escape = [];
    kill = [];
    go = [];
end

[x,y,dir] = CS4300_act(x,y,dir,action_taken,percept);
[x,y,dir]';
safe(4-y+1,x) = 1;
visited(4-y+1,x) = 1;
board(4-y+1,x) = 0;

% Update info
ra = 4 - y + 1;
ca = x;
stenches(ra,ca) = percept(1);
breezes(ra,ca) = percept(2);
[pit_probs1,wumpus_probs1] = EAGER_MC_atom_probs(breezes,stenches,...
    NUM_TRIALS,PIT_PROB);
if ~isnan(pit_probs1(1,1))
    pit_probs = pit_probs1;
    wumpus_probs = wumpus_probs1;
end
for r = 1:4
    for c = 1:4
        if pit_probs(r,c)==0&wumpus_probs(r,c)==0
            safe(r,c) = 1;
            board(r,c) = 0;
        elseif pit_probs(r,c)==1|wumpus_probs(r,c)==1
            safe(r,c) = 0;
            board(r,c) = 1;
        end
    end
end

for r = 1:4
    for c = 1:4
        if safe(r,c)==1
            rmin = max(1,r-1);
            rmax = min(4,r+1);
            cmin = max(1,c-1);
            cmax = min(4,c+1);
            found = 0;
            for rr = rmin:rmax
                for cc = cmin:cmax
                    if norm([r,c]-[rr,cc])<1.1&visited(rr,cc)==1
                        found = 1;
                    end
                end
            end
            if found==0
                safe(r,c) = 0;
            end
        end
    end
end

if percept(3)==1&isempty(escape)  % Glitter percept
    row = 4 - y + 1;
    col = x;
    safe(row,col) = 1;
    board(row,col) = 0;
    [sol,nodes] = CS4300_Wumpus_A_star(board,[x,y,dir],[1,1,0],...
        'CS4300_A_star_Man');
    sol = [x,y,dir,GRAB;sol(2:end,:);1,1,1,CLIMB];
    escape = sol(:,end);
    action = escape(1);
    escape = escape(2:end);
    action_taken = action;
    return
end

if ~isempty(escape)   % pre-planned escape sequence of actions
    action = escape(1);
    escape = escape(2:end);
    action_taken = action;
    return
end

if ~isempty(kill)   % pre-planned kill Wumpus sequence
    action = kill(1);
    kill = kill(2:end);
    action_taken = action;
    return
end

if ~isempty(go)   % pre-planned movements to unvisited safe cell
    action = go(1);
    go = go(2:end);
    action_taken = action;
    return
end

% If Wumpus found, plan kill sequence
[rw,cw] = find(wumpus_probs==1);
if ~isempty(rw)
    xw = cw;
    yw = 4 - rw + 1;
    [rows,cols] = find(safe==1);
    row = find(rows==rw);
    col = find(cols==cw);
    if ~isempty(row)
        yg = 4 - row(1) + 1;
        xg = xw;
    elseif ~isempty(col)
        yg = yw;
        xg = col(1);
    end
    dirg = round(180*posori(atan2(yw-yg,xw-xg))/pi/90);
    [sol,nodes] = CS4300_Wumpus_A_star(board,[x,y,dir],[xg,yg,dirg],...
        'CS4300_A_star_Man');
    sol = [sol(2:end,:);xg,yg,3,2;xg,yg,dirg,SHOOT];
    kill = sol(:,end);
    action = kill(1);
    kill = kill(2:end);
    action_taken = action;
    wumpus_probs(rw,cw) = 0;
    stenches = zeros(4,4);
    return
end

% Go to nearest safe unvisited cell
rn = 0;
cn = 0;
min_dist = Inf;
for r = 1:4
    for c = 1:4
        if safe(r,c)==1&visited(r,c)==0
            dist = norm([ra,ca]-[r,c]);
            if dist<min_dist
                rn = r;
                cn = c;
                min_dist = dist;
            end
        end
    end
end
if rn>0  % found safe unvisited cell
    xn = cn;
    yn = 4 - rn + 1;
    dirn = dir;
    [sol,nodes] = CS4300_Wumpus_A_star(board,[x,y,dir],[xn,yn,dirn],...
        'CS4300_A_star_Man');
    sol = sol(2:end,:);
    go = sol(:,end);
    action = go(1);
    go = go(2:end);
    action_taken = action;
    return
end

% visit safest frontier cell
frontier = zeros(4,4);
f_probs = ones(4,4);
for r = 1:4
    for c = 1:4
        if safe(r,c)==1
            rmin = max(1,r-1);
            rmax = min(4,r+1);
            cmin = max(1,c-1);
            cmax = min(4,c+1);
            for rr = rmin:rmax
                for cc = cmin:cmax
                    dist = norm([r,c]-[rr,cc]);
                    if dist<1.1&visited(rr,cc)==0
                        frontier(rr,cc) = 1;
                        f_probs(rr,cc) = min(f_probs(rr,cc),...
                            max(pit_probs(rr,cc),...
                            wumpus_probs(rr,cc)));
                    end
                end
            end
        end
    end
end
% need to find lowest prob pit and Wumpus
[rows,cols] = find(f_probs==min(min(f_probs)));
rn = rows(1);
cn = cols(1);
dirn = dir;
xn = cn;
yn = 4 - rn + 1;
if x==xn&y==yn
    [rows,cols] = find(visited==0);
    if isempty(rows)
        [vrows,vcols] = find(visited==1);
        for k = 1:length(vrows)
            if vrows(k)~=4-y+1|vcols(k)~=x
                xn = vcols(k);
                y = 4 - vrows(k) + 1;
            end
        end
    else
        xn = cols(1);
        yn = 4 - rows(1) + 1;
    end
end
board(rn,cn) = 0;
[sol,nodes] = CS4300_Wumpus_A_star(board,[x,y,dir],[xn,yn,dirn],...
    'CS4300_A_star_Man');
sol = sol(2:end,:);
go = sol(:,end);
action = go(1);
go = go(2:end);
action_taken = action;
return
