吃豆人幽灵人工智能

发布于 2024-12-16 01:48:15 字数 404 浏览 4 评论 0原文

我目前正在用java制作pacman游戏。不过我有一个关于鬼魂的问题。

我知道鬼魂的攻击方式并不都是一样的。我首先想研究让幽灵追上吃豆人的基础知识,而不用担心其中的差异。

我向你们聪明人提出的问题是,让鬼魂追逐吃豆人但有时会随机改变路径的最佳方法是什么。我目前正在使用 21 x 21 2D 数组来告诉墙壁在哪里,所以我想让它更多地尝试并前往 pacman 当前的网格位置。 (例如转到10,14)当然,同时避免像吃豆人一样穿墙。我想知道如何才能让它做到这一点,并且让鬼魂有时停下来并走向另一个方向或其他东西,这样它就不会总是持续不断的追逐,吃豆人有机会逃脱。也许你们中的一些人已经编写了吃豆人游戏,或者只是知道一个好方法。任何帮助将不胜感激。

(请注意,我目前正在学习 11 年级的计算机科学课程,并且正在学习 Java 的第一学期的一半。)

I'm currently making a pacman game in java. I have a question about the ghosts though.

I understand that the ghosts do not all have the same style of attack. I first want to work on the basics of getting the ghost to go after the pacman and not worry about there differences yet.

My question to you smart people out there is what would be the best way to make the ghosts chase the pacman but sometimes randomly divert paths. I'm currently using a 21 by 21 2D array for telling where walls are and such so I was thinking make it more try and head for the current grid location of pacman. (for example go to 10,14) Of course while avoiding going through walls like pacman. I'm wondering how I could make it do this and also have the ghosts sometimes stop and go another direction or something so that it's not always a constant chase and pacman has a chance to get away. Maybe some of you have programmed a pacman game or just know a good way for this. Any help would be greatly appreciated.

(Please note I'm currently in a Grade 11 Computer Science course and halfway through the first semester ever of learned java.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总以为 2024-12-23 01:48:15

如果你只是想让幽灵的行为不一样,那么每次它们遇到十字路口时,就将它们的决定随机组合一些合理的追逐默认值(例如继续以最短距离到达吃豆人的路线 - 使用 Dijkstra 算法 在所有后继者上选择最好的一个)和随机选择。

If you just want the ghosts not all to behave the same, each time they encounter an intersection, make their decision a random mix of some reasonable chasing default (such as continuing the way with the shortest distance to Pacman — Use Dijkstra's algorithm on all successors to pick the best one) and a random choice.

冰魂雪魄 2024-12-23 01:48:15

我发现这篇文章非常有帮助:了解吃豆人幽灵行为 ,因为它解释了您所需要的。

I found very helpful this article: Understanding Pac-Man Ghost Behavior, as it explains just what you need.

晨曦慕雪 2024-12-23 01:48:15

这是一种可能性:对于幽灵可以采取的所有步骤,计算该步骤是否会使其更接近吃豆人。这可以通过曼哈顿距离来完成,在二维网格中它只是x 距离 + y 距离。然后随机选择一个步骤,将更高的概率分配给那些实际上会更接近的步骤。

如果您有一个数组 steps,其中前 n_ending_in 步代表将让幽灵更接近 Pacman 的步数,那么您可以为它们指定总概率为 prob_ending_in

double total_probility = 1.;
for (int i=0; i<n_closing_in; i++) {
    step_prob[i] = prob_closing_in / n_closing_in;
    total_probability -= prob_closing_in / n_closing_in;
}

然后类似地将 total_probability 中剩余的内容分配到将使幽灵远离 Pacman 的步骤上。

Step random_step(Step[] possible_steps, double[] step_prob) {
    double r = Math.random();

    int i;
    for (i=0; i<possible_steps.length(); i++) {
        if (r < step_prob[i])
            break;
        r -= step_prob[i];
    }
    return possible_steps[i];
}

如果迷宫不是太复杂并且接近的概率是 >.5,幽灵会看起来像是在追逐吃豆人,但是是以一种随意的方式。

prob_ending_in 提高到 1. 将使游戏变得更加困难。

(以上所有代码都未经测试,可能包含错误。我不太擅长 Java。)

Here's a possibility: for all steps that the ghost can take, compute whether that step will bring it closer to Pacman. That can be done with the Manhattan distance, which in a 2d grid is just the x distance + the y distance. Then randomly pick a step, with higher probability assigned to those steps that will actually take it closer.

If you have an array steps with the first n_closing_in steps representing the steps that will take the ghost closer to Pacman, then you can assign those a total probability of prob_closing_in with

double total_probility = 1.;
for (int i=0; i<n_closing_in; i++) {
    step_prob[i] = prob_closing_in / n_closing_in;
    total_probability -= prob_closing_in / n_closing_in;
}

Then similarly distribute what's left in total_probability over the steps that will take the ghost farther away from Pacman.

Step random_step(Step[] possible_steps, double[] step_prob) {
    double r = Math.random();

    int i;
    for (i=0; i<possible_steps.length(); i++) {
        if (r < step_prob[i])
            break;
        r -= step_prob[i];
    }
    return possible_steps[i];
}

If the maze isn't too complicated and the probability of closing in is >.5, the ghosts will appear to chase after Pacman, but in a haphazard way.

Raising prob_closing_in toward 1. will make the game more difficult.

(All of the above code is untested and may contain bugs. I'm not too good at Java.)

听风吹 2024-12-23 01:48:15

在我的大学人工智能课程中,我使用旧版本的 Pacman Ghost 人工智能框架做了一些工作。看起来当前版本可以在这里找到 http://www.pacman-vs-ghosts.net /软件(现已死亡)。在此,您将开发自己的 Ghost 代理来尝试捕获代理或用户控制的 Pacman。

这对于尝试不同的人工智能技术非常有用。

对于您的问题,该框架中的级别实际上是根据图形而不是二维数组构建的。您可以查看代码以了解他们是如何做到的。

原来的链接已经失效,目前还没有找到官方的镜像。不过,以下内容可能会有所帮助:

了解吃豆人幽灵行为
吃豆人女士 vs Ghost AI (GitHub)
PacMan_v6.2 (GitHub)

For my college level AI course, I did some work with an older version of the Pacman Ghost AI framework. It looks like the current version can be found here http://www.pacman-vs-ghosts.net/software (Now DEAD). In this you would develop your own Ghost agents to try and capture an agent or user controlled Pacman.

This was very useful in playing around with different AI techniques.

More to your question, the levels in that framework are actually built out of a graph and not a 2d array. You might take a look at the code to see how they do it.

The original link is dead and so far haven't been able to find an official mirror. However, the below will probably be of help:

Understanding Pac Man Ghost Behavior
Ms Pac Man Vs Ghost AI (GitHub)
PacMan_v6.2 (GitHub)

-柠檬树下少年和吉他 2024-12-23 01:48:15

这种人工智能最简单的实现是使用简单的图搜索算法。 BFS 是一种简单但可行的方法。然而,您当然希望实施启发式方法来更好地优化运行时间,因此从幽灵代理到吃豆人的简单曼哈顿距离就足够了。

摘要:采用曼哈顿距离启发式的 BFS。

如果您想变得更有趣,并使您的幽灵在狩猎时更加高效,您可以实施游戏状态启发式(例如,基于幽灵到吃豆人的距离,以及到目前为止吃豆人已经吃掉了多少点)当鬼魂必须选择下一步要采取的行动时使用。在这种情况下,您可以使用修剪技术来缩短运行时间。

Easiest implementations of such an AI would be to use a naive graph search algorithm. BFS would be a simple one which would work. However, you'd want to implement a heuristic to better optimize runtime of course, so a simple Manhattan distance from ghost agent to Pac-man would suffice.

Summary: BFS with Manhattan distance heuristic.

If you want to get fancier, and make your ghosts even more efficient at hunting, you can implement a game state heuristic (based on distance from ghost to Pac-man, and how many dots Pac-man has eaten so far, for eg) to use for when the ghost has to chose which next move to take. You can use pruning techniques to shorten runtime in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文