This site requires JavaScript, please enable it in your browser!
Greenfoot back
gaisuke
gaisuke wrote ...

2017/11/30

need help on my AI

gaisuke gaisuke

2017/11/30

#
so i'm working on some project for my final semester. then, i got stuck at programming my AI....
public void lariKeFlag (Flag finish){ //this should be the code for the AI to chase the flag. but here my code just "detect" the flag but the AI can't turn around a wall
        double deltaX = getX()-finish.getX();
        double deltaY = getY() - finish.getY();
        World world = getWorld();
        GreenfootImage background = world.getBackground();
        if(Math.abs(deltaX) > Math.abs(deltaY)){
            if(deltaX>0&&background.getColorAt(getX()-7,getY()).equals(white)){
                setLocation(getX() - 1, getY());
                
            }
            else if (deltaX<0&&background.getColorAt(getX() + 7, getY()).equals(white)){
                setLocation(getX() +1, getY());
                
            }
        }
        else {
        if (deltaY >0&&background.getColorAt(getX(), getY() + 7).equals(white)) {
            setLocation(getX(), getY()-1);
            
            
        }
        else if (deltaY<0&&background.getColorAt(getX(), getY() - 4).equals(white)) {
            setLocation(getX(), getY() +1);
            
        }
        }
    }
P.S my game is about maze, there is player, AI, and FLAG.... the goal is to get to flag both player and AI is racing to reach the flag P.S.S player, and AI detect the wall by looking for a color white as the path, black as the wall....
Super_Hippo Super_Hippo

2017/11/30

#
If you have a maze, then you maybe want to check which legal path is the shortest, ignoring the linear distance.
gaisuke gaisuke

2017/12/1

#
how to do that? what syntax do i have to use?
Super_Hippo Super_Hippo

2017/12/3

#
You have to write an algorithm for that. As long as the flag doesn't change its location and the maze doesn't change in some way, it is enough to calculate it once. I think the best way to do this would be to start at the flag, give it route length 0, the possible neighbor cells are then 1 and so on. And then, when you reach the AI, it will have a route length (X) too and can find the shortest route moving from X to X-1, X-2, ... to 2, 1, 0. You can then save this route in a list, so you don't have to calculate it again and again. I don't have the algorithm in code. Maybe you can find maze scenarios here which do something similar or you try to do it yourself.
You need to login to post a reply.