Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. package access;
  2.  
  3. import representation.Board;
  4. import representation.BoardSquare;
  5.  
  6. import java.awt.*;
  7.  
  8. public class MoveValidator {
  9. /**
  10. * Iterate over every possible move from @param from and check whether one of the
  11. * positions is equal to @param to.
  12. *
  13. * Note: does not handle the validity if the move would result in a check.
  14. */
  15. public boolean isValidMove(Board board, BoardSquare from, BoardSquare to) {
  16. if (from.getPiece() == null) {
  17. return false;
  18. }
  19.  
  20. switch (from.getPiece().type) {
  21. case KING:
  22. // TODO >needs to be checked
  23. return isValidKingMove(board, from, to);
  24. case QUEEN:
  25. // TODO >needs to be checked
  26. return isValidQueenMove(board, from, to);
  27. case ROOK:
  28. // TODO >needs to be checked
  29. return isValidRookMove(board, from, to);
  30. case BISHOP:
  31. // TODO >needs to be checked
  32. return isValidBishopMove(board, from, to);
  33. case KNIGHT:
  34. // TODO >needs to be checked
  35. return isValidKnightMove(board, from, to);
  36. case PAWN:
  37. // TODO >needs to be checked
  38. return isValidPawnMove(board, from, to);
  39. default:
  40. System.err.println("Error: invalid piece type");
  41. return false;
  42. }
  43. }
  44.  
  45. private boolean isValidKingMove(Board board, BoardSquare from, BoardSquare to) {
  46. boolean emptyLeft = from.posY > 0;
  47. boolean emptyRight = from.posY < 7;
  48. boolean emptyUp = from.posX > 0;
  49. boolean emptyDown = from.posX < 7;
  50.  
  51. if (emptyLeft && isDestination(board, to, from.posX, from.posY - 1)) {
  52. return emptySquare(board, from.posX, from.posY - 1)
  53. || enemyOnSquare(board, from.posX, from.posY - 1, from.getPiece().color);
  54. }
  55.  
  56. }
  57.  
  58. /**
  59. * Does not take into account en passant capture!
  60. */
  61. private boolean isValidPawnMove(Board board, BoardSquare from, BoardSquare to) {
  62. Color color = from.getPiece().color;
  63. return color == Color.WHITE ? isValidWhitePawnMove(board, from, to) : isValidBlackPawnMove(board, from, to);
  64. }
  65.  
  66. private boolean isValidBlackPawnMove(Board board, BoardSquare from, BoardSquare to) {
  67. if (isDestination(board, to, from.posX - 1, from.posY + 1)) {
  68. return enemyOnSquare(board, from.posX - 1, from.posY - 1, from.getPiece().color);
  69. }
  70.  
  71. if (isDestination(board, to, from.posX - 1, from.posY - 1)) {
  72. return enemyOnSquare(board, from.posX + 1, from.posY - 1, from.getPiece().color);
  73. }
  74.  
  75. if (from.posX == 6) {
  76. // valid move one and two downwards
  77. if (isDestination(board, to, from.posX - 1, from.posY)) {
  78. return emptySquare(board, from.posX - 1, from.posY);
  79. }
  80.  
  81. if (isDestination(board, to, from.posX - 2, from.posY)) {
  82. return emptySquare(board, from.posX - 2 , from.posY);
  83. }
  84. }
  85.  
  86. return false;
  87. }
  88.  
  89. private boolean isValidWhitePawnMove(Board board, BoardSquare from, BoardSquare to) {
  90. if (isDestination(board, to, from.posX + 1, from.posY + 1)) {
  91. return enemyOnSquare(board, from.posX + 1, from.posY + 1, from.getPiece().color);
  92. }
  93.  
  94. if (isDestination(board, to, from.posX + 1, from.posY - 1)) {
  95. return enemyOnSquare(board, from.posX + 1, from.posY - 1, from.getPiece().color);
  96. }
  97.  
  98. if (from.posX == 1) {
  99. // valid move one and two downwards
  100. if (isDestination(board, to, from.posX + 1, from.posY)) {
  101. return emptySquare(board, from.posX + 1, from.posY);
  102. }
  103.  
  104. if (isDestination(board, to, from.posX + 2, from.posY)) {
  105. return emptySquare(board, from.posX + 2, from.posY);
  106. }
  107. }
  108.  
  109. return false;
  110. }
  111.  
  112. private boolean isValidBishopMove(Board board, BoardSquare from, BoardSquare to) {
  113. if (isDestinationDiagonallyLeftUpwards(board, from, to)) {
  114. return true;
  115. }
  116. if (isDestinationDiagonallyLeftDownwards(board, from, to)) {
  117. return true;
  118. }
  119. if (isDestinationDiagonallyRightDownwards(board, from, to)) {
  120. return true;
  121. }
  122. // if the candidate was not found among possible moves, the desired move must be invalid
  123. return isDestinationDiagonallyRightUpwards(board, from, to);
  124. }
  125.  
  126. private boolean isValidRookMove(Board board, BoardSquare from, BoardSquare to) {
  127. if (isDestinationLeft(board, from, to)) {
  128. return true;
  129. }
  130. if (isDestinationRight(board, from, to)) {
  131. return true;
  132. }
  133. if (isDestinationDownwards(board, from, to)) {
  134. return true;
  135. }
  136. // if the candidate was not found among possible moves, the desired move must be invalid
  137. return isDestinationUpwards(board, from, to);
  138. }
  139.  
  140. private boolean isValidQueenMove(Board board, BoardSquare from, BoardSquare to) {
  141.  
  142. if(isValidRookMove(board, from, to) || isValidBishopMove(board, from, to)) {
  143. return true;
  144. }
  145. // if the candidate was not found among possible moves, the desired move must be invalid
  146. return false;
  147. }
  148.  
  149. private boolean isValidKnightMove(Board board, BoardSquare from, BoardSquare to) {
  150. Color color = from.getPiece().color;
  151. int x = from.posX;
  152. int y = from.posY;
  153.  
  154. //all possible combinations of knight's moves
  155. int xChange[] = {-1, 1, -1, 1, 2, 2, -2, -2};
  156. int yChange[] = {2, 2, -2, -2, -1, 1, -1, 1};
  157.  
  158. for(int i = 0; i < 8; i++){
  159. int newX = x + xChange[i];
  160. int newY = y + yChange[i];
  161.  
  162. if(newX >= 0 && newX <= 7 && newY >= 0 && newY <= 7) {
  163. if (isDestination(board, to, newX, newY)) {
  164. return emptySquare(board, newX, newY) || enemyOnSquare(board, newX, newY, color);
  165. }
  166. }
  167. }
  168.  
  169. return false;
  170. }
  171.  
  172. private boolean isDestinationLeft(Board board, BoardSquare from, BoardSquare to){
  173. Color color = from.getPiece().color;
  174. int x = from.posX;
  175. int y = from.posY - 1;
  176.  
  177. for (; y >= 0 && emptySquare(board, x, y); --y) {
  178. if (isDestination(board, to, x, y)) {
  179. return true;
  180. }
  181. }
  182.  
  183. return y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  184. }
  185.  
  186. private boolean isDestinationRight(Board board, BoardSquare from, BoardSquare to){
  187. Color color = from.getPiece().color;
  188. int x = from.posX;
  189. int y = from.posY + 1;
  190.  
  191. for (; y <= 7 && emptySquare(board, x, y); ++y) {
  192. if (isDestination(board, to, x, y)) {
  193. return true;
  194. }
  195. }
  196.  
  197. return y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  198. }
  199.  
  200. private boolean isDestinationDownwards(Board board, BoardSquare from, BoardSquare to){
  201. Color color = from.getPiece().color;
  202. int x = from.posX + 1;
  203. int y = from.posY;
  204.  
  205. for (; x >= 0 && emptySquare(board, x, y); ++x) {
  206. if (isDestination(board, to, x, y)) {
  207. return true;
  208. }
  209. }
  210.  
  211. return x >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  212. }
  213.  
  214. private boolean isDestinationUpwards(Board board, BoardSquare from, BoardSquare to){
  215. Color color = from.getPiece().color;
  216. int x = from.posX - 1;
  217. int y = from.posY;
  218.  
  219. for (; x >= 0 && emptySquare(board, x, y); --x) {
  220. if (isDestination(board, to, x, y)) {
  221. return true;
  222. }
  223. }
  224.  
  225. return x >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  226. }
  227.  
  228. private boolean isDestinationDiagonallyRightUpwards(Board board, BoardSquare from, BoardSquare to) {
  229. Color color = from.getPiece().color;
  230. int x = from.posX - 1;
  231. int y = from.posY + 1;
  232. for (; x >= 0 && y <= 7 && emptySquare(board, x, y); --x, ++y) {
  233. if (isDestination(board, to, x, y)) {
  234. return true;
  235. }
  236. }
  237.  
  238. return x >= 0 && y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  239. }
  240.  
  241. private boolean isDestinationDiagonallyRightDownwards(Board board, BoardSquare from, BoardSquare to) {
  242. Color color = from.getPiece().color;
  243. int x = from.posX + 1;
  244. int y = from.posY + 1;
  245. for (; x <= 7 && y <= 7 && emptySquare(board, x, y); ++x, ++y) {
  246. if (isDestination(board, to, x, y)) {
  247. return true;
  248. }
  249. }
  250.  
  251. return x <= 7 && y <= 7 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  252. }
  253.  
  254. private boolean isDestinationDiagonallyLeftDownwards(Board board, BoardSquare from, BoardSquare to) {
  255. Color color = from.getPiece().color;
  256. int x = from.posX + 1;
  257. int y = from.posY - 1;
  258. for (; x <= 7 && y >= 0 && emptySquare(board, x, y); ++x, --y) {
  259. if (isDestination(board, to, x, y)) {
  260. return true;
  261. }
  262. }
  263.  
  264. return x <= 7 && y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  265. }
  266.  
  267. private boolean isDestinationDiagonallyLeftUpwards(Board board, BoardSquare from, BoardSquare to) {
  268. Color color = from.getPiece().color;
  269. int x = from.posX - 1;
  270. int y = from.posY - 1;
  271. for (; x >= 0 && y >= 0 && emptySquare(board, x, y); --x, --y) {
  272. if (isDestination(board, to, x, y)) {
  273. return true;
  274. }
  275. }
  276.  
  277. return x >= 0 && y >= 0 && isDestination(board, to, x, y) && enemyOnSquare(board, x, y, color);
  278. }
  279.  
  280. private boolean isDestination(Board board, BoardSquare to, int x, int y) {
  281. return board.getPieces()[x][y] == to;
  282. }
  283.  
  284. private boolean emptySquare(Board board, int posX, int posY) {
  285. return board.getPieces()[posX][posY].isEmpty();
  286. }
  287.  
  288. private boolean enemyOnSquare(Board board, int posX, int posY, Color thisPieceColor) {
  289. return board.getPieces()[posX][posY].getPiece().color != thisPieceColor;
  290. }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement