Compare commits

..

4 Commits

Author SHA1 Message Date
7b7280d807 promote popup fix 2025-04-12 12:23:22 +02:00
d2485a4d75 thread pool + ai vs ai 2025-04-12 12:15:58 +02:00
acd20ef7fa GameAdaptator abstract 2025-04-12 11:20:22 +02:00
5cf00309b3 simplify mains 2025-04-12 11:20:12 +02:00
7 changed files with 108 additions and 34 deletions

View File

@@ -11,12 +11,10 @@ import chess.view.consolerender.Console;
public class ConsoleMain {
public static void main(String[] args) {
CommandExecutor commandExecutor = new CommandExecutor();
Game game = new Game(new ChessBoard());
Console console = new Console(commandExecutor);
CommandExecutor commandExecutor = new CommandExecutor(game);
commandExecutor.setGame(game);
Console console = new Console(commandExecutor);
commandExecutor.addListener(console);
commandExecutor.executeCommand(new NewGameCommand());

View File

@@ -1,21 +1,27 @@
package chess;
import chess.ai.DumbAI;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Color;
import chess.model.Game;
import chess.view.simplerender.Window;
public class SwingMain {
public static void main(String[] args) {
CommandExecutor commandExecutor = new CommandExecutor();
Game game = new Game(new ChessBoard());
Window window = new Window(commandExecutor);
CommandExecutor commandExecutor = new CommandExecutor(game);
commandExecutor.setGame(game);
Window window = new Window(commandExecutor, false);
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());
}
}

View 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);
}
}

View File

@@ -13,7 +13,11 @@ public class CommandExecutor {
private final GameDispatcher dispatcher;
public CommandExecutor() {
this.game = null;
this(null);
}
public CommandExecutor(Game game) {
this.game = game;
this.dispatcher = new GameDispatcher();
}

View File

@@ -3,7 +3,7 @@ package chess.controller.event;
import chess.model.Color;
import chess.model.Coordinate;
public class GameAdaptator implements GameListener {
public abstract class GameAdaptator implements GameListener {
@Override
public void playerTurn(Color color) {}

View File

@@ -2,6 +2,9 @@ package chess.controller.event;
import java.util.ArrayList;
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.Coordinate;
@@ -9,60 +12,64 @@ import chess.model.Coordinate;
public class GameDispatcher implements GameListener{
private final List<GameListener> listeners;
private final Executor executor;
public GameDispatcher() {
this.listeners = new ArrayList<>();
this.executor = Executors.newSingleThreadExecutor();
}
public void addListener(GameListener listener) {
this.listeners.add(listener);
}
private void asyncForEachCall(Consumer<GameListener> func) {
this.executor.execute(() -> this.listeners.forEach(func));
}
@Override
public void playerTurn(Color color) {
this.listeners.forEach((l) -> l.playerTurn(color));
asyncForEachCall((l) -> l.playerTurn(color));
}
@Override
public void winnerIs(Color color) {
this.listeners.forEach((l) -> l.winnerIs(color));
asyncForEachCall((l) -> l.winnerIs(color));
}
@Override
public void kingIsInCheck() {
this.listeners.forEach((l) -> l.kingIsInCheck());
asyncForEachCall((l) -> l.kingIsInCheck());
}
@Override
public void kingIsInMat() {
this.listeners.forEach((l) -> l.kingIsInMat());
asyncForEachCall((l) -> l.kingIsInMat());
}
@Override
public void patSituation() {
this.listeners.forEach((l) -> l.patSituation());
asyncForEachCall((l) -> l.patSituation());
}
@Override
public void hasSurrendered(Color color) {
this.listeners.forEach((l) -> l.hasSurrendered(color));
asyncForEachCall((l) -> l.hasSurrendered(color));
}
@Override
public void gameStarted() {
this.listeners.forEach((l) -> l.gameStarted());
asyncForEachCall((l) -> l.gameStarted());
}
@Override
public void promotePawn(Coordinate pieceCoords) {
this.listeners.forEach((l) -> l.promotePawn(pieceCoords));
asyncForEachCall((l) -> l.promotePawn(pieceCoords));
}
@Override
public void updateDisplay() {
this.listeners.forEach((l) -> l.updateDisplay());
asyncForEachCall((l) -> l.updateDisplay());
}
}

View File

@@ -7,7 +7,6 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
@@ -22,12 +21,11 @@ import chess.controller.CommandExecutor;
import chess.controller.commands.CastlingCommand;
import chess.controller.commands.GetAllowedMovesPieceCommand;
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.GameListener;
import chess.controller.commands.UndoCommand;
import chess.controller.event.GameListener;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
@@ -44,10 +42,13 @@ public class Window extends JFrame implements GameListener {
private final JButton bigCastlingButton = new JButton("Grand Roque");
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.displayText = new JLabel();
this.commandExecutor = commandExecutor;
this.showPopups = showPopups;
setSize(800, 910);
setVisible(true);
setLocationRelativeTo(null);
@@ -198,15 +199,6 @@ public class Window extends JFrame implements GameListener {
@Override
public void playerTurn(chess.model.Color 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
@@ -219,6 +211,8 @@ public class Window extends JFrame implements GameListener {
@Override
public void kingIsInCheck() {
if (!showPopups)
return;
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "Check!");
});
@@ -235,6 +229,7 @@ public class Window extends JFrame implements GameListener {
public void patSituation() {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
this.dispose();
});
}
@@ -252,6 +247,8 @@ public class Window extends JFrame implements GameListener {
@Override
public void promotePawn(Coordinate pieceCoords) {
if (!showPopups)
return;
SwingUtilities.invokeLater(() -> {
String result = null;