ai castling

This commit is contained in:
2025-04-16 11:25:05 +02:00
parent ebb013cae5
commit f54ddc1f59

View File

@@ -5,10 +5,13 @@ import java.util.Random;
import chess.controller.Command; import chess.controller.Command;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedCastlingsCommand;
import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.GetPieceAtCommand;
import chess.controller.commands.GetPlayerMovesCommand; import chess.controller.commands.GetPlayerMovesCommand;
import chess.controller.commands.MoveCommand; import chess.controller.commands.MoveCommand;
import chess.controller.commands.PromoteCommand; import chess.controller.commands.PromoteCommand;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import chess.controller.commands.PromoteCommand.PromoteType; import chess.controller.commands.PromoteCommand.PromoteType;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.Color; import chess.model.Color;
@@ -34,9 +37,46 @@ public class DumbAI extends GameAdaptator {
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand(); GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
sendCommand(cmd); sendCommand(cmd);
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
sendCommand(cmd2);
CastlingResult castlings = cmd2.getCastlingResult();
List<Move> moves = cmd.getMoves(); List<Move> 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()); int randomMove = this.random.nextInt(moves.size());
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove))); this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
} }
@Override @Override
@@ -50,10 +90,10 @@ public class DumbAI extends GameAdaptator {
} }
private Piece pieceAt(Coordinate coordinate) { private Piece pieceAt(Coordinate coordinate) {
GetPieceAtCommand command = new GetPieceAtCommand(coordinate); GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
sendCommand(command); sendCommand(command);
return command.getPiece(); return command.getPiece();
} }
private void sendCommand(Command command) { private void sendCommand(Command command) {
this.commandExecutor.executeCommand(command); this.commandExecutor.executeCommand(command);