63 lines
1.8 KiB
Java
63 lines
1.8 KiB
Java
package chess.ai;
|
|
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
import chess.controller.Command;
|
|
import chess.controller.CommandExecutor;
|
|
import chess.controller.commands.GetPieceAtCommand;
|
|
import chess.controller.commands.GetPlayerMovesCommand;
|
|
import chess.controller.commands.MoveCommand;
|
|
import chess.controller.commands.PromoteCommand;
|
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
|
import chess.controller.event.GameAdaptator;
|
|
import chess.model.Color;
|
|
import chess.model.Coordinate;
|
|
import chess.model.Move;
|
|
import chess.model.Piece;
|
|
|
|
public class DumbAI extends GameAdaptator {
|
|
|
|
private final Color player;
|
|
private final CommandExecutor commandExecutor;
|
|
private final Random random = new Random();
|
|
|
|
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
|
this.player = color;
|
|
this.commandExecutor = commandExecutor;
|
|
}
|
|
|
|
@Override
|
|
public void playerTurn(Color color) {
|
|
if (color != player)
|
|
return;
|
|
|
|
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
|
sendCommand(cmd);
|
|
List<Move> moves = cmd.getMoves();
|
|
int randomMove = this.random.nextInt(moves.size());
|
|
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
|
}
|
|
|
|
@Override
|
|
public void promotePawn(Coordinate pieceCoords) {
|
|
Piece pawn = pieceAt(pieceCoords);
|
|
if (pawn.getColor() != this.player)
|
|
return;
|
|
|
|
int promote = this.random.nextInt(PromoteType.values().length);
|
|
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
|
|
}
|
|
|
|
private Piece pieceAt(Coordinate coordinate) {
|
|
GetPieceAtCommand command = new GetPieceAtCommand(coordinate);
|
|
sendCommand(command);
|
|
return command.getPiece();
|
|
}
|
|
|
|
private void sendCommand(Command command) {
|
|
this.commandExecutor.executeCommand(command);
|
|
}
|
|
|
|
}
|