From 30eb12145d024a81c23523f7b0d71cc9798bb63b Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 17 Apr 2025 08:53:21 +0200 Subject: [PATCH] add hungry ai --- app/src/main/java/chess/SwingMain.java | 3 +- app/src/main/java/chess/ai/HungryAI.java | 61 +++++++++++++++++++++++ app/src/main/java/chess/ai/PieceCost.java | 60 ++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/chess/ai/HungryAI.java create mode 100644 app/src/main/java/chess/ai/PieceCost.java diff --git a/app/src/main/java/chess/SwingMain.java b/app/src/main/java/chess/SwingMain.java index 1c8822c..ab5ce8b 100644 --- a/app/src/main/java/chess/SwingMain.java +++ b/app/src/main/java/chess/SwingMain.java @@ -1,6 +1,7 @@ package chess; import chess.ai.DumbAI; +import chess.ai.HungryAI; import chess.controller.CommandExecutor; import chess.controller.commands.NewGameCommand; import chess.controller.event.GameAdaptator; @@ -20,7 +21,7 @@ public class SwingMain { DumbAI ai = new DumbAI(commandExecutor, Color.Black); commandExecutor.addListener(ai); - DumbAI ai2 = new DumbAI(commandExecutor, Color.White); + HungryAI ai2 = new HungryAI(commandExecutor, Color.White); commandExecutor.addListener(ai2); commandExecutor.addListener(new GameAdaptator(){ diff --git a/app/src/main/java/chess/ai/HungryAI.java b/app/src/main/java/chess/ai/HungryAI.java new file mode 100644 index 0000000..7c1cb54 --- /dev/null +++ b/app/src/main/java/chess/ai/HungryAI.java @@ -0,0 +1,61 @@ +package chess.ai; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import chess.controller.CommandExecutor; +import chess.controller.commands.MoveCommand; +import chess.controller.commands.PromoteCommand; +import chess.controller.commands.PromoteCommand.PromoteType; +import chess.model.Color; +import chess.model.Coordinate; +import chess.model.Move; +import chess.model.Piece; + +public class HungryAI extends AI { + + private final PieceCost pieceCost; + private final Random random; + + public HungryAI(CommandExecutor commandExecutor, Color color) { + super(commandExecutor, color); + this.pieceCost = new PieceCost(color); + this.random = new Random(); + } + + private int getMoveCost(Move move) { + Piece piece = pieceAt(move.getDeadPieceCoords()); + return pieceCost.getCost(piece); + } + + private List getBestMoves() { + List moves = getAllowedMoves(); + List bestMoves = new ArrayList<>(); + int bestCost = 0; + for (Move move : moves) { + int moveCost = getMoveCost(move); + if (moveCost == bestCost) { + bestMoves.add(move); + } else if (moveCost > bestCost) { + bestMoves.clear(); + bestMoves.add(move); + bestCost = moveCost; + } + } + return bestMoves; + } + + @Override + protected void play() { + List bestMoves = getBestMoves(); + int randomMove = this.random.nextInt(bestMoves.size()); + this.commandExecutor.executeCommand(new MoveCommand(bestMoves.get(randomMove))); + } + + @Override + protected void promote(Coordinate pawnCoords) { + sendCommand(new PromoteCommand(PromoteType.Queen)); + } + +} diff --git a/app/src/main/java/chess/ai/PieceCost.java b/app/src/main/java/chess/ai/PieceCost.java new file mode 100644 index 0000000..61a9c4f --- /dev/null +++ b/app/src/main/java/chess/ai/PieceCost.java @@ -0,0 +1,60 @@ +package chess.ai; + +import chess.model.Color; +import chess.model.Piece; +import chess.model.PieceVisitor; +import chess.model.pieces.Bishop; +import chess.model.pieces.King; +import chess.model.pieces.Knight; +import chess.model.pieces.Pawn; +import chess.model.pieces.Queen; +import chess.model.pieces.Rook; + +public class PieceCost implements PieceVisitor { + + private final Color player; + + public PieceCost(Color color) { + this.player = color; + } + + public int getCost(Piece piece) { + if (piece == null) + return 0; + int cost = visit(piece); + if (piece.getColor() == player) + cost = -cost; + return cost; + } + + @Override + public Integer visitPiece(Bishop bishop) { + return 3; + } + + @Override + public Integer visitPiece(King king) { + return 90; + } + + @Override + public Integer visitPiece(Knight knight) { + return 3; + } + + @Override + public Integer visitPiece(Pawn pawn) { + return 1; + } + + @Override + public Integer visitPiece(Queen queen) { + return 9; + } + + @Override + public Integer visitPiece(Rook rook) { + return 5; + } + +}