Compare commits

...

10 Commits

Author SHA1 Message Date
Justin e6e1fa3187 Changed design in controller 2025-06-20 03:26:14 +02:00
Justin 6374c206de Reworked statsPanel and added new Buttons in GameGui 2025-06-20 03:09:17 +02:00
Justin 543caeb366 Bigger chess figures 2025-06-20 02:54:46 +02:00
Justin db7e2a9f5a New initFields method in GameGui 2025-06-20 02:51:11 +02:00
Justin 7394548cc5 Added code to fill JScrollPane in Controller 2025-06-20 02:26:44 +02:00
Justin 33dc7623f4 New method getMoveListStringsGrouped in ChessEngine 2025-06-20 02:20:28 +02:00
Justin 2dab6128b4 Added a List to get all Moves in ChessEngine 2025-06-20 02:17:17 +02:00
Justin 47591a7264 New method updateMoveList to fill JScrollPane 2025-06-20 01:58:28 +02:00
Justin d1d85ed428 Added JScrollPane to statsPanel for statistics 2025-06-20 01:53:30 +02:00
Justin 28496459cf Changing name from Chess to ChessDE in MainGui 2025-06-20 01:49:59 +02:00
4 changed files with 100 additions and 14 deletions

View File

@ -86,7 +86,7 @@ public class Controller {
selectedRow = modelRow;
selectedCol = modelCol;
gui.getField(guiRow, guiCol).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
gui.getField(guiRow, guiCol).setBorder(BorderFactory.createLineBorder(new Color(0x1b263b), 7));
String fromSquare = coordToChessNotation(modelRow, modelCol);
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
@ -94,7 +94,7 @@ public class Controller {
for (MoveDTO move : moves) {
int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
gui.getField(guiToRow, guiToCol).setBackground(new Color( 27, 38, 59 ));
highlightedFields.add(new int[]{guiToRow, guiToCol});
}
} else {
@ -113,13 +113,13 @@ public class Controller {
}
}
public void handleMove(MoveDTO move) {
if (engine.move(move)) {
updateGuiBoard();
//Züge in der MoveList aktualisieren
gui.updateMoveList(engine.getMoveListStringsGrouped());
//Spielstatus prüfen
if (engine.isMated()) {
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";

View File

@ -9,6 +9,7 @@ import com.github.bhlangonijr.chesslib.move.Move;
public class ChessEngine {
private Board board;
private List<Move> moves = new ArrayList<>();
public ChessEngine() {
board = new Board();
@ -20,6 +21,7 @@ public class ChessEngine {
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
if (board.legalMoves().contains(libMove)) {
board.doMove(libMove);
moves.add(libMove); // <-- hier merken!
return true;
}
return false;
@ -40,6 +42,20 @@ public class ChessEngine {
return destinations;
}
public List<String> getMoveListStringsGrouped() {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < moves.size(); i++) {
if (i % 2 == 0) sb.append((i/2 + 1) + ". ");
sb.append(moves.get(i).toString()).append(" ");
if (i % 2 == 1 || i == moves.size() - 1) {
result.add(sb.toString().trim());
sb = new StringBuilder();
}
}
return result;
}
public PieceDTO getPieceAt(String square) {
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
return convertPieceToDTO(piece);

View File

@ -1,5 +1,6 @@
package de.hs_mannheim.informatik.chess.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
@ -7,12 +8,15 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import de.hs_mannheim.informatik.chess.model.BoardDTO;
@ -24,7 +28,11 @@ public class GameGui {
private JButton flipBoardButton;
private boolean isFlipped = false;
private JPanel moveListPanel;
private JScrollPane moveListScroll;
public GameGui(){
initFields();
mainFrame();
}
@ -40,6 +48,22 @@ public class GameGui {
return frame;
}
private void initFields() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 40));
if ((row + col) % 2 == 0) {
label.setBackground(new Color(0x778da9));
} else {
label.setBackground(new Color(0xe0e1dd));
}
fields[row][col] = label;
}
}
}
public JPanel mainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
@ -52,7 +76,7 @@ public class GameGui {
gbc.insets = new Insets(5, 5, 5, 0);
//oben, links, unten, rechts
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(chessPanel(boardPanel()),gbc);
mainPanel.add(chessPanel(boardPanel()), gbc);
// Rechts (Stats)
gbc.gridx = 1;
@ -77,7 +101,7 @@ public class GameGui {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setOpaque(true);
label.setFont(new Font("Serif", Font.BOLD, 40));
label.setFont(new Font("Serif", Font.BOLD, 70));
if ((row + col) % 2 == 0) {
label.setBackground(new Color(0x778da9));
} else {
@ -126,9 +150,55 @@ public class GameGui {
}
public JPanel statsPanel() {
JPanel statsPanel = new JPanel();
statsPanel.setBackground(new Color(0x0d1b2a));
return statsPanel;
JPanel statsPanel = new JPanel(new BorderLayout());
statsPanel.setBackground(new Color(0x0d1b2a));
// Move-Liste (scrollbar, wie gehabt)
moveListPanel = new JPanel();
moveListPanel.setLayout(new BoxLayout(moveListPanel, BoxLayout.Y_AXIS));
moveListPanel.setBackground(new Color(0x0d1b2a));
moveListScroll = new JScrollPane(moveListPanel);
moveListScroll.setPreferredSize(new Dimension(250, 800));
statsPanel.add(moveListScroll, BorderLayout.CENTER);
// Button-Leiste
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(0x0d1b2a));
// Grid oder Flow je nach Geschmack
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
JButton btnFirst = new JButton("|<");
JButton btnPrev = new JButton("<");
JButton btnNext = new JButton(">");
JButton btnLast = new JButton(">|");
// Style (optional)
btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE);
btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE);
btnNext.setBackground(new Color(0x212529)); btnNext.setForeground(Color.WHITE);
btnLast.setBackground(new Color(0x212529)); btnLast.setForeground(Color.WHITE);
// Hinzufügen
buttonPanel.add(btnFirst);
buttonPanel.add(btnPrev);
buttonPanel.add(btnNext);
buttonPanel.add(btnLast);
// Unten ins BorderLayout
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
return statsPanel;
}
public void updateMoveList(List<String> moves) {
moveListPanel.removeAll();
for (String move : moves) {
JLabel moveLabel = new JLabel(move);
moveLabel.setForeground(Color.WHITE);
moveLabel.setFont(new Font("SansSerif", Font.PLAIN, 18));
moveListPanel.add(moveLabel);
}
moveListPanel.revalidate();
moveListPanel.repaint();
}
public JLabel getField(int row, int col) {

View File

@ -26,7 +26,7 @@ public class MainGui {
gbc.insets = new Insets(15, 0, 15, 0);
//Title
JLabel title = new JLabel("Chess", SwingConstants.CENTER);
JLabel title = new JLabel("ChessDE", SwingConstants.CENTER);
title.setFont(new Font("Serif", Font.BOLD, 150));
title.setForeground(new Color(0x1b263b));
gbc.gridy = 0;