Pacman: how do the eyes find their way back to the monster hole?

后端 未结 22 1786
南方客
南方客 2021-01-29 17:53

I found a lot of references to the AI of the ghosts in Pacman, but none of them mentioned how the eyes find their way back to the central ghost hole after a ghost is eaten by Pa

相关标签:
22条回答
  • 2021-01-29 17:59

    You should take a look a pathfindings algorithm, like Dijsktra's Algorithm or A* algorithm. This is what your problem is : a graph/path problem.

    0 讨论(0)
  • 2021-01-29 18:00

    For an alternative to more traditional pathfinding algorithms, you could take a look at the (appropriately-named!) Pac-Man Scent Antiobject pattern.

    You could diffuse monster-hole-scent around the maze at startup and have the eyes follow it home.

    Once the smell is set up, runtime cost is very low.


    Edit: sadly the wikipedia article has been deleted, so WayBack Machine to the rescue...

    0 讨论(0)
  • 2021-01-29 18:00

    My approach is a little memory intensive (from the perspective of Pacman era), but you only need to compute once and it works for any level design (including jumps).

    Label Nodes Once

    When you first load a level, label all the monster lair nodes 0 (representing the distance from the lair). Proceed outward labelling connected nodes 1, nodes connected to them 2, and so on, until all nodes are labelled. (note: this even works if the lair has multiple entrances)

    I'm assuming you already have objects representing each node and connections to their neighbours. Pseudo code might look something like this:

    public void fillMap(List<Node> nodes) { // call passing lairNodes
        int i = 0;
    
        while(nodes.count > 0) {
            // Label with distance from lair
            nodes.labelAll(i++);
    
            // Find connected unlabelled nodes
            nodes = nodes
                .flatMap(n -> n.neighbours)
                .filter(!n.isDistanceAssigned());
        }
    }
    

    Eyes Move to Neighbour with Lowest Distance Label

    Once all the nodes are labelled, routing the eyes is trivial... just pick the neighbouring node with the lowest distance label (note: if multiple nodes have equal distance, it doesn't matter which is picked). Pseudo code:

    public Node moveEyes(final Node current) {
        return current.neighbours.min((n1, n2) -> n1.distance - n2.distance);
    }
    

    Fully Labelled Example

    0 讨论(0)
  • 2021-01-29 18:01

    Actually, I'd say your approach is a pretty awesome solution, with almost zero-run time cost compared to any sort of pathfinding.

    If you need it to generalise to arbitrary maps, you could use any pathfinding algorithm - breadth-first search is simple to implement, for example - and use that to calculate which directions to encode at each of the corners, before the game is run.

    EDIT (11th August 2010): I was just referred to a very detailed page on the Pacman system: The Pac-Man Dossier, and since I have the accepted answer here, I felt I should update it. The article doesn't seem to cover the act of returning to the monster house explicitly but it states that the direct pathfinding in Pac-Man is a case of the following:

    • continue moving towards the next intersection (although this is essentially a special case of 'when given a choice, choose the direction that doesn't involve reversing your direction, as seen in the next step);
    • at the intersection, look at the adjacent exit squares, except the one you just came from;
    • picking one which is nearest the goal. If more than one is equally near the goal, pick the first valid direction in this order: up, left, down, right.
    0 讨论(0)
  • 2021-01-29 18:02

    I don't know much on how you implemented your game but, you could do the following:

    1. Determine the eyes location relative position to the gate. i.e. Is it left above? Right below?
    2. Then move the eyes opposite one of the two directions (such as make it move left if it is right of the gate, and below the gate) and check if there are and walls preventing you from doing so.
    3. If there are walls preventing you from doing so then make it move opposite the other direction (for example, if the coordinates of the eyes relative to the pin is right north and it was currently moving left but there is a wall in the way make it move south.
    4. Remember to keep checking each time to move to keep checking where the eyes are in relative to the gate and check to see when there is no latitudinal coordinate. i.e. it is only above the gate.
    5. In the case it is only above the gate move down if there is a wall, move either left or right and keep doing this number 1 - 4 until the eyes are in the den.
    6. I've never seen a dead end in Pacman this code will not account for dead ends.
    7. Also, I have included a solution to when the eyes would "wobble" between a wall that spans across the origin in my pseudocode.

    Some pseudocode:

       x = getRelativeOppositeLatitudinalCoord()
       y
       origX = x
        while(eyesNotInPen())
           x = getRelativeOppositeLatitudinalCoordofGate()
           y = getRelativeOppositeLongitudinalCoordofGate()
           if (getRelativeOppositeLatitudinalCoordofGate() == 0 && move(y) == false/*assume zero is neither left or right of the the gate and false means wall is in the way */)
                while (move(y) == false)
                     move(origX)
                     x = getRelativeOppositeLatitudinalCoordofGate()
            else if (move(x) == false) {
                move(y)
        endWhile
    
    0 讨论(0)
  • 2021-01-29 18:03

    dtb23's suggestion of just picking a random direction at each corner, and eventually you'll find the monster-hole sounds horribly ineficient.

    However you could make use of its inefficient return-to-home algorithm to make the game more fun by introducing more variation in the game difficulty. You'd do this by applying one of the above approaches such as your waypoints or the flood fill, but doing so non-deterministically. So at every corner, you could generate a random number to decide whether to take the optimal way, or a random direction.

    As the player progresses levels, you reduce the likelihood that a random direction is taken. This would add another lever on the overall difficulty level in addition to the level speed, ghost speed, pill-eating pause (etc). You've got more time to relax while the ghosts are just harmless eyes, but that time becomes shorter and shorter as you progress.

    0 讨论(0)
提交回复
热议问题