autoplayer

This commit is contained in:
Lucie 2025-05-21 02:49:12 +02:00
parent b8340ede02
commit 5a80488b2f
4 changed files with 96 additions and 15 deletions

View File

@ -1,16 +1,74 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer {
Move move;
Piece[][] board1;
boolean isWhite;
Random rand;
int randomX;
int randomY;
MoveCalculator moveCalc;
Piece piece;
PieceType type;
boolean[] bool;
boolean enPassant;
boolean rookLeft;
boolean rookRight;
boolean king;
int pawnX;
int pawnY;
ArrayList<int[]> legalMoves;
int randomMove;
int[] moveSelected;
int turnNumber;
/**
* returns the best Move to try on provided board for active player
* @param board
* @return
*/
public Move computeBestMove(Board board) {
return null;
public void computeBestMove(Board board) {
legalMoves = new ArrayList<>();
board1 = board.getBoard(board);
isWhite = board.isTurnWhite();
rand = new Random();
System.out.println(board.toString());
boolean ok = true;
while (legalMoves.size() == 0) {
randomX = rand.nextInt(8);
randomY = rand.nextInt(8);
while (board1[randomY][randomX] == null || board1[randomY][randomX].isWhite() != isWhite) {
randomX = rand.nextInt(8);
randomY = rand.nextInt(8);
}
moveCalc = new MoveCalculator(board1);
piece = board1[randomY][randomX];
type = board1[randomY][randomX].getType();
bool = board.getBooleans();
enPassant = bool[0];
rookLeft = true;
rookRight = true;
king = true;
if (isWhite) {
rookLeft = bool[1];
rookRight = bool[2];
king = bool[5];
}
else {
rookLeft = bool[3];
rookRight = bool[4];
king = bool[6];
}
pawnX = board.getPawnX();
pawnY = board.getPawnY();
legalMoves = moveCalc.getMove(type, isWhite, randomX, randomY, king, rookRight, rookLeft, enPassant, pawnX, pawnY);
}
board.userTouch(randomX, randomY);
randomX = rand.nextInt(8);
randomY = rand.nextInt(8);
while (board.isHighlighted(randomX, randomY)==false) {
randomX = rand.nextInt(8);
randomY = rand.nextInt(8);
}
board.userTouch(randomX, randomY);
}

View File

@ -76,9 +76,24 @@ public class Board {
return check.isCheckmate(whiteTurn);
}
public Piece[][] getBoard(Board board1) {
System.out.println(board1.board);
return board1.board;
}
public boolean[] getBooleans() {
boolean[] bool = {enPassant, whiteRookLeft, whiteRookRight, blackRookLeft, blackRookRight, whiteKing, blackKing};
return bool;
}
public int getPawnX() {
return pawnX;
}
public int getPawnY() {
return pawnY;
}
// INITIALISE THE BOARD
public void initialise() {
public void initialise() {
this.x = -1;
this.y = -1;
turns = 0;
@ -98,6 +113,14 @@ public class Board {
states = new ArrayList<>();
}
public void setTurnNumber(int turn) {
this.turns = turn;
}
public void setTurn(boolean white) {
this.whiteTurn = white;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[y][x] = new Piece(isWhite, type, x, y);
}
@ -469,7 +492,7 @@ public class Board {
board[move.getFromY()][move.getFromX()].setY(move.getToY());
board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()];
board[move.getFromY()][move.getFromX()] = null;
promotion(x, y);
promotion(move.getToX(), move.getToY());
}
private void promotion(int x, int y) {

View File

@ -49,7 +49,7 @@ public class Game extends Thread {
private void aiPlayerTurn() {
if(isAITurn()) {
board.playMove(aiPlayer.computeBestMove(new Board(board)));
aiPlayer.computeBestMove(board);
}
}

View File

@ -17,8 +17,8 @@ public class MoveCalculator {
private int enPassantY;
ArrayList<int[]> moves;
public MoveCalculator(Piece[][] boardF) {
board = boardF;
public MoveCalculator(Piece[][] boardI) {
board = boardI;
knightMoves = new int[][] {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
kingMoves = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}, {2, 0}, {-2, 0}};
whitePawn = new int[][] {{6}, {0, -1}, {1, -1}, {-1, -1}, {0, -2}};