add general ai
This commit is contained in:
68
app/src/main/java/chess/ai/AI.java
Normal file
68
app/src/main/java/chess/ai/AI.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package chess.ai;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
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<Move> getAllowedMoves() {
|
||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||
sendCommand(cmd);
|
||||
return cmd.getMoves();
|
||||
}
|
||||
|
||||
protected CastlingResult getAllowedCastlings() {
|
||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd2);
|
||||
return cmd2.getCastlingResult();
|
||||
}
|
||||
|
||||
protected void sendCommand(Command command) {
|
||||
this.commandExecutor.executeCommand(command);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,46 +3,28 @@ package chess.ai;
|
||||
import java.util.List;
|
||||
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.GetAllowedCastlingsCommand.CastlingResult;
|
||||
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;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
|
||||
public class DumbAI extends GameAdaptator {
|
||||
public class DumbAI extends AI {
|
||||
|
||||
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;
|
||||
super(commandExecutor, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {
|
||||
if (color != player)
|
||||
return;
|
||||
|
||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
||||
sendCommand(cmd);
|
||||
|
||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
||||
sendCommand(cmd2);
|
||||
|
||||
CastlingResult castlings = cmd2.getCastlingResult();
|
||||
List<Move> moves = cmd.getMoves();
|
||||
protected void play() {
|
||||
CastlingResult castlings = getAllowedCastlings();
|
||||
List<Move> moves = getAllowedMoves();
|
||||
|
||||
switch (castlings) {
|
||||
case Both: {
|
||||
@@ -76,27 +58,12 @@ public class DumbAI extends GameAdaptator {
|
||||
|
||||
int randomMove = this.random.nextInt(moves.size());
|
||||
this.commandExecutor.executeCommand(new MoveCommand(moves.get(randomMove)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPromotePawn(Coordinate pieceCoords) {
|
||||
Piece pawn = pieceAt(pieceCoords);
|
||||
if (pawn.getColor() != this.player)
|
||||
return;
|
||||
|
||||
protected void promote(Coordinate pawnCoords) {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ public class CommandExecutor {
|
||||
this.dispatcher.onGameEnd();
|
||||
return;
|
||||
}
|
||||
switchPlayerTurn();
|
||||
switchPlayerTurn(command instanceof UndoCommand);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void switchPlayerTurn() {
|
||||
private void switchPlayerTurn(boolean undone) {
|
||||
this.game.switchPlayerTurn();
|
||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn());
|
||||
this.dispatcher.onPlayerTurn(this.game.getPlayerTurn(), undone);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,7 +51,7 @@ public class NewGameCommand extends Command {
|
||||
game.reset();
|
||||
|
||||
outputSystem.onGameStart();
|
||||
outputSystem.onPlayerTurn(game.getPlayerTurn());
|
||||
outputSystem.onPlayerTurn(game.getPlayerTurn(), false);
|
||||
|
||||
return CommandResult.NotMoved;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {
|
||||
public void onPlayerTurn(Color color, boolean undone) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,7 +7,7 @@ import chess.model.Move;
|
||||
public abstract class GameAdaptator implements GameListener {
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {}
|
||||
public void onPlayerTurn(Color color, boolean undone) {}
|
||||
|
||||
@Override
|
||||
public void onWin(Color color) {}
|
||||
|
||||
@@ -29,8 +29,8 @@ public class GameDispatcher implements GameListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {
|
||||
asyncForEachCall((l) -> l.onPlayerTurn(color));
|
||||
public void onPlayerTurn(Color color, boolean undone) {
|
||||
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,8 +56,9 @@ public interface GameListener {
|
||||
/**
|
||||
* Invoked when it's the player turn
|
||||
* @param color the color of the player who should play
|
||||
* @param undone true if it's a result of an undo command
|
||||
*/
|
||||
void onPlayerTurn(Color color);
|
||||
void onPlayerTurn(Color color, boolean undone);
|
||||
|
||||
/**
|
||||
* Invoked when a pawn should be promoted
|
||||
|
||||
@@ -61,7 +61,7 @@ public class Console implements GameListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(Color color) {
|
||||
public void onPlayerTurn(Color color, boolean undone) {
|
||||
if (!captureInput)
|
||||
return;
|
||||
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||
|
||||
@@ -213,7 +213,7 @@ public class Window extends JFrame implements GameListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerTurn(chess.model.Color color) {
|
||||
public void onPlayerTurn(chess.model.Color color, boolean undone) {
|
||||
this.displayText.setText("Current turn: " + color);
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user