diff --git a/composition echec/test1 b/composition echec/test1 index 913968d..fb30642 100644 --- a/composition echec/test1 +++ b/composition echec/test1 @@ -1,10 +1,11 @@ -BR,BN,BB,BQ,BK,BB,BN,BR, -BP,BP,BP,BP,BP, ,BP,BP, - , , , , , , , , - , , , ,WP,BP, , , - , , , , , , , , - , , , , , , , , -WP,WP,WP,WP, ,WP,WP,WP, -WR,WN,WB,WQ,WK,WB,WN,WR, -3 -1531 +BR, ,BB,BQ,BK,BB,BN,BR, +BP,BP,BP, , , , ,BP, +BN, , , , , , , , + , , ,BP,BP,BP,BP, , + , , , , ,WP,WP, , + , , , , ,WN, ,WB, +WP,WP,WP,WP,WP, , ,WP, +WR,WN,WB,WQ,WK, , ,WR, +9 +1630 +00000010 diff --git a/src/backend/Board.java b/src/backend/Board.java index 82f497c..2694fdb 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -11,6 +11,7 @@ public class Board { private int x; private int y; private int turnNumber; + private ArrayList board; private boolean lastTurnPawnTwo;//for en passant private int xTwo; @@ -25,9 +26,9 @@ public class Board { private boolean castling; private boolean castlingDone; private ArrayList previousBoard; - private ArrayList altPieces; + //private ArrayList altPieces; - private boolean isChecked; + //private boolean isChecked; public Board(int colNum, int lineNum) { this.kingWMoved = false; @@ -48,7 +49,7 @@ public class Board { lastTurnPawnTwo = false; previousBoard = new ArrayList<>(); } -//test + public int getWidth() { return colNum; //The width is represented by the columns } @@ -247,7 +248,7 @@ public class Board { public void userTouch(int x, int y) { boolean positionOccupied = this.positionOccupied(x, y) ; // verify if the square is occupied - // + if ((this.x == -1 ) && (positionOccupied == true)){ // set coordinates of the piece to play this.x = x; this.y = y; @@ -538,17 +539,17 @@ public class Board { /* The following methods require more work ! */ // COUCOU LES COPAINS SI VOUS ARRIVEZ A OPTIMISER LA SUITE CA SERAIT COOL - public ArrayList> highlightBishop(){ //creates a table of the x and y coordinates to highlight for the bishop + public ArrayList> highlightBishop(int xFrom, int yFrom, ArrayList pieces){ //creates a table of the x and y coordinates to highlight for the bishop boolean spotOccupied = false; ArrayList> toHighlight = new ArrayList<> (); toHighlight.add(new ArrayList<>()); // ArrayList that will contain the x coordinates toHighlight.add(new ArrayList<>()); // ArrayList that will contain the y coordinates int i = 0; while (spotOccupied == false) { //left top diagonal - if ((x-i-1 >= 0) && (y-i-1 >= 0)) { //check if we are still in the board - toHighlight.get(0).add(this.x-i-1); - toHighlight.get(1).add(this.y-i-1); - if (positionOccupied(this.x-i-1,this.y-i-1)) { //if we reach a position already occupied, stops highlighting the diagonal + if ((xFrom-i-1 >= 0) && (yFrom-i-1 >= 0)) { //check if we are still in the board + toHighlight.get(0).add(xFrom-i-1); + toHighlight.get(1).add(yFrom-i-1); + if (positionOccupied(xFrom-i-1,yFrom-i-1)) { //if we reach a position already occupied, stops highlighting the diagonal spotOccupied = true; } } @@ -561,10 +562,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //left bottom diagonal - if ((x-i-1 >= 0) && (y+1+i <= 7)) { - toHighlight.get(0).add(this.x-i-1); - toHighlight.get(1).add(this.y+i+1); - if (positionOccupied(this.x-i-1,this.y+i+1)) { + if ((xFrom-i-1 >= 0) && (yFrom+1+i <= 7)) { + toHighlight.get(0).add(xFrom-i-1); + toHighlight.get(1).add(yFrom+i+1); + if (positionOccupied(xFrom-i-1,yFrom+i+1)) { spotOccupied = true; } } @@ -577,10 +578,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //right bottom diagonal - if ((x+1+i <= 7) && (y+1+i <= 7)) { - toHighlight.get(0).add(this.x+i+1); - toHighlight.get(1).add(this.y+i+1); - if (positionOccupied(this.x+i+1,this.y+i+1)) { + if ((xFrom+1+i <= 7) && (yFrom+1+i <= 7)) { + toHighlight.get(0).add(xFrom+i+1); + toHighlight.get(1).add(yFrom+i+1); + if (positionOccupied(xFrom+i+1,yFrom+i+1)) { spotOccupied = true; } } @@ -593,10 +594,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //right top diagonal - if ((y-i-1 >= 0) && (x+1+i <= 7)) { - toHighlight.get(0).add(this.x+i+1); - toHighlight.get(1).add(this.y-i-1); - if (positionOccupied(this.x+i+1,this.y-i-1)) { + if ((yFrom-i-1 >= 0) && (xFrom+1+i <= 7)) { + toHighlight.get(0).add(xFrom+i+1); + toHighlight.get(1).add(yFrom-i-1); + if (positionOccupied(xFrom+i+1,yFrom-i-1)) { spotOccupied = true; } } @@ -608,25 +609,25 @@ public class Board { return toHighlight; } - public ArrayList> highlightQueen(){ //list of positions to highlight for the queens - ArrayList> toHighlight = highlightBishop(); - ArrayList> toHighlight2 = highlightRook(); + public ArrayList> highlightQueen(int xFrom, int yFrom, ArrayList pieces){ //list of positions to highlight for the queens + ArrayList> toHighlight = highlightBishop(xFrom, yFrom, pieces); + ArrayList> toHighlight2 = highlightRook(xFrom, yFrom, pieces); toHighlight.get(0).addAll(toHighlight2.get(0)); //combine the x coordinates toHighlight.get(1).addAll(toHighlight2.get(1)); //combine the y coordinates return toHighlight; } - public ArrayList> highlightRook(){ //list of positions to highlight for the rooks + public ArrayList> highlightRook(int xFrom, int yFrom, ArrayList pieces){ //list of positions to highlight for the rooks boolean spotOccupied = false; ArrayList> toHighlight = new ArrayList<> (); toHighlight.add(new ArrayList<>()); toHighlight.add(new ArrayList<>()); int i = 0; while (spotOccupied == false) { //horizontal top line - if (y-i-1 >= 0) { // check if we are still in the board (works the same as the bishop) - toHighlight.get(0).add(this.x); - toHighlight.get(1).add(this.y-i-1); - if (positionOccupied(this.x,this.y-i-1)) { + if (yFrom-i-1 >= 0) { // check if we are still in the board (works the same as the bishop) + toHighlight.get(0).add(xFrom); + toHighlight.get(1).add(yFrom-i-1); + if (positionOccupied(xFrom,yFrom-i-1)) { spotOccupied = true; } } @@ -639,10 +640,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //horizontal bottom line - if (y+i+1 <= 7) { - toHighlight.get(0).add(this.x); - toHighlight.get(1).add(this.y+i+1); - if (positionOccupied(this.x,this.y+i+1)) { + if (yFrom+i+1 <= 7) { + toHighlight.get(0).add(xFrom); + toHighlight.get(1).add(yFrom+i+1); + if (positionOccupied(xFrom,yFrom+i+1)) { spotOccupied = true; } } @@ -655,10 +656,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //vertical left line - if (x-i-1 >= 0) { - toHighlight.get(0).add(this.x-i-1); - toHighlight.get(1).add(this.y); - if (positionOccupied(this.x-i-1,this.y)) { + if (xFrom-i-1 >= 0) { + toHighlight.get(0).add(xFrom-i-1); + toHighlight.get(1).add(yFrom); + if (positionOccupied(xFrom-i-1,yFrom)) { spotOccupied = true; } } @@ -671,10 +672,10 @@ public class Board { spotOccupied = false; i = 0; while (spotOccupied == false) { //vertical right line - if (x+i+1 <= 7) { - toHighlight.get(0).add(this.x+i+1); - toHighlight.get(1).add(this.y); - if (positionOccupied(this.x+i+1,this.y)) { + if (xFrom+i+1 <= 7) { + toHighlight.get(0).add(xFrom+i+1); + toHighlight.get(1).add(yFrom); + if (positionOccupied(xFrom+i+1,yFrom)) { spotOccupied = true; } } @@ -683,18 +684,247 @@ public class Board { } i += 1; } + + int indexPieceSelect = whatPiece(xFrom,yFrom); + //castling + for(int x = 0; x<8; x++) { + for(int y = 0; y<8; y++) { + if (pieces.get(indexPieceSelect).isWhite() == true) {//check if castling is possible for the white + if (kingWMoved == false && rookLWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==0 && pieces.get(indexPieceSelect).getY()==7) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + if (kingWMoved == false && rookRWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==7) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + else { + castling = false; + } + } + if (pieces.get(indexPieceSelect).isWhite() == false) {//check if castling is possible for the black + if (kingBMoved == false && rookLBMoved == false && x==4 && y==0 && pieces.get(indexPieceSelect).getX()==0 && pieces.get(indexPieceSelect).getY()==0) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + if (kingBMoved == false && rookRBMoved == false && x==4 && y==0 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==0) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + else { + castling = false; + } + } + } + } return toHighlight; } - - public boolean isHighlighted(int x, int y) { //not at all optimized, if you have ideas go ahead + public ArrayList> highlightPawn(int xFrom, int yFrom, ArrayList pieces){ //test + ArrayList> toHighlight = new ArrayList<> (); + toHighlight.add(new ArrayList<>()); + toHighlight.add(new ArrayList<>()); + + int indexPieceSelect = whatPiece(xFrom,yFrom); + for(int x = 0; x<8; x++) { + for(int y = 0; y<8; y++) { + if (pieces.get(indexPieceSelect).isWhite()) {//white pawns + + if (lastTurnPawnTwo) {//en passant + if(yTwo == yFrom && Math.abs(xTwo - xFrom) == 1 && x == xTwo && y == yFrom - 1) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + enPassant = true; + } + } + + if(y == yFrom-1) { + if((x == xFrom) && (positionOccupied(x,y)==false)) {//advance one case + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + + + 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 and of another color + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + if((y == yFrom-2) && (yFrom == 6) && (x == xFrom) && (positionOccupied(xFrom,yFrom-1))==false && (positionOccupied(xFrom,yFrom-2) == false)) {//move by two if not moved and if no piece in front + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + + if (pieces.get(indexPieceSelect).isWhite() == false) {//black pawns + if(y == yFrom+1) { + + if (lastTurnPawnTwo) {//en passant + if(yTwo == yFrom && Math.abs(xTwo - xFrom) == 1 && x == xTwo && y == yFrom+1) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + enPassant = true; + } + } + + if((x == xFrom) && (positionOccupied(x,y)==false)) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + + if((Math.abs(x-xFrom) == 1) && (positionOccupied(x,y)) && pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + if((y == yFrom+2) && (yFrom == 1) && (x == xFrom) && (positionOccupied(xFrom,yFrom+1) == false) && (positionOccupied(xFrom,yFrom+2) == false)) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + } + } + + return toHighlight; + } + + public ArrayList> highlightKing(int xFrom, int yFrom, ArrayList pieces){ //returns a board of x and y coordinates to highlight when selecting a king + ArrayList> toHighlight = new ArrayList<> (); + toHighlight.add(new ArrayList<>()); + toHighlight.add(new ArrayList<>()); + + int indexPieceSelect = whatPiece(xFrom,yFrom); + for(int x = 0; x<8; x++) { + for(int y = 0; y<8; y++) { + if(positionOccupied(x,y) == false) { + if((Math.abs(x-xFrom) <= 1) && (Math.abs(y-yFrom) <= 1)) { //consider all cases at distance one from the king + if ((Math.abs(x-xFrom) != 0) || (Math.abs(y-yFrom) != 0)) { //check if we are not considering the place where the king is + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()){//check that we do not highlight a case with a piece of our color + if((Math.abs(x-xFrom) <= 1) && (Math.abs(y-yFrom) <= 1)) { //consider all cases at distance one from the king + if ((Math.abs(x-xFrom) != 0) || (Math.abs(y-yFrom) != 0)) { //check if we are not considering the place where the king is + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + } + //check if the castling is possible, if yes highlight the rook(s) + int nbOccupied = 0; + boolean occupiedLeftLine = true; + boolean occupiedRightLine = true; + + for(int i = 3; i > 0; i--) { + if (positionOccupied(i,pieces.get(indexPieceSelect).getY()) == true) { + nbOccupied = nbOccupied + 1; + } + if (nbOccupied == 0) { + occupiedLeftLine=false; + } + } + if (occupiedLeftLine == false) {//check if the castling is possible with the rooks on the left + if (kingWMoved == false && rookLWMoved == false && x==0 && y==7 && pieces.get(whatPiece(0,7)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + if (kingBMoved == false && rookLBMoved == false && x==0 && y==0 && pieces.get(whatPiece(0,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + // } + //else { + //castling = false; + } + } + nbOccupied = 0; + for(int i = 5; i < 7; i++) { + if (positionOccupied(i,pieces.get(indexPieceSelect).getY()) == true) { + nbOccupied = nbOccupied + 1; + } + if (nbOccupied == 0) { + occupiedRightLine=false; + } + } + if (occupiedRightLine == false) {//check if the castling is possible with the rooks on the right + if (kingWMoved == false && rookRWMoved == false && x==7 && y==7 && pieces.get(whatPiece(7,7)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + if (kingBMoved == false && rookRBMoved == false && x==7 && y==0 && pieces.get(whatPiece(7,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + castling = true; + } + // else { + // castling = false; + //} + } + nbOccupied = 0; + } + + } + } + return toHighlight; + } + + public ArrayList> highlightKnight(int xFrom, int yFrom, ArrayList pieces){ //returns a board of x and y coordinates to highlight when selecting a knight + ArrayList> toHighlight = new ArrayList<> (); + toHighlight.add(new ArrayList<>()); + toHighlight.add(new ArrayList<>()); + + int indexPieceSelect = whatPiece(xFrom,yFrom); + for(int x = 0; x<8; x++) { + for(int y = 0; y<8; y++) { + if(positionOccupied(x,y) == false) { + if (((Math.abs(x-xFrom) == 1) && (Math.abs(y-yFrom) == 2)) || ((Math.abs(x-xFrom) == 2) && (Math.abs(y-yFrom) == 1))) {//consider all positions at 2 by 1 from the knight + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()){ + if (((Math.abs(x-xFrom) == 1) && (Math.abs(y-yFrom) == 2)) || ((Math.abs(x-xFrom) == 2) && (Math.abs(y-yFrom) == 1))) {//consider all positions at 2 by 1 from the knight + toHighlight.get(0).add(x); + toHighlight.get(1).add(y); + } + } + } + } + } + + return toHighlight; + } + + public boolean isHighlightedNoCheck(int x, int y, int xFrom, int yFrom, ArrayList pieces) { boolean highlight = false; - if (positionOccupied(this.x,this.y)) { //check if we have selected a position with a piece - int indexPieceSelect = whatPiece(this.x,this.y); + if (positionOccupied(xFrom,yFrom)) { //check if we have selected a position with a piece + int indexPieceSelect = whatPiece(xFrom,yFrom); if (pieces.get(indexPieceSelect).getType() == PieceType.Pawn) {//highlight for pawns - - if (pieces.get(indexPieceSelect).isWhite()) {//white pawns + int numHighlight = highlightPawn(xFrom, yFrom, pieces).get(0).size(); + for(int i = 0; i < numHighlight; i++) { + if ((x == highlightPawn(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightPawn(xFrom, yFrom, pieces).get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted + if(positionOccupied(x,y) == false) { + highlight = true; + } + else { + if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + } + } + } + } + /*if (pieces.get(indexPieceSelect).isWhite()) {//white pawns if (lastTurnPawnTwo) {//en passant if(yTwo == this.y && Math.abs(xTwo - this.x) == 1 && x == xTwo && y == this.y - 1) { @@ -739,11 +969,17 @@ public class Board { 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 THIS IS NOT OPTIMISED BC OF WHATPIECE (SAME FOR ALL FOLLOWING PIECES) - if(positionOccupied(x,y) == false) { + if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings + int numHighlight = highlightKing(xFrom, yFrom, pieces).get(0).size(); + for(int i = 0; i < numHighlight; i++) { + if ((x == highlightKing(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightKing(xFrom, yFrom, pieces).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) { 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; @@ -807,17 +1043,18 @@ public class Board { //} } nbOccupied = 0; - } - - /*altPieces = pieces; //create a copy of pieces that we will modify to check if the movement leaves the king in check - if (highlight) { - altPieces.get(whatPiece(this.x,this.y)).setX(x); - altPieces.get(whatPiece(this.x,this.y)).setY(y); }*/ } if (pieces.get(indexPieceSelect).getType() == PieceType.Knight) { //highlight for knights - if(positionOccupied(x,y) == false) { + int numHighlight = highlightKnight(xFrom, yFrom, pieces).get(0).size(); + for(int i = 0; i < numHighlight; i++) { + if ((x == highlightKnight(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightKnight(xFrom, yFrom, pieces).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) { 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; } @@ -829,12 +1066,12 @@ public class Board { } } } - } + }*/ if (pieces.get(indexPieceSelect).getType() == PieceType.Bishop) { //highlight for bishops - int numHighlight = highlightBishop().get(0).size(); + int numHighlight = highlightBishop(xFrom, yFrom, pieces).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 + if ((x == highlightBishop(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightBishop(xFrom, yFrom, pieces).get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted if(positionOccupied(x,y) == false) { highlight = true; } @@ -848,9 +1085,9 @@ public class Board { } if (pieces.get(indexPieceSelect).getType() == PieceType.Queen) { //highlight for Queens - int numHighlight = highlightQueen().get(0).size(); + int numHighlight = highlightQueen(xFrom, yFrom, pieces).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 + if ((x == highlightQueen(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightQueen(xFrom, yFrom, pieces).get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted if(positionOccupied(x,y) == false) { highlight = true; } @@ -864,9 +1101,9 @@ public class Board { } if (pieces.get(indexPieceSelect).getType() == PieceType.Rook) { //highlight for rooks - int numHighlight = highlightRook().get(0).size(); + int numHighlight = highlightRook(xFrom, yFrom, pieces).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 + if ((x == highlightRook(xFrom, yFrom, pieces).get(0).get(i)) && (y == highlightRook(xFrom, yFrom, pieces).get(1).get(i))) {//check if the considered x and y are in the list of positions that should be highlighted if(positionOccupied(x,y) == false) { highlight = true; } @@ -875,7 +1112,7 @@ public class Board { highlight = true; } //check if the castling is possible, if yes highlight the king - if (pieces.get(indexPieceSelect).isWhite() == true) {//check if castling is possible for the white + /*if (pieces.get(indexPieceSelect).isWhite() == true) {//check if castling is possible for the white if (kingWMoved == false && rookLWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==0 && pieces.get(indexPieceSelect).getY()==7) { highlight = true; castling = true; @@ -900,7 +1137,7 @@ public class Board { else { castling = false; } - } + }*/ } } } @@ -909,6 +1146,20 @@ public class Board { return highlight; } + public boolean isHighlighted(int x, int y) { //not at all optimized, if you have ideas go ahead + boolean highlight = false; + Move move = new Move(this.x, this.y, x, y); + //Board board1 = new Board(pieces); + String[] boardS = toString().split("\n"); + Check check = new Check(boardS, move); + //System.out.println(x); + //System.out.println(y); + if(isHighlightedNoCheck(x, y, this.x, this.y, pieces) && check.getCheck() == false) { + highlight = true; + } + return highlight; + } + public void undoLastMove() { this.x = -1; this.y = -1; @@ -990,9 +1241,18 @@ public class Board { } + public Board(ArrayList pieces) { + /*ArrayList pieces1 = new ArrayList <>(); + int sizePieces = pieces.size(); + for(int i = 0; i < sizePieces; i++) { + Piece newP = new Piece(pieces.get(i)); + pieces1.add(newP); + } + this.board = pieces1;*/ + } + public Board(Board board) { - //TODO - + this.pieces = board.getPieces(); } public void playMove(Move move) { diff --git a/src/backend/Check.java b/src/backend/Check.java index c943f26..04f9515 100644 --- a/src/backend/Check.java +++ b/src/backend/Check.java @@ -1,53 +1,63 @@ package backend; -import backend.Board; import java.util.ArrayList; -import java.lang.Math; public class Check { - private ArrayList altPieces ; + private ArrayList altPieces = new ArrayList<>(); private boolean isCheck; private int x; private int y; private int xMove; private int yMove; private Board board; - //in Board, add a boolean isChecked ? - public Check (ArrayList pieces, int x, int y, int xMove, int yMove) { - altPieces = pieces; - this.x = x; - this.y = y; - this.xMove = xMove; - this.yMove = yMove; + + public Check (String[] boardS, Move move) { + this.board = new Board(boardS); + int sizePieces = this.board.getPieces().size(); + for(int i = 0; i < sizePieces; i++) { + altPieces.add(this.board.getPieces().get(i)); + } + this.x = move.getFromX(); + this.y = move.getFromY(); + this.xMove = move.getToX(); + this.yMove = move.getToY(); } - public int whatPiece(int x, int y) { // method which gives the index (in the pieces array) of the piece at a position x,y - boolean pieceHere = false; - int index = 0; - while (pieceHere == false && index != altPieces.size()) { - if((x == altPieces.get(index).getX()) && (y == altPieces.get(index).getY())){ - pieceHere =true; - } - else { - index += 1; + public boolean getCheck() { + isCheck = false; + if (this.x != -1 && this.y != -1) { + boolean color = altPieces.get(this.board.whatPiece(this.x, this.y)).isWhite(); + altPieces.get(this.board.whatPiece(this.x,this.y)).setX(this.xMove);//in the alternative board, moves the selected piece to the wanted position. + altPieces.get(this.board.whatPiece(this.xMove,this.y)).setY(this.yMove); + int xBase = this.x; + int yBase = this.y; + //System.out.println(this.board.toString()); + int sizePieces = altPieces.size(); + for (int i = 0; i < sizePieces; i++) { + if(altPieces.get(i).isWhite() != color) { + this.x = altPieces.get(i).getX(); + this.y = altPieces.get(i).getY(); + for (int x = 0; x<8; x++) { + for (int y = 0; y<8; y++) { + boolean highlight = this.board.isHighlightedNoCheck(x, y, this.x, this.y, altPieces); + if (this.board.positionOccupied(x, y)) { + if(highlight && altPieces.get(this.board.whatPiece(x,y)).getType() == PieceType.King && altPieces.get(this.board.whatPiece(x,y)).isWhite() == color){ + isCheck = true; + } + } + } + } + } + else { + this.x = xBase; + this.y = yBase; + } } } - if(index == altPieces.size()) { - index=-1; - } - return index; - } - - public boolean kingCheck() { - altPieces.get(whatPiece(this.x,this.y)).setX(this.xMove); - altPieces.get(whatPiece(this.x,this.y)).setY(this.yMove); - return isCheck; + } - - - } diff --git a/src/backend/Move.java b/src/backend/Move.java index 5f64282..73c48db 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -1,5 +1,31 @@ package backend; public class Move { + private int fromX; + private int fromY; + private int toX; + private int toY; + public Move (int fromX, int fromY, int toX, int toY) { + this.fromX = fromX; + this.fromY = fromY; + this.toX = toX; + this.toY = toY; + } + + public int getFromX() { + return fromX; + } + + public int getFromY() { + return fromY; + } + + public int getToX() { + return toX; + } + + public int getToY() { + return toY; + } } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index dc6cc6e..d5c0c9d 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -12,6 +12,15 @@ public class Piece { this.type = type; this.isWhite = isWhite; } + + public Piece(Piece piece) { + this.x = piece.getX(); + this.y = piece.getY(); + this.type = piece.getType(); + this.isWhite = piece.isWhite(); + //Piece piece1 = new Piece(piece.isWhite(), piece.getType(), piece.getX(), piece.getY()); + } + public int getX() { return x; }