diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 3069545..fc42517 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,4 +1,5 @@ eclipse.preferences.version=1 encoding//src/Main.java=UTF-8 +encoding//src/backend/AutoPlayer.java=UTF-8 encoding//src/backend/Board.java=UTF-8 encoding/=windows-1252 diff --git a/src/Main.java b/src/Main.java index 277f651..402205c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,7 +4,7 @@ import backend.Piece; import backend.PieceType; import windowInterface.MyInterface; - + public class Main { @@ -30,7 +30,6 @@ public class Main { } } - - + diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index a988a22..0d43701 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -1,17 +1,64 @@ package backend; +import java.util.ArrayList; +import java.util.Random; + 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; - } - - -} + + public Move computeBestMove(Board board) { + boolean isWhite = board.isTurnWhite(); + ArrayList allMoves = new ArrayList<>(); + ArrayList pieces = board.getPieces(); + + for (int i = 0; i < pieces.size(); i++) { + Piece piece = pieces.get(i); + + if (piece.isWhite() == isWhite) { + ArrayList moves = board.getValidMoves(piece); + + for (int j = 0; j < moves.size(); j++) { + int[] moveCoords = moves.get(j); + int toX = moveCoords[0]; + int toY = moveCoords[1]; + Piece captured = board.getPieceAt(toX, toY); + + Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured); + allMoves.add(move); + } + } + } + + if (allMoves.isEmpty()) { + return null; // no legal moves → maybe stalemate or checkmate + } + + // === Basic Evaluation: Prefer capturing higher-value pieces === + Move bestMove = allMoves.get(0); + int bestScore = evaluateCapture(bestMove.getCaptured()); + + for (int i = 1; i < allMoves.size(); i++) { + Move move = allMoves.get(i); + int score = evaluateCapture(move.getCaptured()); + + if (score > bestScore) { + bestMove = move; + bestScore = score; + } + } + + return bestMove; + } + + // Simple evaluation: return value of captured piece, or 0 if no capture + private int evaluateCapture(Piece captured) { + if (captured == null) return 0; + + PieceType type = captured.getType(); + + if (type == PieceType.Pawn) return 1; + if (type == PieceType.Knight || type == PieceType.Bishop) return 3; + if (type == PieceType.Rook) return 5; + if (type == PieceType.Queen) return 9; + return 0; // king shouldn't be captured anyway + } +} \ No newline at end of file diff --git a/src/backend/Board.java b/src/backend/Board.java index d6c1148..d37e254 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -282,7 +282,41 @@ public class Board { } public void playMove(Move move) { - //TODO + if (move == null) return; + + Piece piece = move.getPiece(); + int toX = move.getToX(); + int toY = move.getToY(); + + // Remove captured piece if any + Piece captured = move.getCaptured(); + if (captured != null) { + pieces.remove(captured); + } + + // Move the piece + piece.setX(toX); + piece.setY(toY); + + // Switch turn + isWhiteTurn = !isWhiteTurn; + turnNumber++; + + // Clear selection and highlights (if needed in GUI) + selectedX = -1; + selectedY = -1; + highlightedSquares.clear(); + + // Check status after move + for (int i = 0; i < 2; i++) { + boolean white = (i == 0); + if (isKingInCheck(white)) { + System.out.println((white ? "White" : "Black") + " is in check!"); + if (isCheckmate(white)) { + System.out.println((white ? "White" : "Black") + " is in checkmate!"); + } + } + } } diff --git a/src/backend/Game.java b/src/backend/Game.java index 7ab9929..1f3eff6 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -23,7 +23,9 @@ public class Game extends Thread { aiPlayer = new AutoPlayer(); } - + public Board getBoard() { + return board; + } public int getWidth() { return board.getWidth(); diff --git a/src/backend/Move.java b/src/backend/Move.java index 86d7ed1..99849ed 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -1,9 +1,43 @@ package backend; - public class Move { - + private Piece piece; + private int fromX; + private int fromY; + private int toX; + private int toY; + private Piece captured; // can be null + + public Move(Piece piece, int fromX, int fromY, int toX, int toY, Piece captured) { + this.piece = piece; + this.fromX = fromX; + this.fromY = fromY; + this.toX = toX; + this.toY = toY; + this.captured = captured; } - - - \ No newline at end of file + + public Piece getPiece() { + return piece; + } + + public int getFromX() { + return fromX; + } + + public int getFromY() { + return fromY; + } + + public int getToX() { + return toX; + } + + public int getToY() { + return toY; + } + + public Piece getCaptured() { + return captured; + } +} \ No newline at end of file diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index b7e2549..f19a59a 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -46,6 +46,11 @@ public class MyInterface extends JFrame { /** * Create the frame. */ + + public void setGame(Game g) { + this.game = g; + } + public MyInterface() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(10, 10, 650, 650);