Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Queen extends ChessPiece {
- /** Constructor for the Queen class */
- public Queen(int initialRow, int initialCol, int pieceColor) {
- this.row = initialRow;
- this.col = initialCol;
- this.color = pieceColor;
- }
- /**
- * Method that returns a boolean indicating whether or not the queen can
- * legally move to the specified location (you need to fill this one in).
- */
- public boolean canMoveTo(int row, int col, ChessBoard board)
- {
- //return automagicQueenCanMoveTo(row, col, board);
- ChessPiece piece = board.pieceAt(row, col);
- ChessPiece oldPiece = board.pieceAt(this.row, this.col);
- int dx = (col-this.col);
- int dy = (row-this.row);
- if (this.row==row && this.col==col){
- return false;
- }
- if (Math.abs(dx)==Math.abs(dy) || this.row==row || this.col==col){
- int i = 0;
- int j = 0;
- if (dx==0 && dy<0){
- i = 0;
- j = -1;
- }
- if (dx==0 && dy>0){
- i = 0;
- j = 1;
- }
- if (dx>0 && dy==0){
- i = 1;
- j = 0;
- }
- if (dx<0 && dy==0){
- i = -1;
- j = 0;
- }
- if (dx>0 && dy<0){
- i = 1;
- j = -1;
- }
- if (dx>0 && dy>0){
- i = 1;
- j = 1;
- }
- if (dx<0 && dy>0){
- i = -1;
- j = 1;
- }
- if (dx<0 && dy<0){
- i = -1;
- j = -1;
- }
- int curRow = this.row;
- int curCol = this.col;
- while (curRow != row && curCol != col){
- curRow = curRow + j;
- curCol = curCol + i;
- if(board.pieceAt(curRow, curCol) != null){
- println("a");
- return false;
- //break;
- }
- }
- if (curRow == row && curCol == col){
- if(board.pieceAt(row, col) != null){
- if (piece.getColor() == oldPiece.getColor()){
- println("b");
- return false;
- }
- println("c");
- return true;
- }
- }
- if(piece == null){
- return true;
- }
- if(piece.getColor() == this.color){
- return false;
- } else {
- if (moveWouldCauseCheck(row, col, board)){
- return false;
- } else {
- return true;
- }
- }
- } else {
- return false;
- }
- }
- private void println(String string) {
- // TODO Auto-generated method stub
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement