diff --git a/src/backend/Board.java b/src/backend/Board.java index 111bf54..da7f7d7 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -153,11 +153,15 @@ public class Board { } else { // we know we already have a piece so we move it to another position if( this.positionOccupied(x, y) == true) { - this.pieces.remove(whatPiece(x,y)); //if there is a piece at the position we want to move we remove it from the array list + if (pieces.get(whatPiece(x,y)).isWhite() != pieces.get(whatPiece(this.x,this.y)).isWhite()) {//check if we do not move to a position occupied by a piece of the same color + this.pieces.remove(whatPiece(x,y)); //if there is a piece at the position we want to move we remove it from the array list + } + } + if(this.positionOccupied(x, y) == false || pieces.get(whatPiece(x,y)).isWhite() != pieces.get(whatPiece(this.x,this.y)).isWhite()) {//moves the piece only if the position selected is not occupied by a piece of the same color + int indexPiece = this.whatPiece(this.x, this.y); + pieces.get(indexPiece).setX(x); + pieces.get(indexPiece).setY(y); } - int indexPiece = this.whatPiece(this.x, this.y); - pieces.get(indexPiece).setX(x); - pieces.get(indexPiece).setY(y); // we now reset our parameters this.x=-1; this.y=-1; @@ -350,11 +354,11 @@ public class Board { if((x == this.x) && (positionOccupied(x,y)==false)) {//advance one case highlight = true; } - if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y))) {//eat the piece in diagonal, if existing + if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y)) && pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) {//highlight the piece in diagonal, if existing highlight = true; } } - if((y == this.y-2) && (this.y == 6) && (x == this.x)) {//move by two if not moved + if((y == this.y-2) && (this.y == 6) && (x == this.x) && (positionOccupied(this.x,this.y-1))==false) {//move by two if not moved and if no piece in front highlight = true; } } @@ -364,27 +368,47 @@ public class Board { if((x == this.x) && (positionOccupied(x,y)==false)) { highlight = true; } - if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y))) { + if((Math.abs(x-this.x) == 1) && (positionOccupied(x,y)) && pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { highlight = true; } } - if((y == this.y+2) && (this.y == 1) && (x == this.x)) { + if((y == this.y+2) && (this.y == 1) && (x == this.x) && (positionOccupied(this.x,this.y+1) == false)) { highlight = true; } } } - if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings - if((Math.abs(x-this.x) <= 1) && (Math.abs(y-this.y) <= 1)) { //consider all cases at distance one from the king - if ((Math.abs(x-this.x) != 0) || (Math.abs(y-this.y) != 0)) { //check if we are not considering the place where the king is - highlight = true; + if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings THIS IS NOT OPTIMISED BC OF WHATPIECE (SAME FOR ALL FOLLOWING PIECES) + if(positionOccupied(x,y) == false) { + if((Math.abs(x-this.x) <= 1) && (Math.abs(y-this.y) <= 1)) { //consider all cases at distance one from the king + if ((Math.abs(x-this.x) != 0) || (Math.abs(y-this.y) != 0)) { //check if we are not considering the place where the king is + highlight = true; + } + } + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()){ + if((Math.abs(x-this.x) <= 1) && (Math.abs(y-this.y) <= 1)) { //consider all cases at distance one from the king + if ((Math.abs(x-this.x) != 0) || (Math.abs(y-this.y) != 0)) { //check if we are not considering the place where the king is + highlight = true; + } + } } } } if (pieces.get(indexPieceSelect).getType() == PieceType.Knight) { //highlight for knights - if (((Math.abs(x-this.x) == 1) && (Math.abs(y-this.y) == 2)) || ((Math.abs(x-this.x) == 2) && (Math.abs(y-this.y) == 1))) {//consider all positions at 2 by 1 from the knight - highlight = true; + if(positionOccupied(x,y) == false) { + if (((Math.abs(x-this.x) == 1) && (Math.abs(y-this.y) == 2)) || ((Math.abs(x-this.x) == 2) && (Math.abs(y-this.y) == 1))) {//consider all positions at 2 by 1 from the knight + highlight = true; + } + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()){ + if (((Math.abs(x-this.x) == 1) && (Math.abs(y-this.y) == 2)) || ((Math.abs(x-this.x) == 2) && (Math.abs(y-this.y) == 1))) {//consider all positions at 2 by 1 from the knight + highlight = true; + } + } } } @@ -392,7 +416,14 @@ public class Board { int numHighlight = highlightBishop().get(0).size(); for(int i = 0; i < numHighlight; i++) { if ((x == highlightBishop().get(0).get(i)) && (y == highlightBishop().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted - highlight = true; + if(positionOccupied(x,y) == false) { + highlight = true; + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + } + } } } } @@ -401,7 +432,14 @@ public class Board { int numHighlight = highlightQueen().get(0).size(); for(int i = 0; i < numHighlight; i++) { if ((x == highlightQueen().get(0).get(i)) && (y == highlightQueen().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted - highlight = true; + if(positionOccupied(x,y) == false) { + highlight = true; + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + } + } } } } @@ -410,7 +448,14 @@ public class Board { int numHighlight = highlightRook().get(0).size(); for(int i = 0; i < numHighlight; i++) { if ((x == highlightRook().get(0).get(i)) && (y == highlightRook().get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted - highlight = true; + if(positionOccupied(x,y) == false) { + highlight = true; + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + } + } } } }