java - 尝试让人工智能使用目标优先系统

标签 java swing jframe artificial-intelligence

我为 Agar.io 副本制作了一个 AI,AI 会跟随比它小的最近的单元格并尝试吃掉它,但是我想让它根据优先级跟随附近的单元格,所以它是不一定是距离 AI 最近的单元格,例如:距离 AI 最近的单元格比距离稍远且大得多的单元格小,请选择较大的单元格,因为它的质量使其具有更高的优先级分数。

这是我的代码。 (如果您需要完整类(class),here is the entire class on pastebin.)

public void Update() {
        if (this.mass > 3500) {
            this.mass = 3500;
        }
        for (Cell cell : cells) {
            if (this.checkCollide(cell.x, cell.y, cell.mass) && this != cell && this.mass > cell.mass + 10) {
                if (1 / (this.mass / cell.mass) >= .4 && this.mass < 4000) {
                    addMass(cell.mass);
                }
                respawn(cell);
            }
        }
        if (!isPlayer) {
            if (goalReached) {
                if (returnNearestCell() > -1) { // No Cell Found
                    if (!isTarget) {
                        target = cells.get(returnNearestCell());
                        isTarget = true;
                        targetType = "c";
                    } else if (isTarget && targetType.equals("c")) {
                        targetType = "n";
                        isTarget = false;
                    }
                } else if (returnNearestCell() == -1) { // Cell Found
                    if (!isTarget) {
                        pTarget = Particle.particles.get(returnNearestP());
                        isTarget = true;
                        targetType = "p";
                    } else if (isTarget) {
                        targetType = "n";
                        isTarget = false;
                    }
                }
                goalReached = false;
            } else {
                double dx = 0;

                double dy = 0;
                if (targetType.equals("c")) {
                    if (returnNearestCell() > -1) {
                        target = cells.get(returnNearestCell());
                        dx = (target.x - this.x);
                        dy = (target.y - this.y);
                    } else {
                        goalReached = true;
                    }
                } else if (targetType.equals("p")) {
                    pTarget = Particle.particles.get(returnNearestP());
                    dx = (pTarget.x - this.x);
                    dy = (pTarget.y - this.y);
                } else {
                    goalReached = true;
                }
                double distance = Math.sqrt((dx) * (dx) + (dy) * (dy));
                if (distance > 1) {
                    x += (dx) / distance * speed;
                    y += (dy) / distance * speed;
                } else if (distance <= 1) {
                    goalReached = true;
                }

            }
        } else {
            double dx = (goalX - this.x);
            double dy = (goalY - this.y);
            this.x += (dx) * 1 / 50;
            this.y += (dy) * 1 / 50;
            // addMass(10);
        }
    }
<小时/>

更新:这是我到目前为止为优先级系统编写的内容,然后我可以在 Update() 函数中调用它,但是目前我对下一步该做什么一无所知。 .

    public int targetPriority() {
        int priority = 0;
        int distance = 0;

        int mass = 0;

        List<Cell> possibleTarget = null;

        int aPriority = 0;
        int bPriority = 0;
        int cPriority = 0;

        for (Cell cell : cells) {
            mass = cell.mass / 10;

            distance = (int) Math.sqrt((this.x - cell.x) * (this.x - cell.x) + (cell.y - this.y) * (cell.y - this.y));
            priority = distance - mass;

            for (Cell A : possibleTarget) {
                for (Cell B : possibleTarget) {
                    for (Cell C : possibleTarget) {
                    }
                }
            }   
        }
        return priority;
    }

最佳答案

我将使用评分系统解决上述问题,对于每个附近的智能体,根据其距离和大小给它一个分数,然后将智能体的分数插入基于优先级队列的分数。

例如:假设您附近有 3 个代理 A、B 和 C A:1m距离&4尺寸 B:2m距离&1尺寸 C:5m距离&8尺寸

假设他们都比你小,那么他们将根据距离和大小之间的差异进行评分,如下所示:

A-->3
B-->1
C-->3

A 和 C 相等-->选择最接近的一个

这是问题的简单解决方案,但真正的解决方案是使用启发式函数并确定选择的最佳路径

heuristic

关于java - 尝试让人工智能使用目标优先系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55997617/

相关文章:

Java hadoop 错误值类 : class RatioCount$WritableArray is not class org. apache.hadoop.io.DoubleWritable

java - 从 JTable CellEditor 中删除 HTML

java - 如果 isDisplayChangeSupported() 显示不可用,是否可以启用全屏独占模式

java - EclipseWindow Builder - JFrame 无法缩放

Java显示消息问题

java - JFrame setComponentZOrder() 更改对象的大小

java - 参数化类中的空构造函数

java - 如何使用 Mockito 模拟被调用两次且第二个方法调用的参数是第一个方法调用的输出的方法

java - 为 Eclipse 项目设置日志记录配置文件

java - 在没有上下文菜单的表格中提供更多选项