Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. public boolean isMoveValid(int fromRow, int fromColumn,
  2.                                int toRow, int toColumn) {
  3.  
  4.         //out of bounds
  5.         if (fromRow < 0 || fromRow > 7 || fromColumn < 0 || fromColumn > 7 ||
  6.                 toRow < 0 || toRow > 7 || toColumn < 0 || toColumn > 7)
  7.             return false;
  8.  
  9.         //if piece you want to move is not there or you are grabbing and moving to the same color piece
  10.         if (getPieceAt(fromRow, fromColumn) == PieceType.NONE ||
  11.                 getPieceAt(fromRow, fromColumn) == getPieceAt(toRow, toColumn))
  12.             return false;
  13.         if (getPlayerInTurn() == PlayerType.WHITE) {
  14.             if ((fromRow - 1 == toRow && fromColumn + 0 == toColumn) ||
  15.                     (fromRow - 1 == toRow && fromColumn - 1 == toColumn) ||
  16.                     (fromRow - 1 == toRow && fromColumn + 1 == toColumn) ||
  17.                     (fromRow + 1 != toRow)) {
  18.                 if (getPieceAt(toRow, toColumn) == PieceType.BLACK ||
  19.                         getPieceAt(toRow, toColumn) == PieceType.NONE)
  20.                     return true;
  21.                 else return false;
  22.             } else return false;
  23.  
  24.         } else if (getPlayerInTurn() == PlayerType.BLACK) {
  25.             if ((fromRow + 1 == toRow && fromColumn + 0 == toColumn) ||
  26.                     (fromRow + 1 == toRow && fromColumn + 1 == toColumn) ||
  27.                     (fromRow + 1 == toRow && fromColumn - 1 == toColumn)) {
  28.                 if (getPieceAt(toRow, toColumn) == PieceType.WHITE ||
  29.                         getPieceAt(toRow, toColumn) == PieceType.NONE)
  30.                     return true;
  31.                 else return false;
  32.             } else return false;
  33.         }
  34.  
  35.         return false;
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement