repair castling with the rook + remove useless commented code
This commit is contained in:
parent
6cb9135501
commit
21497c2a75
|
|
@ -8,10 +8,10 @@ public class AutoPlayer {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Move computeBestMove(Board board) {
|
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
|
Board copy = new Board(board);//create a copy of the board in order not to impact the real board
|
||||||
ArrayList<Piece> alivePieces = copy.getPieces();
|
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);
|
Piece piece = alivePieces.get(i);
|
||||||
if (piece.isWhite() == copy.isTurnWhite()) {
|
if (piece.isWhite() == copy.isTurnWhite()) {
|
||||||
copy.userTouch(piece.getX(), piece.getY());
|
copy.userTouch(piece.getX(), piece.getY());
|
||||||
|
|
@ -27,10 +27,10 @@ public class AutoPlayer {
|
||||||
}
|
}
|
||||||
if (authorizedMoves.isEmpty()) {
|
if (authorizedMoves.isEmpty()) {
|
||||||
System.out.print("no move possible");
|
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());
|
int randomNum = (int)(Math.random() * authorizedMoves.size());
|
||||||
return authorizedMoves.get(randomNum);
|
return authorizedMoves.get(randomNum);//choose a random move in the authorized moves list
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,6 @@ public class Board {
|
||||||
private boolean castling;
|
private boolean castling;
|
||||||
private boolean castlingDone;
|
private boolean castlingDone;
|
||||||
private ArrayList<String> previousBoard;
|
private ArrayList<String> previousBoard;
|
||||||
//private ArrayList<Piece> altPieces;
|
|
||||||
|
|
||||||
//private boolean isChecked;
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.kingWMoved = false;
|
this.kingWMoved = false;
|
||||||
this.kingBMoved = 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
|
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
|
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 (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(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){
|
if (pieces.get(whatPiece(this.x,this.y)).isWhite()==true){
|
||||||
|
|
@ -281,9 +278,9 @@ public class Board {
|
||||||
this.y=-1;
|
this.y=-1;
|
||||||
this.turnNumber +=1;
|
this.turnNumber +=1;
|
||||||
castlingDone=true;
|
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){
|
if (pieces.get(whatPiece(x,y)).isWhite()==true){
|
||||||
kingWMoved = true;
|
kingWMoved = true;
|
||||||
}
|
}
|
||||||
|
|
@ -305,6 +302,7 @@ public class Board {
|
||||||
this.pieces.remove(whatPiece(xTwo,yTwo));
|
this.pieces.remove(whatPiece(xTwo,yTwo));
|
||||||
enPassant = false;
|
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) {
|
if (pieces.get(whatPiece(this.x, this.y)).getX() == 0 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) {
|
||||||
rookLWMoved = true;
|
rookLWMoved = true;
|
||||||
}
|
}
|
||||||
|
|
@ -315,7 +313,7 @@ public class Board {
|
||||||
rookLBMoved = true;
|
rookLBMoved = true;
|
||||||
}
|
}
|
||||||
if (pieces.get(whatPiece(this.x, this.y)).getX() == 7 && pieces.get(whatPiece(this.x, this.y)).getY() == 0) {
|
if (pieces.get(whatPiece(this.x, this.y)).getX() == 7 && pieces.get(whatPiece(this.x, this.y)).getY() == 0) {
|
||||||
rookRBMoved = true;
|
rookRBMoved = true;
|
||||||
}
|
}
|
||||||
if (pieces.get(whatPiece(this.x, this.y)).getX() == 4 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) {
|
if (pieces.get(whatPiece(this.x, this.y)).getX() == 4 && pieces.get(whatPiece(this.x, this.y)).getY() == 7) {
|
||||||
kingWMoved = true;
|
kingWMoved = true;
|
||||||
|
|
@ -472,10 +470,6 @@ public class Board {
|
||||||
this.castlingDone = true;
|
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
|
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;
|
boolean spotOccupied = false;
|
||||||
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
|
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
|
||||||
|
|
@ -621,47 +615,10 @@ public class Board {
|
||||||
}
|
}
|
||||||
i += 1;
|
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;
|
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<> ();
|
ArrayList<ArrayList<Integer>> toHighlight = new ArrayList<> ();
|
||||||
toHighlight.add(new ArrayList<>());
|
toHighlight.add(new ArrayList<>());
|
||||||
toHighlight.add(new ArrayList<>());
|
toHighlight.add(new ArrayList<>());
|
||||||
|
|
@ -758,7 +715,7 @@ public class Board {
|
||||||
int nbOccupied = 0;
|
int nbOccupied = 0;
|
||||||
boolean occupiedLeftLine = true;
|
boolean occupiedLeftLine = true;
|
||||||
boolean occupiedRightLine = true;
|
boolean occupiedRightLine = true;
|
||||||
|
//check if the castling is possible with the rooks on the left
|
||||||
for(int i1 = 3; i1 > 0; i1--) {
|
for(int i1 = 3; i1 > 0; i1--) {
|
||||||
if (positionOccupied(i1,pieces.get(indexPieceSelect).getY()) == true) {
|
if (positionOccupied(i1,pieces.get(indexPieceSelect).getY()) == true) {
|
||||||
nbOccupied = nbOccupied + 1;
|
nbOccupied = nbOccupied + 1;
|
||||||
|
|
@ -767,7 +724,7 @@ public class Board {
|
||||||
if (nbOccupied == 0) {
|
if (nbOccupied == 0) {
|
||||||
occupiedLeftLine=false;
|
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()) {
|
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(0).add(x);
|
||||||
toHighlight.get(1).add(y);
|
toHighlight.get(1).add(y);
|
||||||
|
|
@ -777,12 +734,10 @@ public class Board {
|
||||||
toHighlight.get(0).add(x);
|
toHighlight.get(0).add(x);
|
||||||
toHighlight.get(1).add(y);
|
toHighlight.get(1).add(y);
|
||||||
castling = true;
|
castling = true;
|
||||||
// }
|
|
||||||
//else {
|
|
||||||
//castling = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nbOccupied = 0;
|
nbOccupied = 0;
|
||||||
|
//check if the castling is possible with the rooks on the right
|
||||||
for(int i2 = 5; i2 < 7; i2++) {
|
for(int i2 = 5; i2 < 7; i2++) {
|
||||||
if (positionOccupied(i2,pieces.get(indexPieceSelect).getY()) == true) {
|
if (positionOccupied(i2,pieces.get(indexPieceSelect).getY()) == true) {
|
||||||
nbOccupied = nbOccupied + 1;
|
nbOccupied = nbOccupied + 1;
|
||||||
|
|
@ -791,7 +746,7 @@ public class Board {
|
||||||
if (nbOccupied == 0) {
|
if (nbOccupied == 0) {
|
||||||
occupiedRightLine=false;
|
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()) {
|
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(0).add(x);
|
||||||
toHighlight.get(1).add(y);
|
toHighlight.get(1).add(y);
|
||||||
|
|
@ -802,9 +757,6 @@ public class Board {
|
||||||
toHighlight.get(1).add(y);
|
toHighlight.get(1).add(y);
|
||||||
castling = true;
|
castling = true;
|
||||||
}
|
}
|
||||||
// else {
|
|
||||||
// castling = false;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
nbOccupied = 0;
|
nbOccupied = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -924,6 +876,57 @@ 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;
|
||||||
}
|
}
|
||||||
|
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 (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;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
castling = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 && 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -987,16 +990,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) {
|
public Board(Board board) {
|
||||||
this.kingWMoved = board.kingWMoved;
|
this.kingWMoved = board.kingWMoved;
|
||||||
|
|
@ -1024,6 +1017,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
Piece pieceToMove = null;
|
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++) {
|
for (int i=0; i < this.pieces.size(); i++) {
|
||||||
Piece piece = this.pieces.get(i);
|
Piece piece = this.pieces.get(i);
|
||||||
if (piece.getX() == move.getFromX() && piece.getY() == move.getFromY()) {
|
if (piece.getX() == move.getFromX() && piece.getY() == move.getFromY()) {
|
||||||
|
|
@ -1031,6 +1025,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Piece pieceTargeted = null;
|
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++) {
|
for (int i=0; i < this.pieces.size(); i++) {
|
||||||
Piece piece = this.pieces.get(i);
|
Piece piece = this.pieces.get(i);
|
||||||
if (piece.getX() == move.getToX() && piece.getY() == move.getToY()) {
|
if (piece.getX() == move.getToX() && piece.getY() == move.getToY()) {
|
||||||
|
|
@ -1038,14 +1033,14 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pieceTargeted != null) {
|
if (pieceTargeted != null) {
|
||||||
this.pieces.remove(pieceTargeted);
|
this.pieces.remove(pieceTargeted);//the enemy piece is removed
|
||||||
}
|
}
|
||||||
pieceToMove.setX(move.getToX());
|
pieceToMove.setX(move.getToX());
|
||||||
pieceToMove.setY(move.getToY());
|
pieceToMove.setY(move.getToY());
|
||||||
this.turnNumber = this.turnNumber + 1;
|
this.turnNumber = this.turnNumber + 1;
|
||||||
this.x = -1;
|
this.x = -1;
|
||||||
this.y = -1;
|
this.y = -1;
|
||||||
previousBoard.add(this.toString());
|
previousBoard.add(this.toString());//save the move
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue