lots of things

This commit is contained in:
2025-04-01 23:11:41 +02:00
parent 2c6b64fa7d
commit 1b9ff5bdd1
15 changed files with 428 additions and 79 deletions

View File

@@ -9,6 +9,12 @@ import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import chess.io.Command;
import chess.io.CommandExecutor;
import chess.io.CommandResult;
import chess.io.OutputSystem;
import chess.io.commands.MoveCommand;
import chess.io.commands.NewGameCommand;
import chess.model.ChessBoard;
import chess.model.Coordinate;
import chess.model.Game;
@@ -17,25 +23,66 @@ import chess.model.Move;
public class Window extends JFrame {
private final JLabel cells[][];
private final Game game;
private final CommandExecutor commandExecutor;
private final ChessBoard board;
private Coordinate lastClick = null;
public Window(Game game) {
this.cells = new JLabel[8][8];
this.game = game;
this.board = game.getBoard();
initSlots();
this.commandExecutor = new CommandExecutor(game, initSlots());
this.board = this.commandExecutor.getGame().getBoard();
build();
setSize(800, 800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
sendCommand(new NewGameCommand());
updateBoard();
}
private void initSlots() {
this.game.OnRenderUpdate.connect(this::updateBoard);
this.game.OnMoveRefused.connect(this::drawInvalid);
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) {
CommandResult result = this.commandExecutor.executeCommand(command);
if (result == CommandResult.Moved) {
updateBoard();
}
return result;
}
private Color getCellColor(int x, int y) {
@@ -77,10 +124,10 @@ public class Window extends JFrame {
}
}
private void previewMoves(int x, int y) {
boolean[][] allowedMoves = this.board.getAllowedMoves(new Coordinate(x, y));
private boolean previewMoves(int x, int y) {
boolean[][] allowedMoves = this.commandExecutor.getGame().getAllowedMoves(new Coordinate(x, y));
if (allowedMoves == null)
return;
return false;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
JLabel cell = this.cells[i][j];
@@ -88,6 +135,7 @@ public class Window extends JFrame {
cell.setBackground(Color.CYAN);
}
}
return true;
}
private void drawInvalid(Move move) {
@@ -111,12 +159,17 @@ public class Window extends JFrame {
if (this.lastClick == null) {
if (this.board.isCellEmpty(new Coordinate(x, y)))
return;
if (!previewMoves(x, y))
return;
this.lastClick = new Coordinate(x, y);
previewMoves(x, y);
return;
}
if (!this.lastClick.equals(new Coordinate(x, y)))
this.game.tryMove(new Move(lastClick, new Coordinate(x, y)));
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;
}