function action = EAGER_KB_Amelia_agent(percept)
% EAGER_KB_agent - KB 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_KB_agent([0,0,0,0,0]);
% Author:
%     T. Henderson
%     UU
%     Fall 2017/Spring 2022
%

persistent board PKB MKB found turn bb ps
persistent x y dir action_taken escape kill go explored ls

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




if isempty(board)
    turn = 0;
    PKB = gen_PitKB;
    PKB.m_kb =  BR_PSAT2PSAT3(PKB.m_kb);
    MKB = gen_MonsterKB;
    MKB.m_kb =  BR_PSAT2PSAT3(MKB.m_kb);
  
    ls = [2, 3, 5, 6, 7, 9, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28];
    for i = 1:16
        MKB.tellClause(MKB.m_bw_map(ls(i)), 1/15);
        PKB.tellClause(PKB.m_bw_map(ls(i)), 0.2);
    end
    explored = -ones(4,4);
    explored(4,1) = 1;
    board = -ones(4,4);
    board(4,1) = 0;
    x = 1;
    y = 1;
    dir = 0;
    action = 0;
    action_taken = 0;
    escape = [];
    kill = [];
    found = 0;
    go = [];
    PKB.tellClause("P11", 0);
    MKB.tellClause("M11", 0);
   if percept(2)==1&isempty(escape)
    stp = "B" + int2str(x) + int2str(y);
    PKB.tellClause(stp, 1);
    
    if(x+1 < 5)
        stp1 = "P" + int2str(x+1) + int2str(y);
        PKB.tellClause2(stp1);
        stp2 = "-P" + int2str(x+1) + int2str(y);
        stp3 = [stp, stp2];
        PKB.tellClause(stp3, 1);
    end
    if(x-1 > 0)
        stp1 = "P" + int2str(x-1) + int2str(y);
        PKB.tellClause2(stp1);
        stp2 = "-P" + int2str(x-1) + int2str(y);
        stp3 = [stp, stp2];
        PKB.tellClause(stp3, 1);
    end
    if(y+1 < 5)
        stp1 = "P" + int2str(x) + int2str(y+1);
        PKB.tellClause2(stp1);
        stp2 = "-P" + int2str(x) + int2str(y+1);
        stp3 = [stp, stp2];
        PKB.tellClause(stp3, 1);
    end
    if(y-1 > 0)
       stp1 = "P" + int2str(x) + int2str(y-1);
        PKB.tellClause2(stp1);
        stp2 = "-P" + int2str(x) + int2str(y-1);
        stp3 = [stp, stp2];
        PKB.tellClause(stp3, 1);
    end
   end
    if percept(2)==0&isempty(escape)
    stp = "B" + int2str(x) + int2str(y);
    PKB.tellClause(stp, 0);
    if(x+1 < 5)
        stp = "P" + int2str(x+1) + int2str(y);
        PKB.tellClause(stp, 0);
    end
    if(x-1 > 0)
        stp = "P" + int2str(x-1) + int2str(y);
        PKB.tellClause(stp, 0);
    end
    if(y+1 < 5)
        stp = "P" + int2str(x) + int2str(y+1);
        PKB.tellClause(stp, 0);
    end
    if(y-1 > 0)
        stp = "P" + int2str(x) + int2str(y-1);
        PKB.tellClause(stp, 0);
    end
    
    end
    
    if percept(1)==0&isempty(escape)
    stp = "S" + int2str(x) + int2str(y);
    MKB.tellClause(stp, 0);
    if(x+1 < 5)
        stp = "W" + int2str(x+1) + int2str(y);
        MKB.tellClause(stp, 0);
    end
    if(x-1 > 0)
         stp = "W" + int2str(x-1) + int2str(y);
        MKB.tellClause(stp, 0);
        
    end
    if(y+1 < 5)
         stp = "W" + int2str(x) + int2str(y+1);
        MKB.tellClause(stp, 0);
    end
    if(y-1 > 0)
        stp = "W" + int2str(x) + int2str(y-1);
        MKB.tellClause(stp, 0);
    end
        
end
    
    ps = zeros(1, 32);
    bb = zeros(1, 32);
    if(percept(1) == 1)
        action_taken = 5;
        action = 5;
        found = 1;
        return;
    end
    if(rand(1) < 0.5)
        action = 1;
    else
        action = 3;
        go = [1];
    end
    action_taken = action;
    return;
    
end

[x,y,dir] = CS4300_act(x,y,dir,action_taken,percept);
 row = 4 - y + 1;
 col = x;
 
 if ~isempty(escape)   % pre-planned escape sequence of actions
    action = escape(1);
    escape = escape(2:end);
    action_taken = action;
    return
end

if percept(3)==1&isempty(escape)  % Glitter percept
    board(row,col) = 0;
    board(1,1) = 0;
    [sol,~] = CS4300_Wumpus_A_starADJ2(explored, 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




 
 
turn = turn + 1;



if(found == 1 && turn == 1)
    explored(row,col) = 1;
    if(percept(5) ~= 1)
        board(3,1) = 3;
        bz = MKB.m_string_map("W12");
        bb = zeros(1, 32);
        bb(bz) = 1;
    else
        bb = zeros(1,32);
    end
    action = 1;
    action_taken = 1;
    return;
end





 

if explored(row,col) == -1
  explored(row,col) = 1;
  % Update KB
if percept(2)==1&isempty(escape)
    
    stp = "B" + int2str(x) + int2str(y);
    PKB.tellClause(stp, 1);
    
    if(x+1 < 5)
        stp1 = "P" + int2str(x+1) + int2str(y);
        PKB.tellClause2(stp1);
       
        
    end
    if(x-1 > 0)
        stp1 = "P" + int2str(x-1) + int2str(y);
        PKB.tellClause2(stp1);
      
      
    end
    if(y+1 < 5)
        stp1 = "P" + int2str(x) + int2str(y+1);
        PKB.tellClause2(stp1);
       
        
    end
    if(y-1 > 0)
        stp1 = "P" + int2str(x) + int2str(y-1);
        PKB.tellClause2(stp1);
        
    end
end
if percept(2)==0&isempty(escape)
    stp = "B" + int2str(x) + int2str(y);
    PKB.tellClause(stp, 0);
     if(x+1 < 5)
        stp = "P" + int2str(x+1) + int2str(y);
        PKB.tellClause(stp, 0);
    end
    if(x-1 > 0)
        stp = "P" + int2str(x-1) + int2str(y);
        PKB.tellClause(stp, 0);
    end
    if(y+1 < 5)
        stp = "P" + int2str(x) + int2str(y+1);
        PKB.tellClause(stp, 0);
    end
    if(y-1 > 0)
        stp = "P" + int2str(x) + int2str(y-1);
        PKB.tellClause(stp, 0);
    end
    
end

if percept(1)==1&isempty(escape)&&found==0
    stp = "S" + int2str(x) + int2str(y);
    MKB.tellClause(stp, 1);
     if(x+1 < 5)
        stp = "W" + int2str(x+1) + int2str(y);
        MKB.tellClause2(stp);
    end
    if(x-1 > 0)
         stp = "W" + int2str(x-1) + int2str(y);
        MKB.tellClause2(stp);
        
    end
    if(y+1 < 5)
         stp = "W" + int2str(x) + int2str(y+1);
        MKB.tellClause2(stp);
    end
    if(y-1 > 0)
        stp = "W" + int2str(x) + int2str(y-1);
        MKB.tellClause2(stp);
    end
end
if percept(1)==0&isempty(escape)&&found==0
    stp = "S" + int2str(x) + int2str(y);
    MKB.tellClause(stp, 0);
    if(x+1 < 5)
        stp = "W" + int2str(x+1) + int2str(y);
        MKB.tellClause(stp, 0);
    end
    if(x-1 > 0)
         stp = "W" + int2str(x-1) + int2str(y);
        MKB.tellClause(stp, 0);
        
    end
    if(y+1 < 5)
         stp = "W" + int2str(x) + int2str(y+1);
        MKB.tellClause(stp, 0);
    end
    if(y-1 > 0)
        stp = "W" + int2str(x) + int2str(y-1);
        MKB.tellClause(stp, 0);
    end
        
end

stp = "P" + int2str(x) + int2str(y);
PKB.tellClause(stp, 0);

stp = "W" + int2str(x) + int2str(y);
MKB.tellClause(stp, 0);


ps = PKB.askClause("B11");


if(found == 0)
    bb = MKB.askClause("S11");
end

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(found == 0 && turn > 3)
    bbmax = max(bb(ls));
    lsi = find(bb(ls) == bbmax);
     MKB.m_bw_map(ls(lsi));
    if bbmax > 0.6
       
        found = 1;
        st = MKB.m_bw_map(ls(lsi));
        s2 = convertStringsToChars(st);
        xw = str2num(s2(2));
        yw = str2num(s2(3));
         board(row,col) = 0;
         bb = zeros(1,32);
     
    [sol,~] = CS4300_Wumpus_A_starADJ2(explored, board,[x,y,dir],[xw,yw,0],...
        'CS4300_A_star_Man');
       
   
    if(isempty(sol))
       [sol,~] = CS4300_Wumpus_A_starADJ(PKB, zeros(1,32), zeros(1,32), board,[x,y,dir],[xw,yw,0],...
        'CS4300_A_star_Man');
    end
    
    sol = sol(2:end,:);
    kill = sol(:,end);
    k = length(kill);
    for i = k:-1:1
        if(kill(i) == 1)
            break;
        end
    end
    
    if(isempty(kill))
        action = 5;
        action_taken = action;
        kill = [1];
        return;
    end
    kill(i) = 5;
    kill(i+1) = 1;
    action = kill(1);
    kill = kill(2:end);
    action_taken = action;
    
    return
    end
end


[xn, yn, force] = takeActionKB(explored, row, col, ps, bb, PKB, board, found);

dirn = 1;

rown = 4 - yn + 1;
coln = xn;
explored(rown, coln) = 1;
board(rown,coln) = 0;

[sol,~] = CS4300_Wumpus_A_starADJ2(explored, board,[x,y,dir],[xn,yn,dirn],...
    'CS4300_A_star_Man');

explored(rown, coln) = -1;


sol = sol(2:end,:);
[a, b] = size(sol);
if(a == 0 || b == 0)
    action = 0;
    action_taken = 0;
    return;
end


go = sol(:,end);
action = go(1);
go = go(2:end);
action_taken = action;
return
