feat: add ai castling (Fixes #4)
All checks were successful
Linux arm64 / Build (push) Successful in 41s
All checks were successful
Linux arm64 / Build (push) Successful in 41s
This commit is contained in:
87
app/src/main/java/chess/ai/actions/AIActions.java
Normal file
87
app/src/main/java/chess/ai/actions/AIActions.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package chess.ai.actions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.Command.CommandResult;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand;
|
||||
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.GetPlayerMovesCommand;
|
||||
import chess.model.Color;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
import chess.model.pieces.Pawn;
|
||||
|
||||
public class AIActions {
|
||||
|
||||
public static List<AIAction> getAllowedActions(CommandExecutor commandExecutor) {
|
||||
List<Move> moves = getAllowedMoves(commandExecutor);
|
||||
CastlingResult castlingResult = getAllowedCastlings(commandExecutor);
|
||||
|
||||
List<AIAction> actions = new ArrayList<>(moves.size() + 10);
|
||||
|
||||
for (Move move : moves) {
|
||||
Piece movingPiece = pieceAt(move.getStart(), commandExecutor);
|
||||
if (movingPiece instanceof Pawn) {
|
||||
int enemyLineY = movingPiece.getColor() == Color.White ? 0 : 7;
|
||||
if (move.getFinish().getY() == enemyLineY) {
|
||||
PromoteType[] promotes = PromoteType.values();
|
||||
for (PromoteType promote : promotes) {
|
||||
actions.add(new AIActionMoveAndPromote(commandExecutor, move, promote));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
actions.add(new AIActionMove(commandExecutor, move));
|
||||
}
|
||||
|
||||
switch (castlingResult) {
|
||||
case Both:
|
||||
actions.add(new AIActionCastling(commandExecutor, true));
|
||||
actions.add(new AIActionCastling(commandExecutor, false));
|
||||
break;
|
||||
|
||||
case Small:
|
||||
actions.add(new AIActionCastling(commandExecutor, false));
|
||||
break;
|
||||
|
||||
case Big:
|
||||
actions.add(new AIActionCastling(commandExecutor, true));
|
||||
break;
|
||||
|
||||
case None:
|
||||
break;
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
|
||||
private static CastlingResult getAllowedCastlings(CommandExecutor commandExecutor) {
|
||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd2, commandExecutor);
|
||||
return cmd2.getCastlingResult();
|
||||
}
|
||||
|
||||
private static List<Move> getAllowedMoves(CommandExecutor commandExecutor) {
|
||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||
sendCommand(cmd, commandExecutor);
|
||||
return cmd.getMoves();
|
||||
}
|
||||
|
||||
private static CommandResult sendCommand(Command command, CommandExecutor commandExecutor) {
|
||||
CommandResult result = commandExecutor.executeCommand(command);
|
||||
assert result != CommandResult.NotAllowed : "Command not allowed!";
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Piece pieceAt(Coordinate coordinate, CommandExecutor commandExecutor) {
|
||||
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
||||
commandExecutor.executeCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user