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

后端 未结 22 1794
南方客
南方客 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 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 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

提交回复
热议问题