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.

115 lines
2.8 KiB

  1. package Game.ChessObj;
  2. public class ChessFigure {
  3. public enum Type {
  4. KING,
  5. QUEEN,
  6. CASTLE,
  7. BISHOP,
  8. KNIGHT,
  9. PAWN
  10. }
  11. public enum Team {
  12. WHITE,
  13. BLACK
  14. }
  15. private final Type type;
  16. private final Team team;
  17. public ChessFigure(Type type, Team team) {
  18. this.type = type;
  19. this.team = team;
  20. }
  21. public Type getType() {
  22. return this.type;
  23. }
  24. public Team getTeam() {
  25. return this.team;
  26. }
  27. public String getSymbol() {
  28. String symbol = "";
  29. switch (getType()) {
  30. case KING:
  31. symbol = "K";
  32. break;
  33. case QUEEN:
  34. symbol = "Q";
  35. break;
  36. case CASTLE:
  37. symbol = "T";
  38. break;
  39. case BISHOP:
  40. symbol = "I";
  41. break;
  42. case KNIGHT:
  43. symbol = "Z";
  44. break;
  45. case PAWN:
  46. symbol = "o";
  47. break;
  48. default:
  49. }
  50. symbol = ((this.getTeam() == Team.WHITE) ? " " : "|") + symbol + ((this.getTeam() == Team.WHITE) ? " " : "|");
  51. return symbol;
  52. }
  53. public boolean isRelativeMoveValid(int dx, int dy) {
  54. if (dx == 0 && dy == 0)
  55. return false;
  56. switch (getType()) {
  57. case KING:
  58. if (Math.abs(dx) == 1 && Math.abs(dy) == 1)
  59. return true;
  60. break;
  61. case QUEEN:
  62. if ((Math.abs(dx) == Math.abs(dy)) || (dx == 0 ^ dy == 0))
  63. return true;
  64. break;
  65. case CASTLE:
  66. if (dx == 0 ^ dy == 0)
  67. return true;
  68. break;
  69. case BISHOP:
  70. if (Math.abs(dx) == Math.abs(dy))
  71. return true;
  72. break;
  73. case KNIGHT:
  74. 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)))
  75. return true;
  76. break;
  77. case PAWN:
  78. if (dx != 0)
  79. return false;
  80. if (getTeam() == Team.WHITE && (dy == 1))
  81. return true;
  82. if (getTeam() == Team.BLACK && (dy == -1))
  83. return true;
  84. break;
  85. default:
  86. }
  87. return false;
  88. }
  89. @Override
  90. public boolean equals(Object o) {
  91. if (!(o instanceof ChessFigure)) {
  92. return false;
  93. }
  94. ChessFigure x = (ChessFigure) o;
  95. if (this.getType() != x.getType() || this.getTeam() != x.getTeam())
  96. return false;
  97. return true;
  98. }
  99. }