package chess.ai; import java.util.List; import chess.controller.Command; import chess.controller.CommandExecutor; import chess.controller.Command.CommandResult; import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.GetPlayerMovesCommand; import chess.controller.commands.GetAllowedCastlingsCommand; import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult; import chess.controller.event.GameAdaptator; import chess.model.Color; import chess.model.Coordinate; import chess.model.Move; import chess.model.Piece; public abstract class AI extends GameAdaptator{ protected final CommandExecutor commandExecutor; protected final Color color; public AI(CommandExecutor commandExecutor, Color color) { this.commandExecutor = commandExecutor; this.color = color; } protected abstract void play(); protected abstract void promote(Coordinate pawnCoords); @Override public void onPlayerTurn(Color color, boolean undone) { if (this.color != color || undone) return; play(); } @Override public void onPromotePawn(Coordinate pieceCoords) { Piece pawn = pieceAt(pieceCoords); if (pawn.getColor() != this.color) return; promote(pieceCoords); } protected Piece pieceAt(Coordinate coordinate) { GetPieceAtCommand command = new GetPieceAtCommand(coordinate); sendCommand(command); return command.getPiece(); } protected List getAllowedMoves() { return getAllowedMoves(this.commandExecutor); } protected List getAllowedMoves(CommandExecutor commandExecutor) { GetPlayerMovesCommand cmd = new GetPlayerMovesCommand(); sendCommand(cmd, commandExecutor); return cmd.getMoves(); } protected CastlingResult getAllowedCastlings() { GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand(); sendCommand(cmd2); return cmd2.getCastlingResult(); } protected CommandResult sendCommand(Command command) { return sendCommand(command, this.commandExecutor); } protected CommandResult sendCommand(Command command, CommandExecutor commandExecutor) { CommandResult result = commandExecutor.executeCommand(command); if(result == CommandResult.NotAllowed){ System.out.println("eeeeee"); } return result; } }