Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ArrayList<Cell> possibleMoves(Cell givenCell) throws NullPointerException{
- if(givenCell == null){
- throw new NullPointerException("Given Cell is null. Cannot find the possible moves of null Cell");
- }
- ArrayList<Cell> nextMoves = new ArrayList<Cell>();
- Piece givenPiece = givenCell.getPiece();
- if(givenPiece == null){
- return nextMoves;
- }
- String playerColor = givenPiece.getColor();
- String opponentColor = Piece.getOpponentColor(playerColor);
- // if the piece is red-colored
- if(playerColor.equals(Piece.RED)){
- //the next move will be one row ahead i.e in row number X+1
- int nextX = givenCell.getX()+1;
- if(nextX < 8){
- //next move = (currentRow +1, currentColumn +1)
- int nextY = givenCell.getY()+1;
- //if the cell is not out of bound further checking is required
- if(nextY < 8){
- //if the cell is empty then add the cell to next move
- if(!this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX + 1;
- int yCoordAfterHoping = nextY + 1;
- if(xCoordAfterHoping < 8 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- }
- //next move = (currentRow+1, currentColumn -1)
- nextY = givenCell.getY() -1;
- // if the cell is within bound and does not contains a piece then add it to nextMoves
- if(nextY >=0){
- if(!this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX + 1;
- int yCoordAfterHoping = nextY - 1;
- if(xCoordAfterHoping < 8 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- }
- }
- //if the given piece is king then have to look to the row behind
- if(givenPiece.isKing()){
- nextX = givenCell.getX() -1;
- if(nextX >=0){
- //nextMove = (currentRow -1, currentColumn+1)
- //add this cell if it is within bound and doesnot contain piece
- int nextY = givenCell.getY()+1;
- if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX - 1;
- int yCoordAfterHoping = nextY + 1;
- if(xCoordAfterHoping >=0 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- //nextMove = (currentRow-1, currentColumn-1)
- //add this cell if it is within bound and does not contains piece
- nextY = givenCell.getY() -1;
- if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX - 1;
- int yCoordAfterHoping = nextY - 1;
- if(xCoordAfterHoping >=0 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- }
- }
- }
- //if the piece is yellow-colored
- else if(givenPiece.getColor().equals(Piece.YELLOW)){
- //yellow pieces are on the higher rows and to move it forward we have to move them to rows with lower row number.
- //So by assigning currentRow = currentRow -1, we are actually advancing the pieces
- //next move will be on the next row of current row. Rember that currentRow -= 1 will advance the row for yellow pieces
- int nextX = givenCell.getX()-1;
- if(nextX >= 0){
- //next move = (currentRow -1, currentColumn +1) which is a row ahead and a column to right
- int nextY = givenCell.getY()+1;
- if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX -1;
- int yCoordAfterHoping = nextY +1;
- if(xCoordAfterHoping >=0 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- //next move = (currentRow -1, currentColumn+1) which is a row ahead and a column to left
- nextY = givenCell.getY()-1;
- if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX -1;
- int yCoordAfterHoping = nextY - 1;
- if(xCoordAfterHoping >=0 && yCoordAfterHoping >=0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- }
- //if the piece is king we have to look back; Remember in Yellow pieces back row = currentRow +1
- if(givenPiece.isKing()){
- //getting to row behind current row
- nextX = givenCell.getX()+1;
- if(nextX < 8){
- //next move = (currentRow +1, currentColumn+1) which is a row behind and a column right
- int nextY = givenCell.getY()+1;
- if(nextY < 8 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY < 8 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX + 1;
- int yCoordAfterHoping = nextY + 1;
- if(xCoordAfterHoping < 8 && yCoordAfterHoping < 8 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- //next move = (currentRow +1, currentColumn-1) which is a row behind and a column left
- nextY = givenCell.getY() -1;
- if(nextY >=0 && !this.board[nextX][nextY].containsPiece()){
- nextMoves.add(this.board[nextX][nextY]);
- }
- else if(nextY >=0 && this.board[nextX][nextY].getPiece().getColor().equals(opponentColor)){
- int xCoordAfterHoping = nextX + 1;
- int yCoordAfterHoping = nextY - 1;
- if(xCoordAfterHoping < 8 && yCoordAfterHoping >= 0 && !this.board[xCoordAfterHoping][yCoordAfterHoping].containsPiece()){
- nextMoves.add(this.board[xCoordAfterHoping][yCoordAfterHoping]);
- }
- }
- }
- }
- }// end of else if Yellow piece
- return nextMoves;
- }// end of possibleMoves method
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement