function [action, pitsout, monstersout] = Amelia_agent(percept)
% CS4300_random_agent - random action 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 = CS4300_random_agent([0,0,0,0,0]);
% Author:
%     T. Henderson
%     UU
%     Fall 2017/Spring 2022
%

persistent below to_right above go shooting go_on pits monsters found board x y dir gold action_taken escape explored

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



if isempty(board)
    go_on = 0;
    to_right = 0;
    above = 0;
    jgbo = 0;
    found = 0;
    below = 0;
    gbo = 0;
    getBack = [2, 2, 1];
    pits = ones(4,4);
    monsters = ones(4,4);
    explored = zeros(4,4);
    explored(4,2) = 1;
    explored(3,1) = 1;
    pits(4,1) = 0;
    monsters(4,1) = 0;
    shooting = 0;
    board = -ones(4,4);
    board(4,1) = 0;
    x = 1;
    y = 1;
    dir = 0;
    action_taken = 1;
    action = 1;
    escape = [];
    if percept(1) == 1 && found == 0
    %some space around us is monster!!!
    
        monsters = adjustPitsAndMonsters(4,1, monsters, explored);
        action = 5;
        shooting = 1;
        action_taken = action;
        pitsout = pits;
        monstersout = monsters;
        return
       
        
    else
    monsters = adjustPitsAndMonsters2(4,1, monsters, explored);
    
    end
    
 
if percept(2) == 1
  %some space around us is pit!!!    
    pits = adjustPitsAndMonsters(4,1, pits, explored);
else
    pits = adjustPitsAndMonsters2(4,1,pits, explored);
    
end
    explored(4,1) = 2;
    pitsout = pits;
    monstersout = monsters;
    return;
    
end

 pitsout = pits;
 monstersout = monsters;
 
if(pits(4,2) > 20 && pits(3,1) > 20)
    pits(3,1) = 100;
    pits(4,2) = 0;
end

if(shooting == 1 && go_on ~= 1)
    found = 1;
    shooting = 0;
    if(percept(5) == 1)
        monsters = clearMonster(monsters);
    else
        monsters(3,1) = 100;
        monsters(4,2) = 0;
    end
    action = 1;
    action_taken = action;
    return;
end
        
[x,y,dir] = CS4300_act(x,y,dir,action_taken);
board(4-y+1,x) = 0;
row = 4 - y + 1;
col = x;



 pitsout = pits;
 monstersout = monsters;
 
 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  % Glitter percept
    row = 4 - y + 1;
    col = x;
    board(row,col) = 0;
    [sol,nodes] = CS4300_Wumpus_A_starADJ(pits, monsters, 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 percept(2) == 1  
    pits = adjustPitsAndMonsters(row,col, pits, explored);
else
    pits(row, col) = 0;
    pits = adjustPitsAndMonsters2(row,col, pits, explored);
    
end 

pits = updatePits(pits);

if percept(1) == 1 && found == 0
    %some space around us is monster!!!
    monsters = adjustPitsAndMonsters(row,col, monsters, explored);
end    
        
if found == 0 && percept(1) == 0
    monsters(row, col) = 0;
    monsters = adjustPitsAndMonsters2(row,col, monsters, explored);  
    
end

if found == 0
    [dx, dy] = findMonster(monsters);


if dx ~= -1 && dy ~= -1
    found = 1;
    
if(dy == 4)
    mp = dy;
    mpr = 4 - dx;
    above = 1;
end
if dy ~=4 || pits(row, col-1) >= 21 || dx == 4
    above = 0;
    mp = dy - 1;
    mpr = 4 - dx + 1;
    to_right = 1;
end
if dy == 1  
    above = 0;
    to_right = 0;
    below = 1;
    mp = dy + 1;
    mpr = 4 - dx +1;
end

explored(row,col) = 2;
board(row,col) = 0;
monsters(row,col) = 0;
pits(row,col) = 0;
board(mp, mpr) = 0;

[sol,nodes] = CS4300_Wumpus_A_starADJ(pits, monsters, board,[x,y,dir],[mp,mpr,0],...
        'CS4300_A_star_Man');

sol = [sol(2:end,:);1,1,1,2];

go = sol(:,end);
go_on = 1;

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

end
end



explored(row,col) = 2;
explored  = exploredUpdate(row, col, explored);

 pitsout = pits;
 monstersout = monsters;
if go_on == 1
    if shooting == 1
        if(length(go) == 1 || length(go) == 0)
      if to_right == 1 
          if dir ~= 0
              action = 2;
              action_taken = action;
              return;
          else
              action = 5;
              action_taken = action;
              shooting =  0;
              go_on = 0;
              return;
          end
      end
      if above == 1
          if dir ~=  1
              action = 2;
              action_taken = action;
              return;
          else
              action = 5;
              action_taken = action;
              shooting = 0;
              go_on = 0;
              return;
          end
      end
        if below == 1
          if dir ~=  2
              action = 2;
              action_taken = action;
              return;
          else
              action = 5;
              action_taken = action;
              shooting = 0;
              go_on = 0;
              return;
          end
       end
      
        end
       action = go(1);
      go = go(2:end);
      action_taken = action;
      return;
          
    end
    if(length(go) == 1 && go(1) == 2)
        go_on = 0;
        action = 5;
        action_taken = action;
        return;
    end
    if(length(go) == 0)
        go_on = 0;
        action = 0;
        action_taken = action;
    elseif go(1) == 0
        go_on = 0;
         action = go(1);
      go = go(2:end);
      action_taken = action;
      return;
    else
      action = go(1);
      go = go(2:end);
      action_taken = action;
      return;
    end

end





  

if percept(5) == 1 && percept(3)~=1
    action = 1;
    action_taken = action;
    monsters = clearMonster(monsters);
    return;
end





[dx, dy] = takeAction(row, col, x, y, explored, pits, monsters);
board(dx,dy) = 0;

drow = 4 - dy + 1;
dcol = dx;
pits(drow, dcol) = 0;



[sol,nodes] = CS4300_Wumpus_A_starADJ(pits, monsters, board,[x,y,dir],[dx,dy,0],...
        'CS4300_A_star_Man');
sol = [sol(2:end,:);1,1,1,2];


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

pitsout = pits;
monstersout = monsters;

end
