If you eat a ghost now, he will go running back to his home. When he reaches his home he'll go back to either SCATTER mode or CHASE mode. But you may notice an issue where he could get stuck inside the home, especially if he is in CHASE mode and Pacman is below the home. There's only 1 exit from the home and that's in the UP direction. You'll also notice that Pacman can enter into the ghost home which he shouldn't be allowed to do ever. The ghost also shouldn't be allowed to enter into the home unless he's in SPAWN mode. So we'll need to do something to restrict entities from moving in certain directions on certain nodes, but also be able to allow them at certain times.
In the Node class we'll create a new dictionary similar to the neighbors where the keys are the 4 directions any entity can go. The values to those keys though are the entities that have access to travel in that direction. By default both Pacman and the Ghost can travel in any possible direction when reaching a node. So if we want to restrict Pacman from moving down on any particular node, for instance, even if there's a connecting node in the DOWN direction we simply remove his name in the access dictionary on that node in the DOWN direction.
So to check to see if a particular entity has access to move in a particular direction on a node all we have to do is add this line to the validDirection method in the Entity class. All this line says is if this entity is in the access list for this direction, then let him pass. It's like trying to get into a party and if your name is not on the list, then you can't get in.
We'll add two new methods to the NodeGroup class that will restrict or allow access to a nodes direction. When we remove a name from the access dictionary we first make sure that it exists. When we add a name to the access dictionary for a node we first make sure that it does not exist. You don't want to have duplicate names.
In the startGame method we'll use these methods to prevent Pacman from moving down into the ghost home. We'll also prevent the ghosts from moving left and right when in the middle of the ghost home. That way they can never get stuck there. As soon as they reach the middle of the home, there's only 1 valid direction and that's UP.
We're also preventing Inky from moving RIGHT and Clyde from moving LEFT. That way they can never escape the home. We're doing this because we don't want them to leave right away. We want to keep them in the ghost home until some condition is met that will allow them to leave. We'll get to that later.
Notice that we're also preventing the ghosts from entering into the ghost home. They should only be allowed to enter if they are in SPAWN mode, so we'll have to allow them access in a bit.
There are also 4 nodes that the ghosts are not allowed to move UP on. This is just to show you that we can deny movement on any node in any direction. It's also how the original game is.
Go ahead and run the code to see what I mean. Try to get Pacman to enter the ghost home. You won't be able to! Bwahahahaha...
If you play the game at this point you'll first notice that Inky and Clyde keep bouncing UP and DOWN. This is to be expected and we'll fix it in a bit. But, you'll also notice that when Pacman eats a Power Pellet and then eats a ghost, that ghost will just circle around the ghost home forever. This is because we denied him access, which is the normal thing to do. When he is in SPAWN mode, however, we'll need to grant him access to the ghost home. Then when he reaches the spawn node, we need to immediately deny him access to the home again. That way he can leave and not enter again until he's in SPAWN mode again.
The only issue now is that Inky and Clyde can never leave as mentioned previously. To get them to leave some condition needs to be met first. When that condition occurs, then we can allow them to leave. That condition depends on the number of pellets Pacman eats. When Pacman eats 30 pellets, Inky can leave. When Pacman eats 70 pellets then Clyde can leave.