Commit 2237727f authored by hosein's avatar hosein

queen -> canRoute added

parent f51b598c
...@@ -26,7 +26,7 @@ public class Queen extends Nut { ...@@ -26,7 +26,7 @@ public class Queen extends Nut {
if (x == destination[0]) if (x == destination[0])
return false; return false;
else else
return Math.abs((y - destination[1])/(x - destination[0])) == 1; return Math.abs((y - destination[1]) / (x - destination[0])) == 1;
} }
} }
...@@ -35,5 +35,45 @@ public class Queen extends Nut { ...@@ -35,5 +35,45 @@ public class Queen extends Nut {
isAlive = false; isAlive = false;
} }
@Override
public boolean canRoute(int[] destination, int[][] ground) {
if (destination[0] == x) {
int t;
if (destination[1] - y > 0) t = 1;
else t = -1;
for (int i = y + t; i < destination[1]; i += t)
if (ground[x][i] != 0)
return false;
} else {
int t;
if (destination[0] - x > 0) t = 1;
else t = -1;
for (int i = x + t; i < destination[0]; i += t)
if (ground[i][y] != 0)
return false;
}
int X, Y;
if (destination[0] < x) X = -1;
else X = 1;
if (destination[1] < y) Y = -1;
else Y = 1;
for (int i = x + X, j = y + Y; i == destination[0]; i += X, j += Y)
if (ground[i][j] != 0)
return false;
return true;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment