tried fixing check and commented

This commit is contained in:
mimie 2025-05-22 15:07:51 +02:00
parent bb30f9818d
commit da59350569
2 changed files with 30 additions and 177 deletions

View File

@ -851,62 +851,16 @@ public class Board {
int numHighlight = highlightPawn(xFrom, yFrom, pieces).get(0).size(); int numHighlight = highlightPawn(xFrom, yFrom, pieces).get(0).size();
for(int i = 0; i < numHighlight; i++) { 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 ((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) { if(positionOccupied(x,y) == false) {//if the position is not occupied
highlight = true; highlight = true;
} }
else { else {
if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) {//or if we eat a piece of the opposite color
highlight = true; 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) {
highlight = true;
enPassant = true;
}
}
if(y == this.y-1) {
if((x == this.x) && (positionOccupied(x,y)==false)) {//advance one case
highlight = true;
}
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
highlight = true;
}
}
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;
}
}
if (pieces.get(indexPieceSelect).isWhite() == false) {//black pawns
if(y == this.y+1) {
if (lastTurnPawnTwo) {//en passant
if(yTwo == this.y && Math.abs(xTwo - this.x) == 1 && x == xTwo && y == this.y+1) {
highlight = true;
enPassant = true;
}
}
if((x == this.x) && (positionOccupied(x,y)==false)) {
highlight = true;
}
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) && (positionOccupied(this.x,this.y+1) == false)) {
highlight = true;
}
}*/
} }
if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings if (pieces.get(indexPieceSelect).getType() == PieceType.King) { //highlight for Kings
@ -916,71 +870,6 @@ public class Board {
highlight = true; 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;
}
}
}
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;
}
}
}
//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;
}*/
} }
if (pieces.get(indexPieceSelect).getType() == PieceType.Knight) { //highlight for knights if (pieces.get(indexPieceSelect).getType() == PieceType.Knight) { //highlight for knights
@ -991,19 +880,6 @@ public class Board {
} }
} }
} }
/*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;
}
}
}
}*/
if (pieces.get(indexPieceSelect).getType() == PieceType.Bishop) { //highlight for bishops if (pieces.get(indexPieceSelect).getType() == PieceType.Bishop) { //highlight for bishops
int numHighlight = highlightBishop(xFrom, yFrom, pieces).get(0).size(); int numHighlight = highlightBishop(xFrom, yFrom, pieces).get(0).size();
@ -1048,33 +924,6 @@ public class Board {
if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) { if(pieces.get(whatPiece(x,y)).isWhite() != pieces.get(indexPieceSelect).isWhite()) {
highlight = true; 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;
}
}*/
} }
} }
} }
@ -1083,15 +932,12 @@ public class Board {
return highlight; return highlight;
} }
public boolean isHighlighted(int x, int y) { //not at all optimized, if you have ideas go ahead public boolean isHighlighted(int x, int y) { //same as isHighlightedNoCheck but with added verification of if we are in check
boolean highlight = false; boolean highlight = false;
Move move = new Move(this.x, this.y, x, y); Move move = new Move(this.x, this.y, x, y);
//Board board1 = new Board(pieces);
String[] boardS = toString().split("\n"); String[] boardS = toString().split("\n");
Check check = new Check(boardS, move); Check check = new Check(boardS, move);
//System.out.println(x); if( isHighlightedNoCheck(x, y, this.x, this.y, pieces) && check.getCheck() == false) {//verify if a case is highlighted and does not put the king in check
//System.out.println(y);
if(isHighlightedNoCheck(x, y, this.x, this.y, pieces) && check.getCheck() == false) {
highlight = true; highlight = true;
} }
return highlight; return highlight;
@ -1102,7 +948,7 @@ public class Board {
this.y = -1; this.y = -1;
int sizeSave = previousBoard.size(); int sizeSave = previousBoard.size();
if(sizeSave == 1) { if(sizeSave == 1) {
String lastBoard = previousBoard.get(0); // we have a String but we need a String[] in order to use the constructer String lastBoard = previousBoard.get(0); // we have a String but we need a String[] in order to use the constructor
String[] oldBoard = lastBoard.split("\n"); String[] oldBoard = lastBoard.split("\n");
Board undo = new Board(oldBoard); Board undo = new Board(oldBoard);
this.pieces = undo.pieces; this.pieces = undo.pieces;
@ -1121,7 +967,7 @@ public class Board {
} }
if (sizeSave != 1 ) { if (sizeSave != 1 ) {
turnNumber -= 1; turnNumber -= 1;
String lastBoard = previousBoard.get(sizeSave-2); // we have a String but we need a String[] in order to use the constructer String lastBoard = previousBoard.get(sizeSave-2); // we have a String but we need a String[] in order to use the constructor
String[] oldBoard = lastBoard.split("\n"); String[] oldBoard = lastBoard.split("\n");
Board undo = new Board(oldBoard); Board undo = new Board(oldBoard);
this.pieces = undo.pieces; this.pieces = undo.pieces;

View File

@ -4,12 +4,14 @@ import java.util.ArrayList;
public class Check { public class Check {
private ArrayList<Piece> altPieces = new ArrayList<>(); private ArrayList<Piece> altPieces = new ArrayList<>();
private ArrayList<Piece> ogPieces = new ArrayList<>();
private boolean isCheck; private boolean isCheck;
private int x; private int x;
private int y; private int y;
private int xMove; private int xMove;
private int yMove; private int yMove;
private Board board; private Board board;
private boolean kingAlrCheck;
public Check (String[] boardS, Move move) { public Check (String[] boardS, Move move) {
@ -17,6 +19,7 @@ public class Check {
int sizePieces = this.board.getPieces().size(); int sizePieces = this.board.getPieces().size();
for(int i = 0; i < sizePieces; i++) { for(int i = 0; i < sizePieces; i++) {
altPieces.add(this.board.getPieces().get(i)); altPieces.add(this.board.getPieces().get(i));
ogPieces.add(this.board.getPieces().get(i));
} }
this.x = move.getFromX(); this.x = move.getFromX();
this.y = move.getFromY(); this.y = move.getFromY();
@ -24,40 +27,44 @@ public class Check {
this.yMove = move.getToY(); this.yMove = move.getToY();
} }
public boolean isCheck() {
isCheck = false;
return isCheck;
}
public boolean getCheck() { public boolean getCheck() {
isCheck = false; isCheck = false;
if (this.x != -1 && this.y != -1) { if (this.x != -1 && this.y != -1) {
boolean color = altPieces.get(this.board.whatPiece(this.x, this.y)).isWhite(); 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.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); 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(); int sizePieces = altPieces.size();
for (int i = 0; i < sizePieces; i++) { for (int i = 0; i < sizePieces; i++) {//check for all pieces
if(altPieces.get(i).isWhite() != color) { if(altPieces.get(i).isWhite() != color) {//if they are of the opposite color
this.x = altPieces.get(i).getX(); int xTemp = altPieces.get(i).getX();
this.y = altPieces.get(i).getY(); int yTemp = altPieces.get(i).getY();
for (int x = 0; x<8; x++) { for (int x = 0; x<8; x++) {// for all cases of the board
for (int y = 0; y<8; y++) { for (int y = 0; y<8; y++) {
boolean highlight = this.board.isHighlightedNoCheck(x, y, this.x, this.y, altPieces); boolean highlight = this.board.isHighlightedNoCheck(x, y, xTemp, yTemp, altPieces);//if the piece (i) highlight the case x,y
if (this.board.positionOccupied(x, y)) { 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){ if(altPieces.get(this.board.whatPiece(x,y)).getType() == PieceType.King && altPieces.get(this.board.whatPiece(x,y)).isWhite() == color){
if(highlight) {//if the case x,y is highlighted and occupied by a king of the same color as the original piece
isCheck = true; isCheck = true;
} }
}
} }
} }
} }
} }
else {
this.x = xBase;
this.y = yBase;
}
} }
} }
return isCheck; return isCheck;
} }
} }