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

2015/5/25

Creating Basic AI?

1
2
MeinKraftBubba MeinKraftBubba

2015/5/28

#
Okay, I've updated the code using the suggestions made by Super_Hippo, as well as what seemed to logically follow based off of the errors I received when compiling. It still receives an error, but I can tell it's getting closer to completion. Here's the code for both again. With Blinky, I omitted the animation code to save up some room, otherwise this is everything in his code:
import greenfoot.*; 

public class Blinky extends Ghost
{
    //Create Image Variables for Animation
   
    //Create movement variables
    int speed;
    public int dir;

    
    public Blinky()
    {
        //Construct Ghost upon spawning
        //Assign image files to image variables
        
        //Assign initial values to animation variables
        
        //Assign values to movement variables
        speed = 2;
        dir = 1;
       ;
    }
    public void act() 
    {
       
        //checkKeyPress();
        checkMove();
    }
    public void checkKeyPress()
    {
        //For testing purposes
        if(Greenfoot.isKeyDown("up"))
        {
            dir = 4;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            dir = 2;
        }
        if(Greenfoot.isKeyDown("left"))
        {
            dir = 3;
        }
        if(Greenfoot.isKeyDown("right"))
        {
            dir = 1;
        }
    }
    public void checkMove()
    {
        checkDirection();
        if(dir == 4)
        {
            //UP
            setLocation(getX(), getY() - speed);
        }
        if(dir == 2)
        {
            //DOWN
            setLocation(getX(), getY() + speed);
        }
        if(dir == 3)
        {
            //LEFT
            setLocation(getX() - speed, getY());
        }
        if(dir == 1)
        {
            //RIGHT
            setLocation(getX() + speed, getY());
        }
    }
    
    public void setDirection(int currentDirection)
    {
        dir = currentDirection;
    }
    public void checkDirection()
    {
        BlinkyPad bp = (BlinkyPad) getOneObjectAtOffset(0, 0, BlinkyPad.class);
        if(bp != null)
        {
            setDirection(bp.getDirection(currentDirection));
        }
    }

}
And here's the updated BlinkyPad:
import greenfoot.*;  

public class BlinkyPad extends MovePad
{
    public int currentDirection;
    boolean d1; //Right
    boolean d2; //Down
    boolean d3; //Left
    boolean d4; //UP
    public BlinkyPad()
    {  

    }

    public int getDirection(int currentDirection)
    {
        boolean[] d =
            {
                getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 3,
                getOneObjectAtOffset(0, -15, Wall.class) == null && currentDirection != 4,
                getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 1,
                getOneObjectAtOffset(0, 15, Wall.class) == null && currentDirection != 2,
            };
        while(true)
        {
            int r = Greenfoot.getRandomNumber(4) +1;
            if (d[r]) return r;
        }

    }
}
So, what's next? Again, I can't stress how much I appreciate the help.
danpost danpost

2015/5/28

#
In the BlinkyPad class, you can remove lines 5 through 9. Then, insert the following at line 19 of the BlinkyPad class code above:
false,
MeinKraftBubba MeinKraftBubba

2015/5/28

#
Okay, having made those changes, what needs to be fixed in the Blinky class?
You need to login to post a reply.
1
2