-1

So, I create my own Snake game with AI, but now I have very-very weird snake actions.

Video

So, my algorithm is:

  1. Get map with: 1 - path is clear, 0 - no path(walls, snake)
  2. Get fruit position
  3. Get SnakeAI head position
  4. Send it to A* algorithm(work correctly)
  5. Get path (Array of Points; end is fruit position, start is snakeAi head)
  6. Set direction of snakeAI

Code on Pastebin

private void AI() {

        Point s = new Point();
        //SnakeAI head
        Point n = new Point();
        //Food
        n.x = food.x / 13;
        n.y = food.y / 13;
        s.x = snakeAI.getBody().get(0).x / 13;
        s.y = snakeAI.getBody().get(0).y / 13;

        int px;
        int py;

        //Path
        g = f.findPath(map, n, s);

        if (g.size() > 0) {
        //Next snake positin
            px = g.get(g.size() - 1).x;
            py = g.get(g.size() - 1).y;
        } else {
           return;
            }

            if (s.x == px) {  
                //Move UP
                if (s.y > py) {
                    snakeAI.setDirection(Snake.DIR_UP);
                }
                //Move DOWN
                if (s.y < py) {
                    snakeAI.setDirection(Snake.DIR_DOWN);
                }
            }
            if (s.y == py) {
                //Move LEFT    
                if (s.x > px) {
                    snakeAI.setDirection(Snake.DIR_LEFT);
                }
                //Move RIGHT
                if (s.x < px) {
                    snakeAI.setDirection(Snake.DIR_RIGHT);
                }
            }
        }

So, I fight with that wrong moves about week, and I really don't know where error is. Yoda Master, help me.

ziggystar
  • 28,410
  • 9
  • 72
  • 124
JohnDow
  • 1,242
  • 4
  • 22
  • 40

1 Answers1

1

I know what the problem is!

In Snake, you can't turn around over yourself. In other words, if you are moving left, you can't move right, you have to move up first. However, your code is ordering the snake to move incorrectly without regard to it's previous direction. It shouldn't be able to go up then down etc.

Fix that problem and you will probably fix the whole thing. Make sure the A* algorithm factors in that turning around is an illegal direction.

Here's the correct pseudocode:

  1. see if we are going in the correct direction
    • if yes, continue
    • if no, figure out if A* wants us to turn left or right
  2. move one square
  3. repeat step 1

The logic should be something like this:

switch (snakeAI.getDirection()) {
  case Snake.DIR_LEFT:
    if (s.x >= px) { // don't go left anymore
      if (s.y > py) {
        snakeAI.setDirection(Snake.DIR_UP);
      } else if (s.y < py) {
        snakeAI.setDirection(Snake.DIR_DOWN);
      } else {
        // You should probably change this
        snakeAI.setDircetion(Snake.DIR_DOWN);
      }
    }
    break;
  case Snake.DIR_RIGHT:
durron597
  • 31,968
  • 17
  • 99
  • 158
  • No. it is OK. I use my g2 vars (vars*13px) – JohnDow Nov 28 '12 at 19:24
  • Can you create a new pastebin with the following: 1) A more localized printout (i.e. only when it starts misbehaving) and 2) Prints the location of the fruit – durron597 Nov 28 '12 at 19:30
  • Umm.... the snake gets to 4,26 in that pastebin. You sure it's not a rounding error? Or possibly a +/- 1 error somewhere – durron597 Nov 28 '12 at 19:34
  • Snake gets to 4.27, and then go out of board. And snake always get first fruit right. (I check A* algo, and algo always work great) – JohnDow Nov 28 '12 at 19:38
  • If you have a time, there is [full project](https://github.com/il-vladislav/PR/tree/master/SnakeClear) – JohnDow Nov 28 '12 at 19:40
  • But look what it's doing in the video, snakes are not allowed to do that – durron597 Nov 28 '12 at 19:44
  • What's about problem, when snake must turn left, but go UP 3 times and then 3 times down forever? – JohnDow Nov 28 '12 at 19:45
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20266/discussion-between-durron597-and-vladislav-ilushin) – durron597 Nov 28 '12 at 19:49