BFS for pacman ghost <p>I've been several days with this problem. I've searched in this forum, and in google for a solution without any luck. My problem is I am not able to make a working BFS algorithm for managing the behaviour of a pacman ghost. I think I am ignoring something in the code. I'll paste the code here, if you can help me you'll be thanked :) pacman is number 2, and ghost is number 3.</p> <pre><code>Queue&lt;Tile&gt; path = new LinkedList&lt;&gt;(); Tile start = new Tile(canvas.g1.x, canvas.g1.y, 3); Tile end = new Tile(canvas.pacman.x, canvas.pacman.y, 2); start.distance = 1; path.add(start); while (!path.isEmpty()) { Tile current = path.remove(); System.out.println(path.size()); canvas.walls[current.y][current.x] = 0; if (canvas.walls[current.y][current.x] == canvas.walls[end.y][end.x]) { end = current; System.out.println("pacman found"); break; } ArrayList&lt;Tile&gt; list = adyacent(current); for (Tile c : list) { if (c.distance == 0) { c.distance = current.distance + 1; path.add(c); canvas.walls[c.y][c.x] = 20; for (Tile v : path) { System.out.println("x: " + v.x + " / y: " + v.y + " / distance: " + v.distance); } } } } int dist = end.distance; System.out.println("Recolection ended, size: " + dist); </code></pre> <p>The class Tile():</p> <pre><code>public class Tile { public int x; public int y; public int distance; public int personaje; public Tile(int x, int y, int personaje) { this.x = x; this.y = y; this.personaje = personaje; distance = 0; } } </code></pre> <p>and the method adyacent():</p> <p>in this method, the numbers I check are the ones with wall(each one with a diferent drawing))</p> <pre><code>public ArrayList&lt;Tile&gt; adyacent(Tile c) { ArrayList&lt;Tile&gt; surrounding = new ArrayList&lt;&gt;(); int ant; ant = canvas.walls[c.y][c.x + 1]; if ((ant != 7) &amp;&amp; (ant != 8) &amp;&amp; (ant != 9) &amp;&amp; (ant != 11) &amp;&amp; (ant != 12) &amp;&amp; (ant != 13) &amp;&amp; (ant != 14) &amp;&amp; (ant != 15) &amp;&amp; (ant != 16)) { surrounding.add(new Tile(c.x + 1, c.y, 3)); } ant = canvas.walls[c.y][c.x - 1]; if ((ant != 7) &amp;&amp; (ant != 8) &amp;&amp; (ant != 9) &amp;&amp; (ant != 11) &amp;&amp; (ant != 12) &amp;&amp; (ant != 13) &amp;&amp; (ant != 14) &amp;&amp; (ant != 15) &amp;&amp; (ant != 16)) { surrounding.add(new Tile(c.x - 1, c.y, 3)); } ant = canvas.walls[c.y + 1][c.x]; if ((ant != 7) &amp;&amp; (ant != 8) &amp;&amp; (ant != 9) &amp;&amp; (ant != 11) &amp;&amp; (ant != 12) &amp;&amp; (ant != 13) &amp;&amp; (ant != 14) &amp;&amp; (ant != 15) &amp;&amp; (ant != 16)) { surrounding.add(new Tile(c.x, c.y + 1, 3)); } ant = canvas.walls[c.y - 1][c.x]; if ((ant != 7) &amp;&amp; (ant != 8) &amp;&amp; (ant != 9) &amp;&amp; (ant != 11) &amp;&amp; (ant != 12) &amp;&amp; (ant != 13) &amp;&amp; (ant != 14) &amp;&amp; (ant != 15) &amp;&amp; (ant != 16)) { surrounding.add(new Tile(c.x, c.y - 1, 3)); } return surrounding; } </code></pre> <p>What I've observed is that it never reaches the System.out in which I return the size of the path. Also, where I see the size of the Queue, it seems right, but the System.out in which I see "x" and "y" and "distance" is bigger each time, much much bigger.</p> <p>I set that the checked tiles change it's color, changing the number which I've got in the array: 0 is floor, 20 is transparent green. And it remains in center, and beggins to work slower and slower.</p> <p>Forget to say that it never finds pacman :( thanks!</p>