Follower is passing closed doors

i got a problem. i want a guy following me but when he is doing so, i want that the guy is 1 step behind. But now there is the problem! i want him one step behind so i can cage him in a room. but when im in the next room and say “close door”, he is still able to walk through the closed door! how can get it fixed so he is not able to cross the door

the idea is im in a room and he is in the same room. when im doing nothing for 3 times hes gonna kill me and if i do something without leaving the room after this, hes gonna follow me (now jack is following)

this is what i got:

if Jack is not in the location:
	if Jack is nfollowing and kontacount of Jack is greater than 2:
		say "i think i got away";
		now kontacount of Jack is 0;
	if Jack is nfollowing and kontacount of Jack is greater than 0:
		now kontacount of Jack is 0;
	if Jack is following:
		increase waiting of jack by 1;
		if the location of Jack is not the location and waiting of jack is 2:
			let way be the best route from the location of Jack to the location of the player, using doors;
			try Jack going way;
			now kontacount of jack is 2;
			say "Jack folgt dir in den Raum.";
			now warten of Jack is 0;

If you lock the door, he won’t be able to open it again.