Compare commits
No commits in common. "08fc96187b5c19468b82487bbfc6b7fd7a5a7fbd" and "17bdd3e37f44e24c4496b386a0b1a3e63ec4016f" have entirely different histories.
08fc96187b
...
17bdd3e37f
|
|
@ -15,8 +15,8 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
|
|
|||
|
|
@ -8,25 +8,23 @@ import java.util.List;
|
|||
|
||||
import javax.swing.BorderFactory;
|
||||
|
||||
import de.hs_mannheim.informatik.chess.gui.Gui;
|
||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||
import de.hs_mannheim.informatik.chess.model.MoveDTO;
|
||||
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
||||
import de.hs_mannheim.informatik.chess.view.GameGui;
|
||||
|
||||
public class Controller {
|
||||
GameGui gui;
|
||||
ChessEngine engine;
|
||||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
Gui gui;
|
||||
ChessEngine engine;
|
||||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
|
||||
public Controller(GameGui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
initListeners();
|
||||
public Controller(Gui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
}
|
||||
|
||||
private void initListeners() {
|
||||
private void initListeners() {
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
final int r = row, c = col;
|
||||
|
|
@ -39,70 +37,75 @@ public class Controller {
|
|||
}
|
||||
}
|
||||
|
||||
private void handleClick(int row, int col) {
|
||||
if (selectedRow == -1 && selectedCol == -1) {
|
||||
// Erstes Feld gewählt
|
||||
selectedRow = row;
|
||||
selectedCol = col;
|
||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||
private void handleClick(int row, int col) {
|
||||
if (selectedRow == -1 && selectedCol == -1) {
|
||||
// Erstes Feld gewählt
|
||||
selectedRow = row;
|
||||
selectedCol = col;
|
||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||
|
||||
// Mögliche Ziele highlighten (DTOs!)
|
||||
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
|
||||
for (MoveDTO move : moves) {
|
||||
int toRow = move.getToRow();
|
||||
int toCol = move.getToCol();
|
||||
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(new int[]{toRow, toCol});
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
highlightedFields.clear();
|
||||
//Mögliche Ziele highlighten
|
||||
String from = coordToChessNotation(row, col);
|
||||
List<String> targets = engine.getLegalDestinations(from);
|
||||
for (String t : targets) {
|
||||
int[] xy = chessNotationToCoord(t);
|
||||
gui.getField(xy[0], xy[1]).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(xy);
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
highlightedFields.clear();
|
||||
|
||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||
handleMove(selectedRow, selectedCol, row, col);
|
||||
selectedRow = -1;
|
||||
selectedCol = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// **Neues MoveDTO statt String-Kram!**
|
||||
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
||||
handleMove(move);
|
||||
public void handleMove(int fromX, int fromY, int toX, int toY) {
|
||||
String from = coordToChessNotation(fromX, fromY);
|
||||
String to = coordToChessNotation(toX, toY);
|
||||
if (engine.move(from, to)) {
|
||||
updateGuiBoard();
|
||||
|
||||
selectedRow = -1;
|
||||
selectedCol = -1;
|
||||
}
|
||||
// Jetzt: Spielstatus prüfen!
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
// Optional: Hier Spiel beenden/disable oder restart anbieten
|
||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||
}
|
||||
} else {
|
||||
gui.displayMessage("Ungültiger Zug!");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateGuiBoard() {
|
||||
gui.updateBoard(engine.getBoardUnicode());
|
||||
}
|
||||
|
||||
public void handleMove(MoveDTO move) {
|
||||
if (engine.move(move)) {
|
||||
updateGuiBoard();
|
||||
|
||||
// Jetzt: Spielstatus prüfen!
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||
}
|
||||
} else {
|
||||
gui.displayMessage("Ungültiger Zug!");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateGuiBoard() {
|
||||
BoardDTO board = engine.getBoardAsDTO();
|
||||
gui.updateBoard(board); // Passe die GUI an, damit sie ein BoardDTO nimmt!
|
||||
}
|
||||
|
||||
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
||||
private String coordToChessNotation(int x, int y) {
|
||||
char file = (char)('A' + y);
|
||||
private String coordToChessNotation(int x, int y) {
|
||||
// (0,0) -> "A8", (7,7) -> "H1"
|
||||
char file = (char)('A' + y); // ACHTUNG! col == y == FILE, row == x == RANK
|
||||
int rank = 8 - x;
|
||||
return "" + file + rank;
|
||||
}
|
||||
|
||||
// ... resetFieldBackground wie gehabt ...
|
||||
private int[] chessNotationToCoord(String square) {
|
||||
// "A8" -> (0,0), "H1" -> (7,7)
|
||||
char file = square.charAt(0);
|
||||
int rank = square.charAt(1) - '0';
|
||||
int x = 8 - rank;
|
||||
int y = file - 'A';
|
||||
return new int[] {x, y};
|
||||
}
|
||||
|
||||
private void resetFieldBackground(int row, int col) {
|
||||
if ((row + col) % 2 == 0) {
|
||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package de.hs_mannheim.informatik.chess.view;
|
||||
package de.hs_mannheim.informatik.chess.gui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
|
|
@ -14,14 +14,11 @@ import javax.swing.JOptionPane;
|
|||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
||||
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||
|
||||
public class GameGui {
|
||||
public class Gui {
|
||||
|
||||
private JLabel[][] fields = new JLabel[8][8];
|
||||
|
||||
public GameGui(){
|
||||
public Gui(){
|
||||
mainFrame();
|
||||
}
|
||||
|
||||
|
|
@ -114,20 +111,13 @@ public class GameGui {
|
|||
return fields[row][col];
|
||||
}
|
||||
|
||||
public void updateBoard(BoardDTO boardDTO) {
|
||||
PieceDTO[][] board = boardDTO.getBoard();
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
PieceDTO piece = board[row][col];
|
||||
if (piece != null) {
|
||||
fields[row][col].setText(piece.getUnicodeSymbol());
|
||||
} else {
|
||||
fields[row][col].setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateBoard(String[][] unicodeBoard) {
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
fields[row][col].setText(unicodeBoard[row][col]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void displayMessage(String msg) {
|
||||
JOptionPane.showMessageDialog(null, msg);
|
||||
|
|
@ -1,19 +1,15 @@
|
|||
package de.hs_mannheim.informatik.chess.main;
|
||||
import de.hs_mannheim.informatik.chess.gui.Gui;
|
||||
import de.hs_mannheim.informatik.chess.controller.Controller;
|
||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||
import de.hs_mannheim.informatik.chess.view.GameGui;
|
||||
import de.hs_mannheim.informatik.chess.view.MainGui;
|
||||
|
||||
|
||||
public class Main{
|
||||
|
||||
public static void main( String[] args ){
|
||||
new MainGui(() -> {
|
||||
//Wenn "Normal Modus" gedrückt wird
|
||||
GameGui gui = new GameGui();
|
||||
ChessEngine engine = new ChessEngine();
|
||||
new Controller(gui, engine);
|
||||
});
|
||||
Gui gui = new Gui();
|
||||
ChessEngine engine = new ChessEngine();
|
||||
new Controller(gui, engine);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
public class BoardDTO {
|
||||
|
||||
private PieceDTO[][] board; // 8x8 Feld
|
||||
|
||||
public BoardDTO(PieceDTO[][] board) {
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public PieceDTO[][] getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
public void setBoard(PieceDTO[][] board) {
|
||||
this.board = board;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -14,47 +15,42 @@ public class ChessEngine {
|
|||
board = new Board();
|
||||
}
|
||||
|
||||
public boolean move(MoveDTO move) {
|
||||
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
|
||||
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
|
||||
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
||||
if (board.legalMoves().contains(libMove)) {
|
||||
board.doMove(libMove);
|
||||
public boolean move(String from, String to) {
|
||||
Move move = new Move(Square.valueOf(from.toUpperCase()), Square.valueOf(to.toUpperCase()));
|
||||
if (board.legalMoves().contains(move)) {
|
||||
board.doMove(move);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<MoveDTO> getLegalDestinations(String from) {
|
||||
List<MoveDTO> destinations = new ArrayList<>();
|
||||
public List<String> getLegalDestinations(String from) {
|
||||
List<String> destinations = new ArrayList<>();
|
||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||
for (Move move : board.legalMoves()) {
|
||||
if (move.getFrom().equals(fromSq)) {
|
||||
int fromRow = 8 - fromSq.getRank().ordinal() - 1;
|
||||
int fromCol = fromSq.getFile().ordinal();
|
||||
int toRow = 8 - move.getTo().getRank().ordinal() - 1;
|
||||
int toCol = move.getTo().getFile().ordinal();;
|
||||
destinations.add(new MoveDTO(fromRow, fromCol, toRow, toCol));
|
||||
destinations.add(move.getTo().toString()); // z.B. "E4"
|
||||
}
|
||||
}
|
||||
return destinations;
|
||||
}
|
||||
|
||||
public PieceDTO getPieceAt(String square) {
|
||||
public String getPieceAt(String square) {
|
||||
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
|
||||
return convertPieceToDTO(piece);
|
||||
return piece.toString(); // z.B. "WHITE_PAWN"
|
||||
}
|
||||
|
||||
public BoardDTO getBoardAsDTO() {
|
||||
PieceDTO[][] dtoBoard = new PieceDTO[8][8];
|
||||
public String[][] getBoardUnicode() {
|
||||
String[][] unicodeBoard = new String[8][8];
|
||||
for (int rank = 8; rank >= 1; rank--) {
|
||||
for (int file = 0; file < 8; file++) {
|
||||
Square square = Square.valueOf("" + (char)('A' + file) + rank);
|
||||
Piece piece = board.getPiece(square);
|
||||
dtoBoard[8-rank][file] = convertPieceToDTO(piece);
|
||||
unicodeBoard[8-rank][file] = pieceToUnicode(piece);
|
||||
}
|
||||
}
|
||||
return new BoardDTO(dtoBoard);
|
||||
return unicodeBoard;
|
||||
}
|
||||
|
||||
private String pieceToUnicode(Piece piece) {
|
||||
|
|
@ -75,14 +71,6 @@ public class ChessEngine {
|
|||
}
|
||||
}
|
||||
|
||||
private PieceDTO convertPieceToDTO(Piece piece) {
|
||||
if (piece == null || piece.equals(Piece.NONE)) return null;
|
||||
String color = piece.name().startsWith("WHITE") ? "WHITE" : "BLACK";
|
||||
String type = piece.name().substring(piece.name().indexOf('_') + 1); // "PAWN", "KING"...
|
||||
String symbol = pieceToUnicode(piece);
|
||||
return new PieceDTO(type, color, symbol);
|
||||
}
|
||||
|
||||
public boolean isMated() {
|
||||
return board.isMated();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
public class MoveDTO {
|
||||
private int fromRow, fromCol;
|
||||
private int toRow, toCol;
|
||||
|
||||
public MoveDTO(int fromRow, int fromCol, int toRow, int toCol) {
|
||||
this.fromRow = fromRow;
|
||||
this.fromCol = fromCol;
|
||||
this.toRow = toRow;
|
||||
this.toCol = toCol;
|
||||
}
|
||||
|
||||
public int getFromRow() {
|
||||
return fromRow;
|
||||
}
|
||||
|
||||
public int getFromCol() {
|
||||
return fromCol;
|
||||
}
|
||||
|
||||
public int getToRow() {
|
||||
return toRow;
|
||||
}
|
||||
|
||||
public int getToCol() {
|
||||
return toCol;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
public class PieceDTO {
|
||||
private String type; // z.B. "KING", "PAWN"
|
||||
private String color; // "WHITE" oder "BLACK"
|
||||
private String unicodeSymbol;
|
||||
|
||||
public PieceDTO(String type, String color, String unicodeSymbol) {
|
||||
this.type = type;
|
||||
this.color = color;
|
||||
this.unicodeSymbol = unicodeSymbol;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public String getUnicodeSymbol() {
|
||||
return unicodeSymbol;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
package de.hs_mannheim.informatik.chess.view;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class MainGui {
|
||||
|
||||
private JFrame frame;
|
||||
private Runnable onStartGame;
|
||||
|
||||
public MainGui(Runnable onStartGame) {
|
||||
this.onStartGame = onStartGame;
|
||||
|
||||
frame = new JFrame("Chess - Hauptmenü");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(1600, 1000);
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
//Haupt-Panel mit GridBagLayout
|
||||
JPanel mainPanel = new JPanel(new GridBagLayout());
|
||||
mainPanel.setBackground(new Color(0xe0e1dd));
|
||||
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.gridx = 0;
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbc.insets = new Insets(15, 0, 15, 0);
|
||||
|
||||
//Title
|
||||
JLabel title = new JLabel("Chess", SwingConstants.CENTER);
|
||||
title.setFont(new Font("Serif", Font.BOLD, 150));
|
||||
title.setForeground(new Color(0x1b263b));
|
||||
gbc.gridy = 0;
|
||||
gbc.ipady = 50;
|
||||
mainPanel.add(title, gbc);
|
||||
|
||||
//Abstand nach unten
|
||||
gbc.gridy = 1;
|
||||
gbc.ipady = 15;
|
||||
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)), gbc);
|
||||
|
||||
//Buttons
|
||||
JButton btnMode1 = new JButton("Normal Mode");
|
||||
JButton btnMode2 = new JButton("Mode 2 (coming soon)");
|
||||
JButton btnMode3 = new JButton("Mode 3 (coming soon)");
|
||||
|
||||
styleButton(btnMode1);
|
||||
styleButton(btnMode2);
|
||||
styleButton(btnMode3);
|
||||
|
||||
gbc.gridy = 2; gbc.ipady = 25;
|
||||
mainPanel.add(btnMode1, gbc);
|
||||
|
||||
gbc.gridy = 3;
|
||||
mainPanel.add(btnMode2, gbc);
|
||||
|
||||
gbc.gridy = 4;
|
||||
mainPanel.add(btnMode3, gbc);
|
||||
|
||||
frame.add(mainPanel);
|
||||
frame.setVisible(true);
|
||||
|
||||
//Button ActionListener für "Normal Modus"
|
||||
btnMode1.addActionListener(e -> {
|
||||
frame.dispose(); // Hauptmenü schließen
|
||||
onStartGame.run(); // **Ruft den Callback auf**
|
||||
});
|
||||
}
|
||||
|
||||
// Helper für Button Styling
|
||||
private void styleButton(JButton btn) {
|
||||
btn.setFont(new Font("Serif", Font.BOLD, 30));
|
||||
btn.setBackground(new Color(0x778da9));
|
||||
btn.setForeground(Color.WHITE);
|
||||
btn.setFocusPainted(false);
|
||||
btn.setBorder(BorderFactory.createEmptyBorder(10, 40, 10, 40));
|
||||
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue