Browse Source

Added isRelativeMoveValid for checking theoretical movement of figures

feature-chess
Nick Stolbov 3 years ago
parent
commit
161cf9bb5b
  1. 38
      src/main/java/Game/ChessObj/ChessFigure.java
  2. 65
      src/test/java/Game/ChessObj/ChessFigureTest.java

38
src/main/java/Game/ChessObj/ChessFigure.java

@ -61,6 +61,44 @@ public class ChessFigure {
return symbol; return symbol;
} }
public boolean isRelativeMoveValid(int dx, int dy) {
if (dx == 0 && dy == 0)
return false;
switch (getType()) {
case KING:
if (Math.abs(dx) == 1 && Math.abs(dy) == 1)
return true;
break;
case QUEEN:
if ((dx == dy) || (dx == 0 ^ dy == 0))
return true;
break;
case CASTLE:
if (dx == 0 ^ dy == 0)
return true;
break;
case BISHOP:
if (dx == dy)
return true;
break;
case KNIGHT:
if ((dy == 2 && (dx == -1 || dx == 1)) || (dy == -2 && (dx == -1 || dx == 1)) || (dx == 2 && (dy == -1 || dy == 1)) || (dx == -2 && (dy == -1 || dy == 1)))
return true;
break;
case PAWN:
if (dx == 0)
return false;
if (getTeam() == Team.WHITE && (dy == 1))
return true;
if (getTeam() == Team.BLACK && (dy == -1))
return true;
break;
default:
}
return false;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof ChessFigure)) { if (!(o instanceof ChessFigure)) {

65
src/test/java/Game/ChessObj/ChessFigureTest.java

@ -4,6 +4,8 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class ChessFigureTest { class ChessFigureTest {
@ -43,4 +45,67 @@ class ChessFigureTest {
assertNotEquals(kw2, kb1); assertNotEquals(kw2, kb1);
assertNotEquals(pw1, kb1); assertNotEquals(pw1, kb1);
} }
@Test
void isRelativeMoveValid() {
ArrayList<ChessFigure> array = new ArrayList<>();
for (ChessFigure.Type type : ChessFigure.Type.values())
for (ChessFigure.Team team : ChessFigure.Team.values())
array.add(new ChessFigure(type, team));
for (ChessFigure figure : array) {
for (int x = -4; x <= 4; x++) {
for (int y = -4; y <= 4; y++) {
if (x == 0 && y == 0) {
assertFalse(figure.isRelativeMoveValid(x, y));
continue;
}
switch (figure.getType()) {
case KING:
if (x * x == 1 && y * y == 1) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
case QUEEN:
if ((x == y) || (x == 0 ^ y == 0)) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
case CASTLE:
if (x == 0 ^ y == 0) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
case BISHOP:
if (x == y) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
case KNIGHT:
if ((y == 2 && (x == -1 || x == 1)) || (y == -2 && (x == -1 || x == 1)) || (x == 2 && (y == -1 || y == 1)) || (x == -2 && (y == -1 || y == 1))) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
case PAWN:
if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x != 0) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x != 0) {
assertTrue(figure.isRelativeMoveValid(x, y));
continue;
}
break;
default:
}
assertFalse(figure.isRelativeMoveValid(x, y), "Type: " + figure.getType() + " X: " + x + " Y: " + y + " should be false");
}
}
}
}
} }
Loading…
Cancel
Save