un-finish AI implementation

This commit is contained in:
PIRANUT_PHLANG 2025-05-13 12:12:30 +02:00
parent 9d64fad05f
commit 20643dfd75
3 changed files with 39 additions and 6 deletions

View File

@ -3,8 +3,16 @@ import backend.Move;
import backend.Piece; import backend.Piece;
import backend.PieceType; import backend.PieceType;
import windowInterface.MyInterface; import windowInterface.MyInterface;
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
public class Main { public class Main {

View File

@ -1,5 +1,8 @@
package backend; package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer { public class AutoPlayer {
@ -8,9 +11,30 @@ public class AutoPlayer {
* @param board * @param board
* @return * @return
*/ */
public Move computeBestMove(Board board) { public Move computeBestMove(Board board) {ArrayList<Move> possibleMoves = new ArrayList<>();
boolean isWhiteTurn = board.isTurnWhite();
// Get all legal moves for all pieces of the current player
for (Piece piece : board.getPieces()) {
if (piece.isWhite() == isWhiteTurn) {
ArrayList<int[]> legalPositions = board.getLegalMoves(piece);
for (int[] pos : legalPositions) {
Piece target = board.getPieceAt(pos[0], pos[1]);
Move move = new Move(piece, piece.getX(), piece.getY(), pos[0], pos[1], target);
possibleMoves.add(move);
}
}
}
// If there are no moves available, return null
if (possibleMoves.isEmpty()) {
return null;
}
// Pick one move randomly
Random rand = new Random();
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
return null;
} }

View File

@ -148,7 +148,7 @@ public class Board {
} }
private ArrayList<int[]> getLegalMoves(Piece piece) { public ArrayList<int[]> getLegalMoves(Piece piece) {
ArrayList<int[]> moves = new ArrayList<>(); ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX(), y = piece.getY(); int x = piece.getX(), y = piece.getY();
int dir = piece.isWhite() ? 1 : -1; int dir = piece.isWhite() ? 1 : -1;
@ -266,11 +266,12 @@ public class Board {
} }
public Board(String[] array) { public Board(String[] array) {
this.pieces = new ArrayList<>();
//TODO //TODO
} }
private Piece getPieceAt(int x, int y) { public Piece getPieceAt(int x, int y) {
for (Piece piece : pieces) { for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) { if (piece.getX() == x && piece.getY() == y) {
return piece; return piece;