auto player in progress

This commit is contained in:
mariettakazimierczak 2025-05-20 19:22:18 +02:00
parent 7509196100
commit ca414277ec
5 changed files with 133 additions and 30 deletions

View File

@ -1,5 +1,9 @@
package backend; package backend;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AutoPlayer { public class AutoPlayer {
@ -9,9 +13,29 @@ public class AutoPlayer {
* @return * @return
*/ */
public Move computeBestMove(Board board) { public Move computeBestMove(Board board) {
List<Move> allPossibleMoves = new ArrayList<>();
boolean isWhiteTurn = board.isTurnWhite();
return null; for (Piece piece : board.getPieces()) {
if (piece.isWhite() == isWhiteTurn) {
board.userTouch(piece.getX(), piece.getY()); // triggers highlighting
for (int newX = 0; newX < board.getWidth(); newX++) {
for (int newY = 0; newY < board.getHeight(); newY++) {
if (board.isHighlighted(newX, newY)) {
allPossibleMoves.add(new Move(piece.getX(), piece.getY(), newX, newY, piece));
}
}
}
}
}
if (allPossibleMoves.isEmpty()) return null;
Random rand = new Random();
return allPossibleMoves.get(rand.nextInt(allPossibleMoves.size()));
}
} }
}

View File

@ -308,10 +308,10 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
private void highlightedPossibleMoves(Piece piece) { /*private void highlightedPossibleMoves(Piece piece) {
highlightedSquares.clear(); highlightedSquares.clear();
highlightedSquares.addAll(MoveHighlighter.getPossibleMoves(piece, this)); highlightedSquares.addAll(MoveHighlighter.getPossibleMoves(piece, this));
} }*/
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
for(int i=0; i<highlightedSquares.size();i++) { //loops through list of highlighted squares for(int i=0; i<highlightedSquares.size();i++) { //loops through list of highlighted squares
@ -393,14 +393,79 @@ return false; }
}*/ }*/
public void playMove(Move move) { public void playMove(Move move) {
//TODO if (move == null || move.getPieceMoved() == null) return;
int fromX = move.getOldX();
int fromY = move.getOldY();
int toX = move.getNewX();
int toY = move.getNewY();
Piece piece = board[fromX][fromY];
if (piece == null) return;
// Capture any piece at destination
board[toX][toY] = null;
// Move piece
board[fromX][fromY] = null;
board[toX][toY] = piece;
piece.setPosition(toX, toY); // <-- make sure Piece has this method
// Update turn
isTurnWhite = !isTurnWhite;
turnNumber++;
// Clear selection/highlight
chosenPiece = null;
highlightedSquares.clear();
} }
/** Expose the raw 2D array for testing or drawing. */ /** Expose the raw 2D array for testing or drawing. */
public Piece[][] getBoardArray() { public Piece[][] getBoardArray() {
return board; return board;
} }
public void highlightedPossibleMoves(Piece piece) {
highlightedSquares.clear();
MovePiece move = new MovePiece(piece, this);
int x = piece.getX();
int y = piece.getY();
for (int newX = 0; newX < width; newX++) {
for (int newY = 0; newY < height; newY++) {
boolean isValid = false;
switch (piece.getType()) {
case Pawn:
isValid = move.movePawnSimulate(newX, newY);
break;
case Rook:
isValid = move.moveRookSimulate(newX, newY);
break;
case Knight:
isValid = move.moveKnightSimulate(newX, newY);
break;
case Bishop:
isValid = move.moveBishopSimulate(newX, newY);
break;
case Queen:
isValid = move.moveQueenSimulate(newX, newY);
break;
case King:
isValid = move.moveKingSimulate(newX, newY);
break;
}
if (isValid) {
highlightedSquares.add(new int[]{newX, newY});
}
}
}
}

View File

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

View File

@ -245,6 +245,8 @@ public class MovePiece {
}return false; }return false;
} }
//bishop movement simulate //bishop movement simulate
public boolean moveBishopSimulate(int x, int y) { public boolean moveBishopSimulate(int x, int y) {
int currentX = piece.getX(); int currentX = piece.getX();

View File

@ -50,4 +50,9 @@ public class Piece {
didMove=value; didMove=value;
} }
public void setPosition(int x, int y) {
this.x_coor = x;
this.y_coor = y;
}
} }