Advertisement
Guest User

MDP 09 rules

a guest
Mar 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.86 KB | None | 0 0
  1.  public ArrayList<Cell> possibleMoves(Cell givenCell) throws NullPointerException{
  2.  
  3.         if(givenCell == null){
  4.             throw new NullPointerException("Given Cell is null. Cannot find the possible moves of null Cell");
  5.         }
  6.  
  7.         ArrayList<Cell> nextMoves = new ArrayList<Cell>();
  8.         Piece givenPiece = givenCell.getPiece();
  9.  
  10.         if(givenPiece == null){
  11.             return nextMoves;
  12.         }
  13.  
  14.         String playerColor = givenPiece.getColor();
  15.         String opponentColor = Piece.getOpponentColor(playerColor);
  16.  
  17.  
  18.         // if the piece is red-colored
  19.         if(playerColor.equals(Piece.RED)){
  20.             //the next move will be one row ahead i.e in row number X+1
  21.             int nextX = givenCell.getX()+1;
  22.  
  23.             if(nextX < 8){
  24.                 //next move = (currentRow +1, currentColumn +1)
  25.                 int nextY = givenCell.getY()+1;
  26.                 //if the cell is not out of bound further checking is required
  27.                 if(nextY < 8){
  28.                     //if the cell is empty then add the cell to next move
  29.                     if(!this.board[nextX][nextY].containsPiece()){
  30.                         nextMoves.add(this.board[nextX][nextY]);
  31.                     }
  32.  
  33.                     else if(this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  34.                         int xCoordAfterHoping = nextX + 1;
  35.                         int yCoordAfterHoping = nextY + 1;
  36.                         if(xCoordAfterHoping < 8 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  37.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  38.                         }
  39.                     }
  40.                 }
  41.  
  42.  
  43.                 //next move = (currentRow+1, currentColumn -1)
  44.                 nextY = givenCell.getY() -1;
  45.                 // if the cell is within bound and does not contains a piece then add it to nextMoves
  46.                 if(nextY >=0){
  47.                     if(!this.board[nextX][nextY].containsPiece()){
  48.                         nextMoves.add(this.board[nextX][nextY]);
  49.                     }
  50.  
  51.                     else if(this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  52.                         int xCoordAfterHoping = nextX + 1;
  53.                         int yCoordAfterHoping = nextY - 1;
  54.                         if(xCoordAfterHoping < 8 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  55.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             //if the given piece is king then have to look to the row behind
  62.             if(givenPiece.isKing()){
  63.                 nextX = givenCell.getX() -1;
  64.                 if(nextX >=0){
  65.                     //nextMove = (currentRow -1, currentColumn+1)
  66.                     //add this cell if it is within bound and doesnot contain piece
  67.                     int nextY = givenCell.getY()+1;
  68.                     if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
  69.                         nextMoves.add(this.board[nextX][nextY]);
  70.                     }
  71.  
  72.                     else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  73.                         int xCoordAfterHoping = nextX - 1;
  74.                         int yCoordAfterHoping = nextY + 1;
  75.                         if(xCoordAfterHoping >=0 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  76.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  77.                         }
  78.                     }
  79.                     //nextMove = (currentRow-1, currentColumn-1)
  80.                     //add this cell if it is within bound and does not contains piece
  81.                     nextY = givenCell.getY() -1;
  82.                     if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
  83.                         nextMoves.add(this.board[nextX][nextY]);
  84.                     }
  85.  
  86.                     else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  87.                         int xCoordAfterHoping = nextX - 1;
  88.                         int yCoordAfterHoping = nextY - 1;
  89.                         if(xCoordAfterHoping >=0 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  90.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.  
  97.         //if the piece is yellow-colored
  98.         else if(givenPiece.getColor().equals(Piece.YELLOW)){
  99.             //yellow pieces are on the higher rows and to move it forward we have to move them to rows with lower row number.
  100.             //So by assigning currentRow = currentRow -1, we are actually advancing the pieces
  101.  
  102.             //next move will be on the next row of current row. Rember that currentRow -= 1 will advance the row for yellow pieces
  103.             int nextX = givenCell.getX()-1;
  104.             if(nextX >= 0){
  105.                 //next move = (currentRow -1, currentColumn +1) which is a row ahead and a column to right
  106.                 int nextY = givenCell.getY()+1;
  107.                 if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
  108.                     nextMoves.add(this.board[nextX][nextY]);
  109.                 }
  110.  
  111.                 else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  112.                     int xCoordAfterHoping = nextX -1;
  113.                     int yCoordAfterHoping = nextY +1;
  114.                     if(xCoordAfterHoping >=0 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  115.                         nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  116.                     }
  117.                 }
  118.                 //next move = (currentRow -1, currentColumn+1) which is a row ahead and a column to left
  119.                 nextY = givenCell.getY()-1;
  120.                 if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
  121.                     nextMoves.add(this.board[nextX][nextY]);
  122.                 }
  123.                 else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  124.                     int xCoordAfterHoping = nextX -1;
  125.                     int yCoordAfterHoping = nextY - 1;
  126.                     if(xCoordAfterHoping >=0 && yCoordAfterHoping >=0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  127.                         nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  128.                     }
  129.                 }
  130.             }
  131.  
  132.             //if the piece is king we have to look back; Remember in Yellow pieces back row = currentRow +1
  133.             if(givenPiece.isKing()){
  134.                 //getting to row behind current row
  135.                 nextX = givenCell.getX()+1;
  136.                 if(nextX < 8){
  137.                     //next move = (currentRow +1, currentColumn+1) which is a row behind and a column right
  138.                     int nextY = givenCell.getY()+1;
  139.                     if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
  140.                         nextMoves.add(this.board[nextX][nextY]);
  141.                     }
  142.  
  143.                     else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  144.                         int xCoordAfterHoping = nextX + 1;
  145.                         int yCoordAfterHoping = nextY +  1;
  146.                         if(xCoordAfterHoping < 8 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  147.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  148.                         }
  149.                     }
  150.  
  151.                     //next move = (currentRow +1, currentColumn-1) which is a row behind and a column left
  152.                     nextY = givenCell.getY() -1;
  153.                     if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
  154.                         nextMoves.add(this.board[nextX][nextY]);
  155.                     }
  156.                     else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
  157.                         int xCoordAfterHoping = nextX + 1;
  158.                         int yCoordAfterHoping = nextY -  1;
  159.                         if(xCoordAfterHoping < 8 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
  160.                             nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
  161.                         }
  162.                     }
  163.                 }
  164.             }
  165.         }// end of else if Yellow piece
  166.  
  167.         return nextMoves;
  168.     }// end of possibleMoves method
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement