Commit f51b598c authored by hosein's avatar hosein

canRoute -> override added

parent 8ce0c0e3
package pack.Nut;
public class Soldier {
}
...@@ -33,7 +33,7 @@ public class Bishop extends Nut { ...@@ -33,7 +33,7 @@ public class Bishop extends Nut {
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (x == destination[0]) return false; if (x == destination[0]) return false;
else else
return Math.abs((y - destination[1])/(x - destination[0])) == 1; return Math.abs((y - destination[1]) / (x - destination[0])) == 1;
} }
@Override @Override
...@@ -41,6 +41,20 @@ public class Bishop extends Nut { ...@@ -41,6 +41,20 @@ public class Bishop extends Nut {
isAlive = false; isAlive = false;
} }
@Override
public boolean canRoute(int[] destination, int[][] ground) {
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;
}
} }
...@@ -42,4 +42,8 @@ public class Knight extends Nut { ...@@ -42,4 +42,8 @@ public class Knight extends Nut {
isAlive = false; isAlive = false;
} }
@Override
public boolean canRoute(int[] destination, int[][] ground) {
return true;
}
} }
package pack.Nuts; package pack.Nuts;
public abstract class Nut { public abstract class Nut {
public abstract void move(); protected int id, x, y; // 1 means white & -1 means black
protected boolean isAlive;
public void move(int[] destination) {
x = destination[0];
y = destination[1];
}
public abstract boolean canRoute(int[] destination, int[][] ground);
public abstract boolean canMove(int[] destination);
public void changeAlive() {
isAlive = false;
}
} }
...@@ -22,16 +22,26 @@ public class Pawn extends Nut { ...@@ -22,16 +22,26 @@ public class Pawn extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (id > 0) { if (id > 0) {
if (x == 6 && destination[0] - x == 2 && destination[1] == y) return true; if (x == 6 && destination[0] - x == 2 && destination[1] == y) return true;
return destination[0] < x && destination[0] >= x - 1 && destination[1] <= y + 1 && destination[1] >= y - 1; return destination[0] < x && destination[0] >= x - 1 && destination[1] <= y + 1 && destination[1] >= y - 1;
}else{ } else {
if (x == 1 && destination[0] - x == 2 && destination[1] == y) return true;
return destination[0] > x && destination[0] <= x + 1 && destination[1] <= y + 1 && destination[1] >= y - 1; return destination[0] > x && destination[0] <= x + 1 && destination[1] <= y + 1 && destination[1] >= y - 1;
}} }
}
@Override @Override
public void changeAlive() { public void changeAlive() {
isAlive = false; isAlive = false;
} }
@Override
public boolean canRoute(int[] destination, int[][] ground) {
if (destination[0] - x == 2 && ground[(x + destination[0])/2][y] != 0) return false;
return true;
}
} }
...@@ -40,4 +40,29 @@ public class Rook extends Nut { ...@@ -40,4 +40,29 @@ public class Rook 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;
}
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