Compare commits

...

19 Commits

Author SHA1 Message Date
Justin fe3a5dec5b Merge branch 'ChessEngine' into Main 2025-06-18 21:19:28 +02:00
Justin 2e42de650d Add displayMessage() method to Gui 2025-06-18 20:45:21 +02:00
Justin 54453eba45 Add updateBoard() method to Gui 2025-06-18 20:41:32 +02:00
Justin 582a03178d Add getField() method to Gui 2025-06-18 20:31:27 +02:00
Justin a70a9a3eb3 Add getCurrentPlayer() method to ChessEngine 2025-06-18 19:37:22 +02:00
Justin c7a3c9918f Add isDraw() method to ChessEngine 2025-06-18 19:36:36 +02:00
Justin 5a4b3ff63e Add isStalemate() method to ChessEngine 2025-06-18 19:35:28 +02:00
Justin 3dade8ce84 Add isMated() method to ChessEngine 2025-06-18 19:33:34 +02:00
Justin 892e994739 Add getBoardUnicode() method to ChessEngine 2025-06-18 19:32:05 +02:00
Justin 0e1aec5193 Add new pieceToUnicode() method to ChessEngine 2025-06-18 19:18:07 +02:00
Justin 88f5bdc929 Add getPieceAt() method to ChessEngine 2025-06-18 19:10:51 +02:00
Justin fe8c1a65d9 Add getLegalDestinations() method to ChessEngine 2025-06-18 18:55:35 +02:00
Justin cf0a8173b4 New move method in ChessEngine 2025-06-18 18:29:02 +02:00
Justin c2e98386a8 Add ChessEngine skeleton and integrate chesslib dependency 2025-06-18 18:14:48 +02:00
Justin 987bd867c8 Add mainPanel() to arrange chess board and stats panels in GUI 2025-06-18 17:52:01 +02:00
Justin 42bbdf3e43 Add boardPanel to display chess board grid in GUI 2025-06-18 17:28:55 +02:00
Justin af8c068fce New Panel for chess statistics 2025-06-18 16:54:10 +02:00
Justin 6baea13c56 Created a new chessPanel for the chess board 2025-06-18 16:50:59 +02:00
Justin aa28f64c4f Creating a simple frame 2025-06-18 16:45:27 +02:00
4 changed files with 234 additions and 16 deletions

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hs-mannheim.informatik.schach</groupId>
@ -10,7 +11,6 @@
<name>schach</name>
<description>A simple schach.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
@ -19,16 +19,31 @@
<maven.compiler.target>8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!-- Schachlib (bhlangonijr) -->
<dependency>
<groupId>com.github.bhlangonijr</groupId>
<artifactId>chesslib</artifactId>
<version>1.3.4</version>
</dependency>
<!-- JUnit 3 (wie im Template, ggf. auf JUnit 4/5 upgraden für moderne Projekte) -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
@ -42,7 +57,6 @@
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>

View File

@ -1,5 +1,126 @@
package de.hs_mannheim.informatik.chess.gui;
public class Gui {
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Gui {
private JLabel[][] fields = new JLabel[8][8];
public Gui(){
mainFrame();
}
public JFrame mainFrame() {
JFrame frame = new JFrame();
frame.setSize(1600, 1000);
frame.setLocationRelativeTo(null);
frame.add(mainPanel());
frame.setDefaultCloseOperation(2);
frame.setVisible(true);
return frame;
}
public JPanel mainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
mainPanel.setBackground(new Color(0xe0e1dd));
// Links (Schach)
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.6;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 5, 5, 0);
//oben, links, unten, rechts
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(chessPanel(boardPanel()),gbc);
// Rechts (Stats)
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.4;
gbc.weighty = 1.0;
gbc.insets = new Insets(5, 0, 5, 5);
//oben, links, unten, rechts
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(statsPanel(), gbc);
return mainPanel;
}
public JPanel boardPanel() {
JPanel boardPanel = new JPanel(new GridLayout(8, 8));
boardPanel.setPreferredSize(new Dimension(800, 800));
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; // <-- Save the label
boardPanel.add(label);
}
}
boardPanel.setBackground(new Color(0x1b263b));
return boardPanel;
}
public JPanel chessPanel(JPanel panel) {
JPanel chessPanel = new JPanel(new GridBagLayout());
GridBagConstraints board = new GridBagConstraints();
chessPanel.setBackground(new Color(0x1b263b));
board.gridx = 0;
board.gridy = 0;
board.weightx = 0.7;
board.weighty = 1.0;
board.insets = new Insets(0, 0, 0, 0);
//oben, links, unten, rechts
board.fill = GridBagConstraints.BOTH;
chessPanel.add(panel);
return chessPanel;
}
public JPanel statsPanel() {
JPanel statsPanel = new JPanel();
statsPanel.setBackground(new Color(0x0d1b2a));
return statsPanel;
}
public JLabel getField(int row, int col) {
return fields[row][col];
}
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);
}
}

View File

@ -1,13 +1,11 @@
package de.hs_mannheim.informatik.chess.main;
import de.hs_mannheim.informatik.chess.gui.Gui;
public class Main{
public static void main( String[] args ){
new Gui();
/**
* Hello world!
*
*/
public class Main
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -1,5 +1,90 @@
package de.hs_mannheim.informatik.chess.model;
public class ChessEngine {
import java.util.ArrayList;
import java.util.List;
import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.Piece;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move;
public class ChessEngine {
private Board board;
public ChessEngine() {
board = new Board();
}
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;
}
}
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)) {
destinations.add(move.getTo().toString()); // z.B. "E4"
}
}
return destinations;
}
public String getPieceAt(String square) {
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
return piece.toString(); // z.B. "WHITE_PAWN"
}
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);
unicodeBoard[8-rank][file] = pieceToUnicode(piece);
}
}
return unicodeBoard;
}
private String pieceToUnicode(Piece piece) {
switch (piece) {
case WHITE_KING: return "♔";
case WHITE_QUEEN: return "♕";
case WHITE_ROOK: return "♖";
case WHITE_BISHOP: return "♗";
case WHITE_KNIGHT: return "♘";
case WHITE_PAWN: return "♙";
case BLACK_KING: return "♚";
case BLACK_QUEEN: return "♛";
case BLACK_ROOK: return "♜";
case BLACK_BISHOP: return "♝";
case BLACK_KNIGHT: return "♞";
case BLACK_PAWN: return "♟";
default: return " ";
}
}
public boolean isMated() {
return board.isMated();
}
public boolean isStalemate() {
return board.isStaleMate();
}
public boolean isDraw() {
return board.isDraw();
}
public String getCurrentPlayer() {
return board.getSideToMove().toString(); // "WHITE" oder "BLACK"
}
}