thread pool + ai vs ai
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import chess.ai.DumbAI;
|
||||||
import chess.controller.CommandExecutor;
|
import chess.controller.CommandExecutor;
|
||||||
import chess.controller.commands.NewGameCommand;
|
import chess.controller.commands.NewGameCommand;
|
||||||
import chess.model.ChessBoard;
|
import chess.model.ChessBoard;
|
||||||
|
import chess.model.Color;
|
||||||
import chess.model.Game;
|
import chess.model.Game;
|
||||||
import chess.view.simplerender.Window;
|
import chess.view.simplerender.Window;
|
||||||
|
|
||||||
@@ -11,9 +13,15 @@ public class SwingMain {
|
|||||||
Game game = new Game(new ChessBoard());
|
Game game = new Game(new ChessBoard());
|
||||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||||
|
|
||||||
Window window = new Window(commandExecutor);
|
Window window = new Window(commandExecutor, false);
|
||||||
commandExecutor.addListener(window);
|
commandExecutor.addListener(window);
|
||||||
|
|
||||||
|
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
|
||||||
|
commandExecutor.addListener(ai);
|
||||||
|
|
||||||
|
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
|
||||||
|
commandExecutor.addListener(ai2);
|
||||||
|
|
||||||
commandExecutor.executeCommand(new NewGameCommand());
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
app/src/main/java/chess/ai/DumbAI.java
Normal file
62
app/src/main/java/chess/ai/DumbAI.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,9 @@ package chess.controller.event;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
@@ -9,60 +12,64 @@ import chess.model.Coordinate;
|
|||||||
public class GameDispatcher implements GameListener{
|
public class GameDispatcher implements GameListener{
|
||||||
|
|
||||||
private final List<GameListener> listeners;
|
private final List<GameListener> listeners;
|
||||||
|
private final Executor executor;
|
||||||
|
|
||||||
public GameDispatcher() {
|
public GameDispatcher() {
|
||||||
this.listeners = new ArrayList<>();
|
this.listeners = new ArrayList<>();
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addListener(GameListener listener) {
|
public void addListener(GameListener listener) {
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void asyncForEachCall(Consumer<GameListener> func) {
|
||||||
|
this.executor.execute(() -> this.listeners.forEach(func));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playerTurn(Color color) {
|
public void playerTurn(Color color) {
|
||||||
this.listeners.forEach((l) -> l.playerTurn(color));
|
asyncForEachCall((l) -> l.playerTurn(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void winnerIs(Color color) {
|
public void winnerIs(Color color) {
|
||||||
this.listeners.forEach((l) -> l.winnerIs(color));
|
asyncForEachCall((l) -> l.winnerIs(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInCheck() {
|
public void kingIsInCheck() {
|
||||||
this.listeners.forEach((l) -> l.kingIsInCheck());
|
asyncForEachCall((l) -> l.kingIsInCheck());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInMat() {
|
public void kingIsInMat() {
|
||||||
this.listeners.forEach((l) -> l.kingIsInMat());
|
asyncForEachCall((l) -> l.kingIsInMat());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void patSituation() {
|
public void patSituation() {
|
||||||
this.listeners.forEach((l) -> l.patSituation());
|
asyncForEachCall((l) -> l.patSituation());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void hasSurrendered(Color color) {
|
public void hasSurrendered(Color color) {
|
||||||
this.listeners.forEach((l) -> l.hasSurrendered(color));
|
asyncForEachCall((l) -> l.hasSurrendered(color));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void gameStarted() {
|
public void gameStarted() {
|
||||||
this.listeners.forEach((l) -> l.gameStarted());
|
asyncForEachCall((l) -> l.gameStarted());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
this.listeners.forEach((l) -> l.promotePawn(pieceCoords));
|
asyncForEachCall((l) -> l.promotePawn(pieceCoords));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateDisplay() {
|
public void updateDisplay() {
|
||||||
this.listeners.forEach((l) -> l.updateDisplay());
|
asyncForEachCall((l) -> l.updateDisplay());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,10 +44,13 @@ public class Window extends JFrame implements GameListener {
|
|||||||
private final JButton bigCastlingButton = new JButton("Grand Roque");
|
private final JButton bigCastlingButton = new JButton("Grand Roque");
|
||||||
private final JButton undoButton = new JButton("Annuler le coup précédent");
|
private final JButton undoButton = new JButton("Annuler le coup précédent");
|
||||||
|
|
||||||
public Window(CommandExecutor commandExecutor) {
|
private final boolean showPopups;
|
||||||
|
|
||||||
|
public Window(CommandExecutor commandExecutor, boolean showPopups) {
|
||||||
this.cells = new JLabel[8][8];
|
this.cells = new JLabel[8][8];
|
||||||
this.displayText = new JLabel();
|
this.displayText = new JLabel();
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
|
this.showPopups = showPopups;
|
||||||
setSize(800, 910);
|
setSize(800, 910);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
@@ -198,15 +201,6 @@ public class Window extends JFrame implements GameListener {
|
|||||||
@Override
|
@Override
|
||||||
public void playerTurn(chess.model.Color color) {
|
public void playerTurn(chess.model.Color color) {
|
||||||
this.displayText.setText("Current turn: " + color);
|
this.displayText.setText("Current turn: " + color);
|
||||||
|
|
||||||
// dumb IA
|
|
||||||
if (color == chess.model.Color.Black) {
|
|
||||||
GetPlayerMovesCommand cmd = new GetPlayerMovesCommand();
|
|
||||||
sendCommand(cmd);
|
|
||||||
List<Move> moves = cmd.getMoves();
|
|
||||||
int random = new Random().nextInt(moves.size());
|
|
||||||
sendCommand(new MoveCommand(moves.get(random)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -219,6 +213,8 @@ public class Window extends JFrame implements GameListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kingIsInCheck() {
|
public void kingIsInCheck() {
|
||||||
|
if (!showPopups)
|
||||||
|
return;
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
JOptionPane.showMessageDialog(this, "Check!");
|
JOptionPane.showMessageDialog(this, "Check!");
|
||||||
});
|
});
|
||||||
@@ -252,39 +248,39 @@ public class Window extends JFrame implements GameListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
SwingUtilities.invokeLater(() -> {
|
// SwingUtilities.invokeLater(() -> {
|
||||||
String result = null;
|
// String result = null;
|
||||||
|
|
||||||
Object[] possibilities = new Object[PromoteType.values().length];
|
// Object[] possibilities = new Object[PromoteType.values().length];
|
||||||
int i = 0;
|
// int i = 0;
|
||||||
for (PromoteType type : PromoteType.values()) {
|
// for (PromoteType type : PromoteType.values()) {
|
||||||
possibilities[i] = type.name();
|
// possibilities[i] = type.name();
|
||||||
i++;
|
// i++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
while (result == null || result.isEmpty()) {
|
// while (result == null || result.isEmpty()) {
|
||||||
result = (String) JOptionPane.showInputDialog(
|
// result = (String) JOptionPane.showInputDialog(
|
||||||
this,
|
// this,
|
||||||
"Choose the type of piece to upgrade the pawn",
|
// "Choose the type of piece to upgrade the pawn",
|
||||||
"Promote Dialog",
|
// "Promote Dialog",
|
||||||
JOptionPane.PLAIN_MESSAGE,
|
// JOptionPane.PLAIN_MESSAGE,
|
||||||
null,
|
// null,
|
||||||
possibilities,
|
// possibilities,
|
||||||
possibilities[0]);
|
// possibilities[0]);
|
||||||
}
|
// }
|
||||||
|
|
||||||
PromoteType choosedType = null;
|
// PromoteType choosedType = null;
|
||||||
|
|
||||||
for (PromoteType type : PromoteType.values()) {
|
// for (PromoteType type : PromoteType.values()) {
|
||||||
if (type.name().equals(result)) {
|
// if (type.name().equals(result)) {
|
||||||
choosedType = type;
|
// choosedType = type;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (choosedType != null)
|
// if (choosedType != null)
|
||||||
sendCommand(new PromoteCommand(choosedType));
|
// sendCommand(new PromoteCommand(choosedType));
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user