238 lines
5.8 KiB
Java
238 lines
5.8 KiB
Java
package chess.simplerender;
|
|
|
|
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;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.SwingUtilities;
|
|
|
|
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.PromoteCommand;
|
|
import chess.io.commands.PromoteCommand.PromoteType;
|
|
import chess.model.Coordinate;
|
|
import chess.model.Move;
|
|
import chess.model.Piece;
|
|
|
|
public class Window extends JFrame implements OutputSystem {
|
|
|
|
private final JLabel cells[][];
|
|
private final CommandExecutor commandExecutor;
|
|
|
|
private Coordinate lastClick = null;
|
|
|
|
public Window(CommandExecutor commandExecutor) {
|
|
this.cells = new JLabel[8][8];
|
|
this.commandExecutor = commandExecutor;
|
|
setSize(800, 800);
|
|
setVisible(true);
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
}
|
|
|
|
private CommandResult sendCommand(Command command) {
|
|
CommandResult result = this.commandExecutor.executeCommand(command);
|
|
if (result == CommandResult.Moved) {
|
|
updateBoard();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private Color getCellColor(int x, int y) {
|
|
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
|
}
|
|
|
|
private void buildBoard() {
|
|
JPanel content = new JPanel(new GridLayout(8, 8));
|
|
setContentPane(content);
|
|
for (int y = 0; y < 8; y++) {
|
|
for (int x = 0; x < 8; x++) {
|
|
JLabel label = new JLabel();
|
|
label.setOpaque(true);
|
|
label.setBackground(getCellColor(x, y));
|
|
this.cells[x][y] = label;
|
|
|
|
final int xx = x;
|
|
final int yy = y;
|
|
|
|
label.addMouseListener(new MouseAdapter() {
|
|
@Override
|
|
public void mousePressed(MouseEvent e) {
|
|
onCellClicked(xx, yy);
|
|
}
|
|
|
|
});
|
|
content.add(label);
|
|
}
|
|
}
|
|
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(pieceAt(x, y)));
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean previewMoves(int x, int y) {
|
|
GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(new Coordinate(x, y));
|
|
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
|
return false;
|
|
|
|
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;
|
|
}
|
|
|
|
private void drawInvalid(Move move) {
|
|
JLabel from = this.cells[move.getStart().getX()][move.getStart().getY()];
|
|
JLabel to = this.cells[move.getFinish().getX()][move.getFinish().getY()];
|
|
from.setBackground(Color.RED);
|
|
to.setBackground(Color.RED);
|
|
}
|
|
|
|
private void clearMoves() {
|
|
for (int y = 0; y < 8; y++) {
|
|
for (int x = 0; x < 8; x++) {
|
|
JLabel cell = this.cells[x][y];
|
|
cell.setBackground(getCellColor(x, y));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void onCellClicked(int x, int y) {
|
|
clearMoves();
|
|
if (this.lastClick == null) {
|
|
if (isCellEmpty(x, y))
|
|
return;
|
|
if (!previewMoves(x, y))
|
|
return;
|
|
this.lastClick = new Coordinate(x, y);
|
|
return;
|
|
}
|
|
if (!this.lastClick.equals(new Coordinate(x, y))) {
|
|
Move move = new Move(lastClick, new Coordinate(x, y));
|
|
if (sendCommand(new MoveCommand(move)) == CommandResult.NotAllowed) {
|
|
drawInvalid(move);
|
|
}
|
|
}
|
|
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) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JOptionPane.showMessageDialog(this, "Victoire de " + color);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void kingIsInCheck() {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JOptionPane.showMessageDialog(this, "Échec !");
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void kingIsInMat() {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JOptionPane.showMessageDialog(this, "Échec et mat !");
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void patSituation() {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JOptionPane.showMessageDialog(this, "Pat. Égalité !");
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void hasSurrendered(chess.model.Color color) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JOptionPane.showMessageDialog(this, "Abandon de " + color);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void gameStarted() {
|
|
buildBoard();
|
|
}
|
|
|
|
@Override
|
|
public void promotePawn(Coordinate pieceCoords) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
// String result = JOptionPane.showInputDialog(this, "Choisissez le type !");
|
|
// sendCommand(new PromoteCommand(PromoteType.Queen, pieceCoords));
|
|
|
|
String result = null;
|
|
|
|
Object[] possibilities = new Object[PromoteType.values().length];
|
|
int i = 0;
|
|
for (PromoteType type : PromoteType.values()) {
|
|
possibilities[i] = type.name();
|
|
i++;
|
|
}
|
|
|
|
while (result == null || result.isEmpty()) {
|
|
result = (String) JOptionPane.showInputDialog(
|
|
this,
|
|
"Choose the type of piece to upgrade the pawn",
|
|
"Promote Dialog",
|
|
JOptionPane.PLAIN_MESSAGE,
|
|
null,
|
|
possibilities,
|
|
possibilities[0]);
|
|
}
|
|
|
|
PromoteType choosedType = null;
|
|
|
|
for (PromoteType type : PromoteType.values()) {
|
|
if (type.name().equals(result)) {
|
|
choosedType = type;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (choosedType != null)
|
|
sendCommand(new PromoteCommand(choosedType, pieceCoords));
|
|
});
|
|
}
|
|
|
|
}
|