diff --git a/composition echec/test1 b/composition echec/test1 new file mode 100644 index 0000000..96226e0 --- /dev/null +++ b/composition echec/test1 @@ -0,0 +1,10 @@ +BR,BN,BB,BQ,BK,BB,BN,BR, +BP,BP,BP, ,BP,BP,BP,BP, + , , , , , , , , + , , ,BP,WP, , , , + , , , , , , , , + , , , , , , , , +WP,WP,WP,WP, ,WP,WP,WP, +WR,WN,WB,WQ,WK,WB,WN,WR, +3 +1331 diff --git a/composition echec/test2 b/composition echec/test2 new file mode 100644 index 0000000..c3cf810 --- /dev/null +++ b/composition echec/test2 @@ -0,0 +1,10 @@ +BR,BN,BB,BQ,BK,BB,BN,BR, +BP,BP,BP, ,BP, ,BP,BP, + , , , , ,WP, , , + , , ,BP, , , , , + , , , , , , , , + , , , , , , , , +WP,WP,WP,WP, ,WP,WP,WP, +WR,WN,WB,WQ,WK,WB,WN,WR, +6 +0440 diff --git a/composition echec/test3 b/composition echec/test3 new file mode 100644 index 0000000..8ebe2b9 --- /dev/null +++ b/composition echec/test3 @@ -0,0 +1,9 @@ +BR,BN,BB,BQ,BK,BB,BN,BR, +BP,BP, ,BP, ,BP, ,BP, + , ,BP, ,BP, ,BP, , + , , , , , , , , + , , , ,WP, , , , + , , , , ,WP,WP, , +WP,WP,WP,WP, , , ,WP, +WR,WN,WB,WQ,WK,WB,WN,WR, +6 diff --git a/src/backend/Board.java b/src/backend/Board.java index 0fc7123..6174608 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -13,10 +13,17 @@ public class Board { private int turnNumber; private boolean lastTurnPawnTwo;//for en passant private int xTwo; - private int yTwo; + private int yTwo; private boolean enPassant; - private ArrayList previousBoard; - + private boolean kingWMoved = false; + private boolean kingBMoved = false; + private boolean rookLWMoved = false; + private boolean rookRWMoved = false; + private boolean rookLBMoved = false; + private boolean rookRBMoved = false; + private boolean castling = false; + private boolean castlingDone = false; + private ArrayList previousBoard; public Board(int colNum, int lineNum) { this.colNum = colNum; @@ -100,6 +107,15 @@ public class Board { public void cleanBoard() { turnNumber = 0; pieces.clear(); + enPassant = false; + kingWMoved = false; + kingBMoved = false; + rookLWMoved = false; + rookRWMoved = false; + rookLBMoved = false; + rookRBMoved = false; + castling = false; + castlingDone = false; } public String toString() { @@ -185,12 +201,65 @@ public class Board { 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 } + else {//castling + if (castling == true) { + if (pieces.get(whatPiece(x,y)).getX()==7 || pieces.get(whatPiece(this.x,this.y)).getX()==7) {//that means we are dealing with the right rooks for castling + if (pieces.get(whatPiece(this.x,this.y)).isWhite()==true){ + kingWMoved = true; + System.out.print("W"); + } + else { + kingBMoved = true; + System.out.print("B"); + } + pieces.get(whatPiece(7,y)).setX(5); + pieces.get(whatPiece(4,y)).setX(6); + this.x=-1; + this.y=-1; + this.turnNumber +=1; + castlingDone=true; + } + + else {//that means we are dealing with the right rooks for castling + if (pieces.get(whatPiece(x,y)).isWhite()==true){ + kingWMoved = true; + } + else { + kingBMoved = true; + } + pieces.get(whatPiece(0,y)).setX(3); + pieces.get(whatPiece(4,y)).setX(2); + this.x=-1; + this.y=-1; + this.turnNumber +=1; + castlingDone=true; + } + } + } } - if(((this.positionOccupied(x, y) == false || pieces.get(whatPiece(x,y)).isWhite() != pieces.get(whatPiece(this.x,this.y)).isWhite())) && (highlights)) {//moves the piece only if the position selected is not occupied by a piece of the same color + if(((this.positionOccupied(x, y) == false || pieces.get(whatPiece(x,y)).isWhite() != pieces.get(whatPiece(this.x,this.y)).isWhite())) && (highlights) && castlingDone==false) {//moves the piece only if the position selected is not occupied by a piece of the same color if (enPassant) { this.pieces.remove(whatPiece(xTwo,yTwo)); enPassant = false; } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 0 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) { + rookLWMoved = true; + } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 7 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) { + rookRWMoved = true; + } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 0 && pieces.get(whatPiece(this.x, this.y)).getY() == 0) { + rookLBMoved = true; + } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 7 && pieces.get(whatPiece(this.x, this.y)).getY() == 0) { + rookRBMoved = true; + } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 4 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) { + kingWMoved = true; + } + if (pieces.get(whatPiece(this.x, this.y)).getX() == 4 && pieces.get(whatPiece(this.x, this.y)).getY() == 0) { + kingBMoved = true; + } int indexPiece = this.whatPiece(this.x, this.y); @@ -212,9 +281,11 @@ public class Board { // we now reset our parameters this.x=-1; this.y=-1; + castlingDone=false; // we add 1 to the number of turned played } } + } public boolean isSelected(int x, int y) { @@ -527,6 +598,55 @@ public class Board { } } } + //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()) { + highlight = true; + castling = true; + } + if (kingBMoved == false && rookLBMoved == false && x==0 && y==0 && pieces.get(whatPiece(0,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + 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()) { + highlight = true; + castling = true; + } + if (kingBMoved == false && rookRBMoved == false && x==7 && y==0 && pieces.get(whatPiece(7,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) { + highlight = true; + castling = true; + } + // else { + // castling = false; + //} + } + nbOccupied = 0; } } @@ -588,6 +708,33 @@ public class Board { if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { 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 (kingWMoved == false && rookLWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==0 && pieces.get(indexPieceSelect).getY()==7) { + highlight = true; + castling = true; + } + if (kingWMoved == false && rookRWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==7) { + highlight = true; + 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) { + highlight = true; + castling = true; + } + if (kingBMoved == false && rookRBMoved == false && x==4 && y==0 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==0) { + highlight = true; + castling = true; + } + else { + castling = false; + } + } } } } diff --git a/src/backend/Game.java b/src/backend/Game.java index 4c64f70..366bba1 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -98,7 +98,7 @@ public class Game extends Thread { public boolean isHighlighted(int x, int y) { return board.isHighlighted(x, y); } - + public void undoLastMove() { board.undoLastMove(); }