This commit is contained in:
edwin 2025-05-23 08:17:57 +02:00
commit 1374fb6c79
3 changed files with 108 additions and 260 deletions

View File

@ -8,10 +8,10 @@ public class AutoPlayer {
* @return
*/
public Move computeBestMove(Board board) {
ArrayList<Move> authorizedMoves = new ArrayList<>();
ArrayList<Move> authorizedMoves = new ArrayList<>();//create a list of possible move that the AutoPlayer is authorized to do
Board copy = new Board(board);//create a copy of the board in order not to impact the real board
ArrayList<Piece> alivePieces = copy.getPieces();
for (int i=0; i < alivePieces.size(); i++) {//for each piece of the copied board we are going to clic on them to check their highlighted case
for (int i=0; i < alivePieces.size(); i++) {//for each piece of the copied board we are going to "clic" on them to check their highlighted cases
Piece piece = alivePieces.get(i);
if (piece.isWhite() == copy.isTurnWhite()) {
copy.userTouch(piece.getX(), piece.getY());
@ -27,10 +27,10 @@ public class AutoPlayer {
}
if (authorizedMoves.isEmpty()) {
System.out.print("no move possible");
return null;//possibility of being checked, must work on it later
return null;//possibility of being checked
}
int randomNum = (int)(Math.random() * authorizedMoves.size());
return authorizedMoves.get(randomNum);
return authorizedMoves.get(randomNum);//choose a random move in the authorized moves list
}
}

View File

@ -26,10 +26,6 @@ public class Board {
private boolean castling;
private boolean castlingDone;
private ArrayList<String> previousBoard;
//private ArrayList<Piece> altPieces;
//private boolean isChecked;
public Board(int colNum, int lineNum) {
this.kingWMoved = false;
this.kingBMoved = false;
@ -266,7 +262,8 @@ 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
/*castling movement*/
else {
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){
@ -281,9 +278,9 @@ public class Board {
this.y=-1;
this.turnNumber +=1;
castlingDone=true;
}//TEST
}
else {//that means we are dealing with the right rooks for castling
else {//that means we are dealing with the left rooks for castling
if (pieces.get(whatPiece(x,y)).isWhite()==true){
kingWMoved = true;
}
@ -305,6 +302,7 @@ public class Board {
this.pieces.remove(whatPiece(xTwo,yTwo));
enPassant = false;
}
/*memorize if they moved in order to avoid them castling*/
if (pieces.get(whatPiece(this.x, this.y)).getX() == 0 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) {
rookLWMoved = true;
}
@ -472,10 +470,6 @@ public class Board {
this.castlingDone = true;
}
}
/* The following methods require more work ! */
// COUCOU LES COPAINS SI VOUS ARRIVEZ A OPTIMISER LA SUITE CA SERAIT COOL
public ArrayList<ArrayList<Integer>> highlightBishop(int xFrom, int yFrom, ArrayList<Piece> pieces){ //creates a table of the x and y coordinates to highlight for the bishop
boolean spotOccupied = false;
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
@ -621,47 +615,10 @@ 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 ArrayList<ArrayList<Integer>> highlightPawn(int xFrom, int yFrom, ArrayList<Piece> pieces){ //test
public ArrayList<ArrayList<Integer>> highlightPawn(int xFrom, int yFrom, ArrayList<Piece> pieces){
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
toHighlight.add(new ArrayList<>());
toHighlight.add(new ArrayList<>());
@ -758,16 +715,16 @@ public class Board {
int nbOccupied = 0;
boolean occupiedLeftLine = true;
boolean occupiedRightLine = true;
for(int i = 4; i > 0; i--) {
if (positionOccupied(i,pieces.get(indexPieceSelect).getY()) == true) {
//check if the castling is possible with the rooks on the left
for(int i1 = 3; i1 > 0; i1--) {
if (positionOccupied(i1,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 (occupiedLeftLine == false) {//if there are no pieces between the rook and the king we can highlight
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);
@ -777,21 +734,19 @@ public class Board {
toHighlight.get(0).add(x);
toHighlight.get(1).add(y);
castling = true;
// }
//else {
//castling = false;
}
}
nbOccupied = 0;
for(int i = 4; i < 7; i++) {
if (positionOccupied(i,pieces.get(indexPieceSelect).getY()) == true) {
//check if the castling is possible with the rooks on the right
for(int i2 = 5; i2 < 7; i2++) {
if (positionOccupied(i2,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 (occupiedRightLine == false) {//if there are no pieces between the rook and the king we can highlight
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);
@ -802,9 +757,6 @@ public class Board {
toHighlight.get(1).add(y);
castling = true;
}
// else {
// castling = false;
//}
}
nbOccupied = 0;
}
@ -851,62 +803,16 @@ public class Board {
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) {
if(positionOccupied(x,y) == false) {//if the position is not occupied
highlight = true;
}
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;
}
}
}
}
/*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
@ -916,71 +822,6 @@ public class Board {
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
@ -991,19 +832,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
int numHighlight = highlightBishop(xFrom, yFrom, pieces).get(0).size();
@ -1048,13 +876,25 @@ 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) {
if (pieces.get(whatPiece(x,y)).getType() == PieceType.King) {//check if the castling is possible, if yes highlight the rook(s)
int nbOccupied = 0;
boolean occupiedLeftLine = true;
boolean occupiedRightLine = true;
for(int i1 = 0; i1 < 4; i1++) {
if (positionOccupied(i1,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 && pieces.get(indexPieceSelect).getX()==0 && x==4 && y==7 && pieces.get(whatPiece(4,7)).isWhite() == pieces.get(indexPieceSelect).isWhite()) {
highlight = true;
castling = true;
}
if (kingWMoved == false && rookRWMoved == false && x==4 && y==7 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==7) {
if (kingBMoved == false && rookLBMoved == false && pieces.get(indexPieceSelect).getX()==0 && x==4 && y==0 && pieces.get(whatPiece(4,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) {
highlight = true;
castling = true;
}
@ -1062,19 +902,31 @@ public class Board {
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) {
nbOccupied = 0;
for(int i2 = 5; i2 < 7; i2++) {
if (positionOccupied(i2,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 && pieces.get(indexPieceSelect).getX()==7 && x==4 && y==7 && pieces.get(whatPiece(4,7)).isWhite() == pieces.get(indexPieceSelect).isWhite()) {
highlight = true;
castling = true;
}
if (kingBMoved == false && rookRBMoved == false && x==4 && y==0 && pieces.get(indexPieceSelect).getX()==7 && pieces.get(indexPieceSelect).getY()==0) {
if (kingBMoved == false && rookRBMoved == false && pieces.get(indexPieceSelect).getX()==7 && x==4 && y==0 && pieces.get(whatPiece(4,0)).isWhite() == pieces.get(indexPieceSelect).isWhite()) {
highlight = true;
castling = true;
}
else {
castling = false;
}
}*/
}
nbOccupied = 0;
}
}
}
}
@ -1083,15 +935,12 @@ public class Board {
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;
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) {
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
highlight = true;
}
return highlight;
@ -1102,7 +951,7 @@ public class Board {
this.y = -1;
int sizeSave = previousBoard.size();
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");
Board undo = new Board(oldBoard);
this.pieces = undo.pieces;
@ -1121,7 +970,7 @@ public class Board {
}
if (sizeSave != 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");
Board undo = new Board(oldBoard);
this.pieces = undo.pieces;
@ -1142,16 +991,6 @@ public class Board {
}
public Board(ArrayList<Piece> pieces) {
/*ArrayList<Piece> 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) {
this.kingWMoved = board.kingWMoved;
this.kingBMoved = board.kingBMoved;
@ -1178,6 +1017,7 @@ public class Board {
}
public void playMove(Move move) {
Piece pieceToMove = null;
//we are searching for the piece concerned by the move returned by the autoplayer
for (int i=0; i < this.pieces.size(); i++) {
Piece piece = this.pieces.get(i);
if (piece.getX() == move.getFromX() && piece.getY() == move.getFromY()) {
@ -1185,6 +1025,7 @@ public class Board {
}
}
Piece pieceTargeted = null;
//check if there is a enemy piece on the destination of the moving piece
for (int i=0; i < this.pieces.size(); i++) {
Piece piece = this.pieces.get(i);
if (piece.getX() == move.getToX() && piece.getY() == move.getToY()) {
@ -1192,14 +1033,14 @@ public class Board {
}
}
if (pieceTargeted != null) {
this.pieces.remove(pieceTargeted);
this.pieces.remove(pieceTargeted);//the enemy piece is removed
}
pieceToMove.setX(move.getToX());
pieceToMove.setY(move.getToY());
this.turnNumber = this.turnNumber + 1;
this.x = -1;
this.y = -1;
previousBoard.add(this.toString());
previousBoard.add(this.toString());//save the move
}
}

View File

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