juste better

This commit is contained in:
2025-04-02 10:54:14 +02:00
parent 1b9ff5bdd1
commit 97cafb903a
12 changed files with 212 additions and 131 deletions

View File

@@ -4,6 +4,7 @@ import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
@@ -13,68 +14,26 @@ import chess.io.Command;
import chess.io.CommandExecutor;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.commands.GetAllowedMovesCommand;
import chess.io.commands.GetPieceAtCommand;
import chess.io.commands.MoveCommand;
import chess.io.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Coordinate;
import chess.model.Game;
import chess.model.Move;
import chess.model.Piece;
public class Window extends JFrame {
public class Window extends JFrame implements OutputSystem {
private final JLabel cells[][];
private final CommandExecutor commandExecutor;
private final ChessBoard board;
private Coordinate lastClick = null;
public Window(Game game) {
public Window(CommandExecutor commandExecutor) {
this.cells = new JLabel[8][8];
this.commandExecutor = new CommandExecutor(game, initSlots());
this.board = this.commandExecutor.getGame().getBoard();
build();
this.commandExecutor = commandExecutor;
setSize(800, 800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
sendCommand(new NewGameCommand());
updateBoard();
}
private OutputSystem initSlots() {
OutputSystem outputSystem = new OutputSystem() {
@Override
public void playerTurn(chess.model.Color color) {
System.out.println("Player turn " + color);
}
@Override
public void winnerIs(chess.model.Color color) {
System.out.println("Winner is " + color);
}
@Override
public void kingIsInCheck() {
System.out.println("Check");
}
@Override
public void kingIsInMat() {
System.out.println("CheckMate");
}
@Override
public void patSituation() {
System.out.println("Pat");
}
@Override
public void hasSurrendered(chess.model.Color color) {
System.out.println("Surrendered");
}
};
return outputSystem;
}
private CommandResult sendCommand(Command command) {
@@ -89,7 +48,7 @@ public class Window extends JFrame {
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
}
private void build() {
private void buildBoard() {
JPanel content = new JPanel(new GridLayout(8, 8));
setContentPane(content);
for (int y = 0; y < 8; y++) {
@@ -104,9 +63,10 @@ public class Window extends JFrame {
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
public void mousePressed(MouseEvent e) {
onCellClicked(xx, yy);
}
});
content.add(label);
}
@@ -114,26 +74,38 @@ public class Window extends JFrame {
updateBoard();
}
private boolean isCellEmpty(int x, int y) {
return pieceAt(x, y) == null;
}
private Piece pieceAt(int x, int y) {
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
sendCommand(command);
return command.getPiece();
}
private void updateBoard() {
PieceIcon pieceIcon = new PieceIcon();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
JLabel cell = this.cells[x][y];
cell.setIcon(pieceIcon.getIcon(this.board.pieceAt(new Coordinate(x, y))));
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
}
}
}
private boolean previewMoves(int x, int y) {
boolean[][] allowedMoves = this.commandExecutor.getGame().getAllowedMoves(new Coordinate(x, y));
if (allowedMoves == null)
GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(new Coordinate(x, y));
if(sendCommand(movesCommand) == CommandResult.NotAllowed)
return false;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
JLabel cell = this.cells[i][j];
if (allowedMoves[i][j])
cell.setBackground(Color.CYAN);
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty())
return false;
for (Coordinate destCoord : allowedMoves) {
JLabel cell = this.cells[destCoord.getX()][destCoord.getY()];
cell.setBackground(Color.CYAN);
}
return true;
}
@@ -157,7 +129,7 @@ public class Window extends JFrame {
private void onCellClicked(int x, int y) {
clearMoves();
if (this.lastClick == null) {
if (this.board.isCellEmpty(new Coordinate(x, y)))
if (isCellEmpty(x, y))
return;
if (!previewMoves(x, y))
return;
@@ -173,4 +145,39 @@ public class Window extends JFrame {
this.lastClick = null;
}
@Override
public void playerTurn(chess.model.Color color) {
System.out.println("C'est au tour de " + color);
}
@Override
public void winnerIs(chess.model.Color color) {
System.out.println("Victoire de " + color);
}
@Override
public void kingIsInCheck() {
System.out.println("Échec !");
}
@Override
public void kingIsInMat() {
System.out.println("Échec et mat !");
}
@Override
public void patSituation() {
System.out.println("Pat. Égalité !");
}
@Override
public void hasSurrendered(chess.model.Color color) {
System.out.println("Abandon de " + color);
}
@Override
public void gameStarted() {
buildBoard();
}
}