Compare commits
4 Commits
8190090adc
...
7b7280d807
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b7280d807 | |||
| d2485a4d75 | |||
| acd20ef7fa | |||
| 5cf00309b3 |
@@ -11,12 +11,10 @@ import chess.view.consolerender.Console;
|
|||||||
|
|
||||||
public class ConsoleMain {
|
public class ConsoleMain {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CommandExecutor commandExecutor = new CommandExecutor();
|
|
||||||
|
|
||||||
Game game = new Game(new ChessBoard());
|
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.addListener(console);
|
||||||
|
|
||||||
commandExecutor.executeCommand(new NewGameCommand());
|
commandExecutor.executeCommand(new NewGameCommand());
|
||||||
|
|||||||
@@ -1,21 +1,27 @@
|
|||||||
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;
|
||||||
|
|
||||||
public class SwingMain {
|
public class SwingMain {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CommandExecutor commandExecutor = new CommandExecutor();
|
|
||||||
|
|
||||||
Game game = new Game(new ChessBoard());
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,7 +13,11 @@ public class CommandExecutor {
|
|||||||
private final GameDispatcher dispatcher;
|
private final GameDispatcher dispatcher;
|
||||||
|
|
||||||
public CommandExecutor() {
|
public CommandExecutor() {
|
||||||
this.game = null;
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandExecutor(Game game) {
|
||||||
|
this.game = game;
|
||||||
this.dispatcher = new GameDispatcher();
|
this.dispatcher = new GameDispatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package chess.controller.event;
|
|||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
|
||||||
public class GameAdaptator implements GameListener {
|
public abstract class GameAdaptator implements GameListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playerTurn(Color color) {}
|
public void playerTurn(Color color) {}
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import java.awt.event.MouseAdapter;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
@@ -22,12 +21,11 @@ import chess.controller.CommandExecutor;
|
|||||||
import chess.controller.commands.CastlingCommand;
|
import chess.controller.commands.CastlingCommand;
|
||||||
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
import chess.controller.commands.GetAllowedMovesPieceCommand;
|
||||||
import chess.controller.commands.GetPieceAtCommand;
|
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.PromoteCommand.PromoteType;
|
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||||
import chess.controller.event.GameListener;
|
|
||||||
import chess.controller.commands.UndoCommand;
|
import chess.controller.commands.UndoCommand;
|
||||||
|
import chess.controller.event.GameListener;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Move;
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
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 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 +199,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 +211,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!");
|
||||||
});
|
});
|
||||||
@@ -235,6 +229,7 @@ public class Window extends JFrame implements GameListener {
|
|||||||
public void patSituation() {
|
public void patSituation() {
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
|
JOptionPane.showMessageDialog(this, "Pat. It's a draw!");
|
||||||
|
this.dispose();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,6 +247,8 @@ public class Window extends JFrame implements GameListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void promotePawn(Coordinate pieceCoords) {
|
public void promotePawn(Coordinate pieceCoords) {
|
||||||
|
if (!showPopups)
|
||||||
|
return;
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
String result = null;
|
String result = null;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user