diff --git a/app/src/main/java/chess/ai/DumbAI.java b/app/src/main/java/chess/ai/DumbAI.java index df0bbf9..0e52503 100644 --- a/app/src/main/java/chess/ai/DumbAI.java +++ b/app/src/main/java/chess/ai/DumbAI.java @@ -5,10 +5,13 @@ import java.util.Random; import chess.controller.Command; import chess.controller.CommandExecutor; +import chess.controller.commands.CastlingCommand; +import chess.controller.commands.GetAllowedCastlingsCommand; import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.GetPlayerMovesCommand; import chess.controller.commands.MoveCommand; import chess.controller.commands.PromoteCommand; +import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult; import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.event.GameAdaptator; import chess.model.Color; @@ -34,9 +37,46 @@ public class DumbAI extends GameAdaptator { GetPlayerMovesCommand cmd = new GetPlayerMovesCommand(); sendCommand(cmd); + + GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand(); + sendCommand(cmd2); + + CastlingResult castlings = cmd2.getCastlingResult(); List moves = cmd.getMoves(); + + switch (castlings) { + case Both: { + int randomMove = this.random.nextInt(moves.size() + 2); + if (randomMove < moves.size() - 2) + break; + this.commandExecutor.executeCommand(new CastlingCommand(randomMove == moves.size())); + return; + } + + case Small: { + int randomMove = this.random.nextInt(moves.size() + 1); + if (randomMove != moves.size()) + break; + this.commandExecutor.executeCommand(new CastlingCommand(false)); + return; + + } + + case Big: { + int randomMove = this.random.nextInt(moves.size() + 1); + if (randomMove != moves.size()) + break; + this.commandExecutor.executeCommand(new CastlingCommand(true)); + return; + } + + default: + break; + } + int randomMove = this.random.nextInt(moves.size()); this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove))); + } @Override @@ -50,10 +90,10 @@ public class DumbAI extends GameAdaptator { } private Piece pieceAt(Coordinate coordinate) { - GetPieceAtCommand command = new GetPieceAtCommand(coordinate); - sendCommand(command); - return command.getPiece(); - } + GetPieceAtCommand command = new GetPieceAtCommand(coordinate); + sendCommand(command); + return command.getPiece(); + } private void sendCommand(Command command) { this.commandExecutor.executeCommand(command);