Castling super ok

This commit is contained in:
gwitt 2025-05-16 09:20:15 +02:00
parent 9f2106ba2d
commit 017377fe73
2 changed files with 150 additions and 2 deletions

View File

@ -15,6 +15,14 @@ public class Board {
private int xTwo;
private int yTwo;
private boolean enPassant = false;
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;
public Board(int colNum, int lineNum) {
this.colNum = colNum;
@ -95,6 +103,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() {
@ -164,12 +181,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);
@ -191,9 +261,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) {
@ -475,6 +547,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;
}
}
@ -536,6 +657,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;
}
}
}
}
}

View File

@ -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();
}