auto player random ok
This commit is contained in:
parent
1a7d19d70d
commit
49878e9bd2
|
|
@ -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<Move> authorizedMoves = new ArrayList<>();
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Piece> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,4 +64,5 @@ public class Piece {
|
|||
return color;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue