Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public class Queen extends ChessPiece {
  2.  
  3. /** Constructor for the Queen class */
  4. public Queen(int initialRow, int initialCol, int pieceColor) {
  5. this.row = initialRow;
  6. this.col = initialCol;
  7. this.color = pieceColor;
  8. }
  9.  
  10. /**
  11. * Method that returns a boolean indicating whether or not the queen can
  12. * legally move to the specified location (you need to fill this one in).
  13. */
  14. public boolean canMoveTo(int row, int col, ChessBoard board)
  15. {
  16. //return automagicQueenCanMoveTo(row, col, board);
  17.  
  18.  
  19. ChessPiece piece = board.pieceAt(row, col);
  20. ChessPiece oldPiece = board.pieceAt(this.row, this.col);
  21. int dx = (col-this.col);
  22. int dy = (row-this.row);
  23.  
  24. if (this.row==row && this.col==col){
  25. return false;
  26. }
  27. if (Math.abs(dx)==Math.abs(dy) || this.row==row || this.col==col){
  28. int i = 0;
  29. int j = 0;
  30. if (dx==0 && dy<0){
  31. i = 0;
  32. j = -1;
  33. }
  34. if (dx==0 && dy>0){
  35. i = 0;
  36. j = 1;
  37. }
  38. if (dx>0 && dy==0){
  39. i = 1;
  40. j = 0;
  41. }
  42. if (dx<0 && dy==0){
  43. i = -1;
  44. j = 0;
  45. }
  46. if (dx>0 && dy<0){
  47. i = 1;
  48. j = -1;
  49. }
  50. if (dx>0 && dy>0){
  51. i = 1;
  52. j = 1;
  53. }
  54. if (dx<0 && dy>0){
  55. i = -1;
  56. j = 1;
  57. }
  58. if (dx<0 && dy<0){
  59. i = -1;
  60. j = -1;
  61. }
  62. int curRow = this.row;
  63. int curCol = this.col;
  64. while (curRow != row && curCol != col){
  65. curRow = curRow + j;
  66. curCol = curCol + i;
  67. if(board.pieceAt(curRow, curCol) != null){
  68. println("a");
  69. return false;
  70. //break;
  71.  
  72. }
  73. }
  74. if (curRow == row && curCol == col){
  75. if(board.pieceAt(row, col) != null){
  76. if (piece.getColor() == oldPiece.getColor()){
  77. println("b");
  78. return false;
  79. }
  80. println("c");
  81. return true;
  82. }
  83. }
  84.  
  85. if(piece == null){
  86. return true;
  87. }
  88. if(piece.getColor() == this.color){
  89. return false;
  90. } else {
  91. if (moveWouldCauseCheck(row, col, board)){
  92. return false;
  93. } else {
  94. return true;
  95. }
  96. }
  97. } else {
  98. return false;
  99. }
  100.  
  101. }
  102.  
  103. private void println(String string) {
  104. // TODO Auto-generated method stub
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement