在PacMan游戏中创建自动运行PacMan的Java代码

在PacMan游戏中创建自动运行PacMan的Java代码,java,auto,pacman,Java,Auto,Pacman,我正试图从Mason 19网站修改pacman游戏。我要做的是让pacman自己运行,并尽可能快地遍历给定的任何棋盘。到目前为止,我已经实现了让pacman独立运行,但它在板的顶部陷入了一个循环。我想知道的是,什么才是使它工作而不陷入循环的最佳方式 我使用的程序代码来自Mason19 我修改的代码在Pac.java和doPolicyStep类中 protected void doPolicyStep(SimState state) { if(lastAction

我正试图从Mason 19网站修改pacman游戏。我要做的是让pacman自己运行,并尽可能快地遍历给定的任何棋盘。到目前为止,我已经实现了让pacman独立运行,但它在板的顶部陷入了一个循环。我想知道的是,什么才是使它工作而不陷入循环的最佳方式

我使用的程序代码来自Mason19

我修改的代码在Pac.java和doPolicyStep类中

    protected void doPolicyStep(SimState state)
    {
        if(lastAction == NOTHING)
        {
            if(isPossibleToDoAction(Pac.E))
            {
                pacman.actions[0] = Pac.E;
                performAction(Pac.E);
            }
            else if(isPossibleToDoAction(Pac.S))
            {
                pacman.actions[0] = Pac.S;
                performAction(Pac.S);
            }
            else if(isPossibleToDoAction(Pac.N))
            {
                pacman.actions[0] = Pac.N;
                performAction(Pac.N);
            }
            else
            {
                pacman.actions[0] = Pac.W;
                performAction(Pac.W);

            }
        }

        else if(lastAction == Pac.E)
        {
            if(isPossibleToDoAction(Pac.E)) 
            {
                pacman.actions[0] = Pac.E;
                performAction(Pac.E);
            }
            else if(isPossibleToDoAction(Pac.S))
            {
                pacman.actions[0] = Pac.S;
                performAction(Pac.S);
            }
            else if(isPossibleToDoAction(Pac.N))
            {
                pacman.actions[0] = Pac.N;
                performAction(Pac.N);
            }
            else
            {
                pacman.actions[0] = Pac.W;
                performAction(Pac.W);
            }
        }
        else if(lastAction == Pac.S)
        {
            if(isPossibleToDoAction(Pac.S)) 
            {
                pacman.actions[0] = Pac.S;
                performAction(Pac.S);
            }
            else if(isPossibleToDoAction(Pac.W))
            {
                pacman.actions[0] = Pac.W;
                performAction(Pac.W);
            }
            else if(isPossibleToDoAction(Pac.E))
            {
                pacman.actions[0] = Pac.E;
                performAction(Pac.E);
            }
            else
            {
                pacman.actions[0] = Pac.N;
                performAction(Pac.N);
            }
        }
        else if(lastAction == Pac.W)
        {
            if(isPossibleToDoAction(Pac.W)) 
            {
                pacman.actions[0] = Pac.W;
                performAction(Pac.W);
            }
            else if(isPossibleToDoAction(Pac.N))
            {
                pacman.actions[0] = Pac.N;
                performAction(Pac.N);
            }
            else if(isPossibleToDoAction(Pac.S))
            {
                pacman.actions[0] = Pac.S;
                performAction(Pac.S);
            }
            else
            {
                pacman.actions[0] = Pac.E;
                performAction(Pac.E);
            }
        }
        else if(lastAction == Pac.N)
        {
            if(isPossibleToDoAction(Pac.N)) 
            {
                pacman.actions[0] = Pac.N;
                performAction(Pac.N);
            }
            else if(isPossibleToDoAction(Pac.W))
            {
                pacman.actions[0] = Pac.W;
                performAction(Pac.W);
            }
            else if(isPossibleToDoAction(Pac.E))
            {
                pacman.actions[0] = Pac.E;
                performAction(Pac.E);
            }
            else
            {
                pacman.actions[0] = Pac.S;
                performAction(Pac.S);
            }
        }
    int nextAction = pacman.getNextAction(tag);
/**方法isPossibleToDoAction()确定代理是否可以使用给定的操作(N/W/S/E/NOTHING)移动,而不会撞到墙*/

public boolean isPossibleToDoAction(int action)
    {
    if (action == NOTHING)
        {
        return false;  // no way
        }
    IntGrid2D maze = pacman.maze;
    int[][] field = maze.field;

    // the Agents grid is discretized exactly on 1x1 boundaries so we can use floor rather than divide

    // the agent can straddle two locations at a time.  The basic location is x0, y0, and the straddled location is x1, y1.
    // It may be that x0 == y0.
    int x0 = (int) location.x;
    int y0 = (int) location.y;
    int x1 = location.x == x0 ? x0 : x0 + 1;
    int y1 = location.y == y0 ? y0 : y0 + 1;

    // for some actions we can only do the action if we're not straddling, or if our previous action was NOTHING
    if ((x0 == x1 && y0 == y1) || lastAction == NOTHING)
        {
        switch (action)
            {
            // we allow toroidal actions
            case N:
                return (field[maze.stx(x0)][maze.sty(y0 - 1)] == 0);
            case E:
                return (field[maze.stx(x0 + 1)][maze.sty(y0)] == 0);
            case S:
                return (field[maze.stx(x0)][maze.sty(y0 + 1)] == 0);
            case W:
                return (field[maze.stx(x0 - 1)][maze.sty(y0)] == 0);
            }
        } // for other actions we're continuing to do what we did last time.
    // assuming we're straddling, this should always be allowed unless our way is blocked
    else if (action == lastAction)
        {
        switch (action)
            {
            // we allow toroidal actions
            case N:  // use y0
                return (field[maze.stx(x0)][maze.sty(y0)] == 0);
            case E:  // use x1
                return (field[maze.stx(x1)][maze.sty(y0)] == 0);
            case S:  // use y1
                return (field[maze.stx(x0)][maze.sty(y1)] == 0);
            case W:  // use x0
                return (field[maze.stx(x0)][maze.sty(y0)] == 0);
            }
        } // last there are reversal actions.  Generally these are always allowed as well.
    else if ((action == N && lastAction == S) ||
        (action == S && lastAction == N) ||
        (action == E && lastAction == W) ||
        (action == W && lastAction == E))
        {
        return true;
        }

    return false;
    }

我相信我需要使用类似BFS搜索的方法来找到最短路径,我只是不知道如何做到这一点。

“它陷入了一个循环中”,我在你的代码中没有看到任何循环,也没有看到
isPossibleToDoAction
做什么?代码中没有循环,但运行时在电路板的左上角有pacman循环。isPossibleToDoAction检查pacman运行时遇到墙时是否会在板上出现N、S、E或W。我的意思是你可以发布该方法的代码。@JoakimDanielson我编辑了原始帖子并包含了该方法。@JoakimDanielson我觉得我需要一个堆栈或类似的东西来跟踪每次移动。