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.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import chess.controller.Command;
|
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.CastlingCommand;
|
import chess.controller.commands.CastlingCommand;
|
||||||
import chess.controller.commands.GetAllowedCastlingsCommand;
|
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
|
||||||
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.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
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();
|
private final Random random = new Random();
|
||||||
|
|
||||||
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
public DumbAI(CommandExecutor commandExecutor, Color color) {
|
||||||
this.player = color;
|
super(commandExecutor, color);
|
||||||
this.commandExecutor = commandExecutor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color) {
|
protected void play() {
|
||||||
if (color != player)
|
CastlingResult castlings = getAllowedCastlings();
|
||||||
return;
|
List<Move> moves = getAllowedMoves();
|
||||||
|
|
||||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
|
||||||
sendCommand(cmd);
|
|
||||||
|
|
||||||
GetAllowedCastlingsCommand cmd2 = new GetAllowedCastlingsCommand();
|
|
||||||
sendCommand(cmd2);
|
|
||||||
|
|
||||||
CastlingResult castlings = cmd2.getCastlingResult();
|
|
||||||
List<Move> moves = cmd.getMoves();
|
|
||||||
|
|
||||||
switch (castlings) {
|
switch (castlings) {
|
||||||
case Both: {
|
case Both: {
|
||||||
@@ -76,27 +58,12 @@ public class DumbAI extends GameAdaptator {
|
|||||||
|
|
||||||
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
|
||||||
public void onPromotePawn(Coordinate pieceCoords) {
|
protected void promote(Coordinate pawnCoords) {
|
||||||
Piece pawn = pieceAt(pieceCoords);
|
|
||||||
if (pawn.getColor() != this.player)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int promote = this.random.nextInt(PromoteType.values().length);
|
int promote = this.random.nextInt(PromoteType.values().length);
|
||||||
this.commandExecutor.executeCommand(new PromoteCommand(PromoteType.values()[promote]));
|
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();
|
this.dispatcher.onGameEnd();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switchPlayerTurn();
|
switchPlayerTurn(command instanceof UndoCommand);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void switchPlayerTurn() {
|
private void switchPlayerTurn(boolean undone) {
|
||||||
this.game.switchPlayerTurn();
|
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();
|
game.reset();
|
||||||
|
|
||||||
outputSystem.onGameStart();
|
outputSystem.onGameStart();
|
||||||
outputSystem.onPlayerTurn(game.getPlayerTurn());
|
outputSystem.onPlayerTurn(game.getPlayerTurn(), false);
|
||||||
|
|
||||||
return CommandResult.NotMoved;
|
return CommandResult.NotMoved;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class EmptyGameDispatcher extends GameDispatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color) {
|
public void onPlayerTurn(Color color, boolean undone) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import chess.model.Move;
|
|||||||
public abstract class GameAdaptator implements GameListener {
|
public abstract class GameAdaptator implements GameListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color) {}
|
public void onPlayerTurn(Color color, boolean undone) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onWin(Color color) {}
|
public void onWin(Color color) {}
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ public class GameDispatcher implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color) {
|
public void onPlayerTurn(Color color, boolean undone) {
|
||||||
asyncForEachCall((l) -> l.onPlayerTurn(color));
|
asyncForEachCall((l) -> l.onPlayerTurn(color, undone));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ public interface GameListener {
|
|||||||
/**
|
/**
|
||||||
* Invoked when it's the player turn
|
* Invoked when it's the player turn
|
||||||
* @param color the color of the player who should play
|
* @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
|
* Invoked when a pawn should be promoted
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class Console implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(Color color) {
|
public void onPlayerTurn(Color color, boolean undone) {
|
||||||
if (!captureInput)
|
if (!captureInput)
|
||||||
return;
|
return;
|
||||||
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
System.out.println(Colors.RED + "Player turn: " + color + Colors.RESET);
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ public class Window extends JFrame implements GameListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerTurn(chess.model.Color color) {
|
public void onPlayerTurn(chess.model.Color color, boolean undone) {
|
||||||
this.displayText.setText("Current turn: " + color);
|
this.displayText.setText("Current turn: " + color);
|
||||||
updateButtons();
|
updateButtons();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user