diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index a988a22..ca21b73 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -1,17 +1,39 @@ package backend; - +import java.util.ArrayList; public class AutoPlayer { - /** * returns the best Move to try on provided board for active player * @param board * @return */ public Move computeBestMove(Board board) { - - return null; + ArrayList authorizedMoves = new ArrayList<>(); + Board copy = new Board(board);//create a copy of the board in order not to impact the real board + ArrayList 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 + Piece piece = alivePieces.get(i); + if (piece.isWhite() == copy.isTurnWhite()) { + copy.userTouch(piece.getX(), piece.getY()); + for (int x = 0; x < copy.getWidth(); x++) { + for (int y = 0; y < copy.getHeight(); y++) { + if (copy.isHighlighted(x, y)) {//if it's highlight, it is because it is a possible move so we add it to the list of possible move + authorizedMoves.add(new Move(piece.getX(), piece.getY(), x, y)); + } + } + } + copy.userTouch(piece.getX(), piece.getY());//reset the usertouch to avoid moving pieces in the copied board + } + } + if (authorizedMoves.isEmpty()) { + System.out.print("no move possible"); + return null;//possibility of being checked, must work on it later + } + int randomNum = (int)(Math.random() * authorizedMoves.size()); + return authorizedMoves.get(randomNum); } - - } + + + + diff --git a/src/backend/Board.java b/src/backend/Board.java index 2694fdb..b928c48 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -1252,12 +1252,55 @@ public class Board { } public Board(Board board) { - this.pieces = board.getPieces(); + this.kingWMoved = board.kingWMoved; + this.kingBMoved = board.kingBMoved; + this.rookLWMoved = board.rookLWMoved; + this.rookRWMoved = board.rookRWMoved; + this.rookLBMoved = board.rookLBMoved; + this.rookRBMoved = board.rookRBMoved; + this.castling = board.castling; + this.castlingDone = board.castlingDone; + this.colNum = board.colNum; + this.lineNum = board.lineNum; + this.pieces = new ArrayList<>(); + this.x = board.x; + this.y = board.y; + this.turnNumber = board.turnNumber; + this.enPassant = board.enPassant; + this.lastTurnPawnTwo = board.lastTurnPawnTwo; + + ArrayList Pieces = board.getPieces(); + for (int i=0; i < Pieces.size(); i++) { + Piece piece = Pieces.get(i); + this.pieces.add(new Piece(piece)); + } } - public void playMove(Move move) { - //TODO - + Piece pieceToMove = null; + for (int i=0; i < this.pieces.size(); i++) { + Piece piece = this.pieces.get(i); + if (piece.getX() == move.getFromX() && piece.getY() == move.getFromY()) { + pieceToMove = piece; + } + } + if (pieceToMove == null) { + return; + } + Piece pieceTargeted = null; + for (int i=0; i < this.pieces.size(); i++) { + Piece piece = this.pieces.get(i); + if (piece.getX() == move.getToX() && piece.getY() == move.getToY()) { + pieceTargeted = piece; + } + } + if (pieceTargeted != null) { + this.pieces.remove(pieceTargeted); + } + pieceToMove.setX(move.getToX()); + pieceToMove.setY(move.getToY()); + this.turnNumber = this.turnNumber + 1; + this.x = -1; + this.y = -1; } } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index d5c0c9d..8137867 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -64,4 +64,5 @@ public class Piece { return color; } + }