Snake AI collide with himself when it shouldn't

60
January 20, 2021, at 8:00 PM

I've started to make a snake ai game.

I have a working A* pathfinder algorithm, which can find path to the food from the snakes head. The field is contained as a 2D array where 0 is the empty space where the snake is able to go and 1000 where is obstacles.

The problem is that the snake often collide its body so somehow a obstacle gets in the path array when it should not. I'm sure that i've set somewhere the direction to wrong but i cannot figure it out where.

This is my setDirection() class where it should return with the next direction to step:

public Directions setDirection() {
    int fieldWidth = (int) this.data.getFieldSize().width + 1;
    int fieldHeight = (int) this.data.getFieldSize().height + 1;
    int[][] field = new int[fieldWidth][fieldHeight];
    //Create array to contain obstacles
    for (int i = 0; i < fieldWidth; i++) {
        for (int j = 0; j < fieldHeight; j++) {
            if (this.data.getFieldElement(i, j) == FieldElement.EMPTY || this.adat.getFieldElement(i, j) == FieldElement.FOOD) {
                field[i][j] = 0; //not obstacles
            } else {
                field[i][j] = 1000; //obstacles
            }
        }
    }
    Point2D p = this.body.getHead();
    Directions direction = this.body.getDirection();
    //The A* pathfinder class/pathFinding init
    PathFinder path = new PathFinder(field, this.body.getHead());
    List<PathFinder.Node> pathToFood = path.findPathTo(this.data.getFood());
    //If path leads straight back
    if (this.body.getSize()>1 && pathToFood.get(1).x == this.body.getBodyPart(1).getX() && pathToFood.get(1).y == this.body.getBodyPart(1).getY() && pathToFood != null) {
        if(this.data.getFood().getY()>p.getY()&&this.data.getFood().getX()>p.getX()){
            if (direction == Directions.UP){
                direction = Directions.RIGHT;
            }else{
                direction = Directions.DOWN;
            }
        }else if(this.data.getFood().getY()<p.getY()&&this.data.getFood().getX()<p.getX()){
            if (direction == Directions.RIGHT){
                direction = Directions.UP;
            }else{
                direction = Directions.LEFT;
            }
        }else if(this.data.getFood().getY()<p.getY()&&this.data.getFood().getX()>p.getX()) {
            if (direction == Directions.DOWN){
                direction = Directions.RIGHT;
            }else{
                direction = Directions.UP;
            }
        }else if(this.data.getFood().getY()>p.getY()&&this.data.getFood().getX()<p.getX()) {
            if (direction == Directions.RIGHT){
                direction = Directions.DOWN;
            }else{
                direction = Directions.LEFT;
            }
        }
    }else{
        if (p.getX() > pathToFood.get(1).x) {
            direction = Directions.LEFT;
        } else if (p.getX() < pathToFood.get(1).x) {
            direction = Directions.RIGHT;
        } else if (p.getY() > pathToFood.get(1).y) {
            direction = Directions.UP;
        } else if (p.getY() < pathToFood.get(1).y) {
            direction = Directions.DOWN;
        }
    }
    return direction;
}

For easier debug i printed to console where the snakes head is ("K"), where the path leads(next step: "P") and the element in the path.

  • 0 - Empty, 2 - Food: allowed to go
  • 1 - Wall, 3 - Snake body: not allowed to go

The gotten output: this

I don't know where i made the mistake, it should work fine...

Thank you for the answers.

READ ALSO
How can I get data for select box and set the data while using searching?

How can I get data for select box and set the data while using searching?

There is a search function, so when I choose the sort and label select box then click the search button, it will sort dataBut the selected value is not set in the select box

69
Is it possible to log the bytes which a server sends? [closed]

Is it possible to log the bytes which a server sends? [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

102
What is the difference between // and .// in XPath?

What is the difference between // and .// in XPath?

When I execute these XPath expression on Chrome Developer Tools' console over googlecom, I got the same results

66