Compare commits

..

3 Commits

Author SHA1 Message Date
gwitt c6912dc166 Castling super ok 2025-05-16 09:22:29 +02:00
gwitt f01ee092c0 Castling super ok 2025-05-16 09:22:07 +02:00
gwitt 017377fe73 Castling super ok 2025-05-16 09:20:15 +02:00
2 changed files with 159 additions and 12 deletions

View File

@ -12,9 +12,17 @@ public class Board {
private int y;
private int turnNumber;
private boolean lastTurnPawnTwo = false;//for en passant
private int xTwo;//coordinates of pawn that moved two
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() {
@ -144,7 +161,7 @@ public class Board {
public ArrayList<Piece> getPieces() {
return pieces;
}
} //test2
public void userTouch(int x, int y) {
boolean positionOccupied = this.positionOccupied(x, y) ; // verify if the square is occupied
@ -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);
@ -185,15 +255,17 @@ public class Board {
pieces.get(indexPiece).setX(x);
pieces.get(indexPiece).setY(y);
this.turnNumber +=1; // we add 1 to the number of turns played
this.turnNumber +=1;
}
// 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) {
@ -207,20 +279,21 @@ public class Board {
/* saving-loading feature :*/
public String[] toFileRep() {
String[] saveFile = new String[9]; // creation of the string array which will be the one returned
String[] saveFile = new String[8]; // creation of the string array which will be the one returned
String actualBoard = this.toString();
int nbLine = 0;
saveFile[0] = "";
for(int i = 0; i<200 ;i++) { // there are in total 200 strings in the
if (actualBoard.charAt(i) == '\n') {
nbLine+=1;
if (nbLine < 8 ) {
saveFile[nbLine]= "";
}
}
else {
saveFile[nbLine] += actualBoard.charAt(i);
}
}
saveFile[8] += this.turnNumber;
return saveFile;
}
@ -229,6 +302,7 @@ public class Board {
this.lineNum = 8;
x = -1;
y = -1;
turnNumber= 0;
pieces = new ArrayList<>();
for(int i=0; i<8;i++) { // this will be the Y coordinate
for(int j=0; j<8;j++) { // this will be the X coordinate
@ -247,10 +321,7 @@ public class Board {
}
}
}
turnNumber = Integer.parseInt(array[8]);
System.out.println(turnNumber);
int turn = this.getTurnNumber();
System.out.println(turn);
}
/* The following methods require more work ! */
@ -476,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;
}
}
@ -537,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();
}