You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
5.0 KiB
111 lines
5.0 KiB
package Game.ChessObj;
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
class ChessFigureTest {
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
}
|
|
|
|
@AfterEach
|
|
void tearDown() {
|
|
}
|
|
|
|
@Test
|
|
void getSymbol() {
|
|
assertEquals(" K ", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals(" Q ", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals(" I ", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals(" T ", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals(" Z ", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals(" o ", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE).getSymbol());
|
|
assertEquals("|K|", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK).getSymbol());
|
|
assertEquals("|Q|", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK).getSymbol());
|
|
assertEquals("|I|", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK).getSymbol());
|
|
assertEquals("|T|", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK).getSymbol());
|
|
assertEquals("|Z|", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK).getSymbol());
|
|
assertEquals("|o|", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK).getSymbol());
|
|
}
|
|
|
|
@Test
|
|
void testEquals() {
|
|
ChessFigure kw1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
|
|
ChessFigure kw2 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
|
|
ChessFigure kb1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK);
|
|
ChessFigure pw1 = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE);
|
|
|
|
assertEquals(kw1, kw2);
|
|
assertNotEquals(kw2, 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 (Math.abs(x) == Math.abs(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 (Math.abs(x) == Math.abs(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");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|