Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. /**
  2.      * Method to see if space puts a piece in jeopardy
  3.      * Implementing this as an isolated method is more extensible
  4.      * than implementing the same logic in whiteCheck or blackCheck
  5.      * because we can use it to see if this is a valid move for the king
  6.      * @input the space to check and the player who is moving
  7.      * @return True if the space is safe, False if otherwise
  8.      *
  9.      * @note this method is super ineffecient. I should find a way to break it up!
  10.      */
  11.     @SuppressWarnings("Duplicates")
  12.     public boolean spaceIsSafe(int x, int y, Player player){
  13.         Board board = Board.getInstance();
  14.  
  15.         //check for rooks and queens
  16.         //check up
  17.         for(int i = (x - 1); i >= 0; i--){
  18.             Piece piece = board.board[i][y];
  19.             if(piece.getPlayer() != player){
  20.                 if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
  21.                     return false;
  22.                 }
  23.             }
  24.             else{
  25.                 break;
  26.             }
  27.         }
  28.         //check down
  29.         for(int i = (x + 1); i <= 7; i++){
  30.             Piece piece = board.board[i][y];
  31.             if(piece.getPlayer() != player){
  32.                 if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
  33.                     return false;
  34.                 }
  35.             }
  36.             else{
  37.                 break;
  38.             }
  39.         }
  40.         //check right
  41.         for(int i = (y + 1); i <= 7; i++){
  42.             Piece piece = board.board[x][i];
  43.             if(piece.getPlayer() != player){
  44.                 if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
  45.                     return false;
  46.                 }
  47.             }
  48.             else{
  49.                 break;
  50.             }
  51.         }
  52.         //check left
  53.         for(int i = (y - 1); i >= 0; i++){
  54.             Piece piece = board.board[x][i];
  55.             if(piece.getPlayer() != player){
  56.                 if(piece.getType() == Type.ROOK || piece.getType() == Type.QUEEN){
  57.                     return false;
  58.                 }
  59.             }
  60.             else{
  61.                 break;
  62.             }
  63.         }
  64.  
  65.         //check for bishops and queens
  66.         //check up-left
  67.         for(int i = 1; x - i >= 0 && y - i >= 0; i ++){
  68.             Piece piece = board.board[x-i][y-i];
  69.             if(piece.getPlayer() != player){
  70.                 if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
  71.                     return false;
  72.                 }
  73.             }
  74.             else{
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         //check up-right
  80.         for(int i = 1; x - i >= 0 && y + i <= 7; i ++){
  81.             Piece piece = board.board[x-i][y+i];
  82.             if(piece.getPlayer() != player){
  83.                 if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
  84.                     return false;
  85.                 }
  86.             }
  87.             else{
  88.                 break;
  89.             }
  90.         }
  91.  
  92.         //check down-left
  93.         for(int i = 1; x + i <= 7 && y - i >= 0; i ++){
  94.             Piece piece = board.board[x+i][y-i];
  95.             if(piece.getPlayer() != player){
  96.                 if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
  97.                     return false;
  98.                 }
  99.             }
  100.             else{
  101.                 break;
  102.             }
  103.         }
  104.  
  105.         //check down-right
  106.         for(int i = 1; x + i <= 7 && y + i <= 7; i ++){
  107.             Piece piece = board.board[x+i][y+i];
  108.             if(piece.getPlayer() != player){
  109.                 if(piece.getType() == Type.BISHOP || piece.getType() == Type.QUEEN){
  110.                     return false;
  111.                 }
  112.             }
  113.             else{
  114.                 break;
  115.             }
  116.         }
  117.         //check for pawns
  118.         if (player.color == Color.WHITE){
  119.             if (x > 0){
  120.                 if(y > 0) {
  121.                     if (board.board[x - 1][y-1].getPlayer() != player &&
  122.                             board.board[x - 1][y-1].getType() == Type.PAWN) {
  123.                         return false;
  124.                     }
  125.                 }
  126.                 if(y < 7){
  127.                     if (board.board[x - 1][y+1].getPlayer() != player &&
  128.                             board.board[x - 1][y+1].getType() == Type.PAWN) {
  129.                         return false;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         else{
  135.             if (x < 7){
  136.                 if(y > 0) {
  137.                     if (board.board[x + 1][y-1].getPlayer() != player &&
  138.                             board.board[x + 1][y-1].getType() == Type.PAWN) {
  139.                         return false;
  140.                     }
  141.                 }
  142.                 if(y < 7){
  143.                     if (board.board[x + 1][y+1].getPlayer() != player &&
  144.                             board.board[x + 1][y+1].getType() == Type.PAWN) {
  145.                         return false;
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         //check for kings
  151.         int[][] directions = new int[][]{{-1,-1}, {-1,0}, {-1,1},  {0,1}, {1,1},  {1,0},  {1,-1},  {0, -1}};
  152.         for(int i = 0; i < directions.length; i++){
  153.             int tempX = x + directions[i][0];
  154.             int tempY = y + directions[i][1];
  155.             if (tempX < 0 ||
  156.                 tempY < 0 ||
  157.                 tempX > 7 ||
  158.                 tempY > 7){
  159.                 continue;
  160.             }
  161.             else{
  162.                 if (board.board[tempX][tempY].getPlayer() != player &&
  163.                     board.board[tempX][tempY].getType() == Type.KING){
  164.                     return false;
  165.                 }
  166.             }
  167.         }
  168.  
  169.         //check for knights
  170.         directions = new int[][]{{-2,-1}, {-2,1}, {2,-1},  {2,1}, {1,-2},  {1,2},  {-1,-2},  {-1, 2}};
  171.         for(int i = 0; i < directions.length; i++){
  172.             int tempX = x + directions[i][0];
  173.             int tempY = y + directions[i][1];
  174.             if (tempX < 0 ||
  175.                     tempY < 0 ||
  176.                     tempX > 7 ||
  177.                     tempY > 7){
  178.                 continue;
  179.             }
  180.             else{
  181.                 if (board.board[tempX][tempY].getPlayer() != player &&
  182.                         board.board[tempX][tempY].getType() == Type.KNIGHT){
  183.                     return false;
  184.                 }
  185.             }
  186.         }
  187.         return true;
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement