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.

110 lines
5.0 KiB

  1. package Game.ChessObj;
  2. import org.junit.jupiter.api.AfterEach;
  3. import org.junit.jupiter.api.BeforeEach;
  4. import org.junit.jupiter.api.Test;
  5. import java.util.ArrayList;
  6. import static org.junit.jupiter.api.Assertions.*;
  7. class ChessFigureTest {
  8. @BeforeEach
  9. void setUp() {
  10. }
  11. @AfterEach
  12. void tearDown() {
  13. }
  14. @Test
  15. void getSymbol() {
  16. assertEquals(" K ", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE).getSymbol());
  17. assertEquals(" Q ", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.WHITE).getSymbol());
  18. assertEquals(" I ", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.WHITE).getSymbol());
  19. assertEquals(" T ", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.WHITE).getSymbol());
  20. assertEquals(" Z ", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.WHITE).getSymbol());
  21. assertEquals(" o ", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE).getSymbol());
  22. assertEquals("|K|", new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK).getSymbol());
  23. assertEquals("|Q|", new ChessFigure(ChessFigure.Type.QUEEN, ChessFigure.Team.BLACK).getSymbol());
  24. assertEquals("|I|", new ChessFigure(ChessFigure.Type.BISHOP, ChessFigure.Team.BLACK).getSymbol());
  25. assertEquals("|T|", new ChessFigure(ChessFigure.Type.CASTLE, ChessFigure.Team.BLACK).getSymbol());
  26. assertEquals("|Z|", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK).getSymbol());
  27. assertEquals("|o|", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK).getSymbol());
  28. }
  29. @Test
  30. void testEquals() {
  31. ChessFigure kw1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
  32. ChessFigure kw2 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
  33. ChessFigure kb1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK);
  34. ChessFigure pw1 = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE);
  35. assertEquals(kw1, kw2);
  36. assertNotEquals(kw2, kb1);
  37. assertNotEquals(pw1, kb1);
  38. }
  39. @Test
  40. void isRelativeMoveValid() {
  41. ArrayList<ChessFigure> array = new ArrayList<>();
  42. for (ChessFigure.Type type : ChessFigure.Type.values())
  43. for (ChessFigure.Team team : ChessFigure.Team.values())
  44. array.add(new ChessFigure(type, team));
  45. for (ChessFigure figure : array) {
  46. for (int x = -4; x <= 4; x++) {
  47. for (int y = -4; y <= 4; y++) {
  48. if (x == 0 && y == 0) {
  49. assertFalse(figure.isRelativeMoveValid(x, y));
  50. continue;
  51. }
  52. switch (figure.getType()) {
  53. case KING:
  54. if (x * x == 1 && y * y == 1) {
  55. assertTrue(figure.isRelativeMoveValid(x, y));
  56. continue;
  57. }
  58. break;
  59. case QUEEN:
  60. if ((x == y) || (x == 0 ^ y == 0)) {
  61. assertTrue(figure.isRelativeMoveValid(x, y));
  62. continue;
  63. }
  64. break;
  65. case CASTLE:
  66. if (x == 0 ^ y == 0) {
  67. assertTrue(figure.isRelativeMoveValid(x, y));
  68. continue;
  69. }
  70. break;
  71. case BISHOP:
  72. if (x == y) {
  73. assertTrue(figure.isRelativeMoveValid(x, y));
  74. continue;
  75. }
  76. break;
  77. case KNIGHT:
  78. 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))) {
  79. assertTrue(figure.isRelativeMoveValid(x, y));
  80. continue;
  81. }
  82. break;
  83. case PAWN:
  84. if (figure.getTeam() == ChessFigure.Team.WHITE && (y == 1) && x == 0) {
  85. assertTrue(figure.isRelativeMoveValid(x, y));
  86. continue;
  87. }
  88. if (figure.getTeam() == ChessFigure.Team.BLACK && (y == -1) && x == 0) {
  89. assertTrue(figure.isRelativeMoveValid(x, y));
  90. continue;
  91. }
  92. break;
  93. default:
  94. }
  95. assertFalse(figure.isRelativeMoveValid(x, y), "Type: " + figure.getType() + " X: " + x + " Y: " + y + " should be false");
  96. }
  97. }
  98. }
  99. }
  100. }