diff --git a/src/main/java/de/mannheim/th/chess/domain/Game.java b/src/main/java/de/mannheim/th/chess/domain/Game.java index a375d29..4d1ada5 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -10,6 +10,7 @@ import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.MoveList; import com.github.bhlangonijr.chesslib.pgn.PgnHolder; +import de.mannheim.th.chess.ui.SpielFrame; import de.mannheim.th.chess.utl.Clock; /** @@ -18,142 +19,155 @@ import de.mannheim.th.chess.utl.Clock; */ public class Game { - private Board board; - private Clock clock; + private Board board; + private Clock clock; + private SpielFrame sp; + private String modus; + private boolean rotieren, zuruecknahme; - private MoveList movelist; + private MoveList movelist; - /** - * Conststructs a new standard GameBoard. - */ - public Game() { - this.board = new Board(); + /** + * Conststructs a new standard GameBoard. + */ + public Game(String modus, boolean rotieren, boolean zuruecknahme) { + this.modus = modus; + this.rotieren = rotieren; + this.zuruecknahme = zuruecknahme; + + + this.board = new Board(); - this.movelist = new MoveList(); + this.movelist = new MoveList(); + + clock = new Clock(modus); + + sp = new SpielFrame(this); - clock = new Clock("blitz"); - clock.start(); + - } + } - /** - * Constructs a new standard GameBoard and applies the provides moves. - * - * @param movelist The list of moves that get played. - */ - public Game(MoveList movelist) { - this.board = new Board(); + /** + * Constructs a new standard GameBoard and applies the provides moves. + * + * @param movelist The list of moves that get played. + */ + public Game(MoveList movelist) { + this.board = new Board(); - this.movelist = movelist; + this.movelist = movelist; - for (Move move : movelist) { - this.board.doMove(move); - } + for (Move move : movelist) { + this.board.doMove(move); + } - // this.clockPlayer1 = new Clock(); - // this.clockPlayer2 = new Clock(); - } + // this.clockPlayer1 = new Clock(); + // this.clockPlayer2 = new Clock(); + } - /** - * Constructs a new GameBoard with the provided fen String as the positions. - * - * @param fen The fen String that provides the customs formation. - */ - public Game(String fen) { - this.board = new Board(); - this.board.loadFromFen(fen); + /** + * Constructs a new GameBoard with the provided fen String as the positions. + * + * @param fen The fen String that provides the customs formation. + */ + public Game(String fen) { + this.board = new Board(); + this.board.loadFromFen(fen); - this.movelist = new MoveList(); + this.movelist = new MoveList(); + //this.sp = new SpielFrame(); - // this.clockPlayer1 = new Clock(); - // this.clockPlayer2 = new Clock(); - } + // this.clockPlayer1 = new Clock(); + // this.clockPlayer2 = new Clock(); + } - /** - * Plays the move on the board and adds it to the movelist - * - * @param move the move to be played - */ - public void playMove(Move move) { - this.board.doMove(move); - this.movelist.add(move); - clock.pressClock(); - } + /** + * Plays the move on the board and adds it to the movelist + * + * @param move the move to be played + */ + public void playMove(Move move) { + this.board.doMove(move); + this.movelist.add(move); + clock.pressClock(); + } - /** - * Plays the move on the board and adds it to the movelist - * - * @param origin The square from wich it moves from. - * @param desination The square where it will move to. - */ - public void playMove(Square origin, Square desination) { - Move move = new Move(origin, desination); - this.board.doMove(move); - this.movelist.add(move); - - } - - public boolean isMate() { - return board.isMated(); - } - - public boolean isDraw() { - return board.isDraw(); - } - - public int getActivePlayer() { - if (board.getSideToMove() == Side.WHITE) { - return 1; - } - return 2; - } + /** + * Plays the move on the board and adds it to the movelist + * + * @param origin The square from wich it moves from. + * @param desination The square where it will move to. + */ + public void playMove(Square origin, Square desination) { + Move move = new Move(origin, desination); + this.board.doMove(move); + this.movelist.add(move); - /** - * Retrieves a list of legal moves originating from the specified square. - * - * @param square The square from which to retrieve legal moves. - * - * @return A list of legal moves that originate from the specified square. - */ - public List getLegalMoves(Square square) { - return this.board.legalMoves().stream() - .filter(move -> move.getFrom() == square) - .collect(Collectors.toList()); + } - } - - public void stopClock() { - clock.endGame(); - } + public boolean isMate() { + return board.isMated(); + } - /** - * Retrieves a list of all legal moveable squares from the current board state. - * - * @return a List of Square objects representing all legal moveable squares. - */ - public List getAllLegalMoveableSquares() { - return this.board.legalMoves().stream() - .map(move -> move.getFrom()) - .distinct() - .collect(Collectors.toList()); - } + public boolean isDraw() { + return board.isDraw(); + } - /** - * Retrieves a list of legal moveable squares for a given square. - * - * @param square the Square from which to retrieve legal moveable squares - * @return a List of Square objects representing the legal moveable squares - * from the specified square. - */ - public List getLegalMoveableSquares(Square square) { - return this.board.legalMoves().stream() - .filter(move -> move.getFrom() == square) - .map(move -> move.getTo()) - .collect(Collectors.toList()); - } + public int getActivePlayer() { + if (board.getSideToMove() == Side.WHITE) { + return 1; + } + return 2; + } - public String toFEN() { - board.toString(); - return board.getFen(); - } + /** + * Retrieves a list of legal moves originating from the specified square. + * + * @param square The square from which to retrieve legal moves. + * + * @return A list of legal moves that originate from the specified square. + */ + public List getLegalMoves(Square square) { + return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).collect(Collectors.toList()); + + } + + public void stopClock() { + clock.endGame(); + } + + /** + * Retrieves a list of all legal moveable squares from the current board state. + * + * @return a List of Square objects representing all legal moveable squares. + */ + public List getAllLegalMoveableSquares() { + return this.board.legalMoves().stream().map(move -> move.getFrom()).distinct().collect(Collectors.toList()); + } + + /** + * Retrieves a list of legal moveable squares for a given square. + * + * @param square the Square from which to retrieve legal moveable squares + * @return a List of Square objects representing the legal moveable squares from + * the specified square. + */ + public List getLegalMoveableSquares(Square square) { + return this.board.legalMoves().stream().filter(move -> move.getFrom() == square).map(move -> move.getTo()) + .collect(Collectors.toList()); + } + + public String toFEN() { + board.toString(); + return board.getFen(); + } + + public void setModus(String modus) { + this.modus = modus; + } + + public Clock getClock() { + return this.clock; + } } diff --git a/src/main/java/de/mannheim/th/chess/ui/MainFrame.java b/src/main/java/de/mannheim/th/chess/ui/MainFrame.java index fc5a437..940cadc 100644 --- a/src/main/java/de/mannheim/th/chess/ui/MainFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/MainFrame.java @@ -22,8 +22,6 @@ import java.awt.Color; public class MainFrame extends JFrame { - private ArrayList spiele = new ArrayList<>(); - private static final long serialVersionUID = 1L; private JPanel contentPane; @@ -76,8 +74,7 @@ public class MainFrame extends JFrame { @Override public void actionPerformed(ActionEvent e) { - SpielFrame sp = new SpielFrame(); - spiele.add(sp); + ModeSelectionFrame ms = new ModeSelectionFrame(); } @@ -115,7 +112,7 @@ public class MainFrame extends JFrame { contentPane.add(Box.createVerticalStrut(15)); - JButton btnNewButton_2 = new JButton("Spiel beenden"); + JButton btnNewButton_2 = new JButton("App beenden"); btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.setForeground(Color.BLACK); diff --git a/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java b/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java new file mode 100644 index 0000000..a83f4a7 --- /dev/null +++ b/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java @@ -0,0 +1,107 @@ +package de.mannheim.th.chess.ui; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import javax.swing.*; +import javax.swing.border.EmptyBorder; + +import de.mannheim.th.chess.domain.Game; + +public class ModeSelectionFrame extends JFrame { + + private static final long serialVersionUID = 1L; + private final JPanel contentPane; + private final ArrayList spiele = new ArrayList<>(); + + public ModeSelectionFrame() { + // Frame-Eigenschaften + setTitle("Modusauswahl"); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setBounds(100, 100, 500, 500); + setResizable(true); + setAlwaysOnTop(true); + + // Panel konfigurieren + contentPane = new JPanel(); + contentPane.setBackground(new Color(90, 90, 90)); + contentPane.setBorder(new EmptyBorder(20, 20, 20, 20)); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + setContentPane(contentPane); + + // Überschrift + JLabel jl = new JLabel("Welchen Modus wollen Sie spielen?"); + jl.setFont(new Font("Calibri", Font.BOLD, 20)); + jl.setForeground(Color.BLACK); + jl.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl); + contentPane.add(Box.createVerticalStrut(15)); + + // Modusauswahl + String[] modi = {"Blitz", "Schnellschach", "Klassisch"}; + JComboBox jcb1 = new JComboBox<>(modi); + jcb1 .setMaximumSize(new Dimension(150, 30)); + jcb1 .setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jcb1 ); + contentPane.add(Box.createVerticalStrut(15)); + + // Spielbrettdrehen + JLabel jl2 = new JLabel("Soll das Spielbrett nach jedem Zug gedreht werden?"); + jl2 .setFont(new Font("Calibri", Font.BOLD, 20)); + jl2 .setForeground(Color.BLACK); + jl2 .setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl2 ); + + JCheckBox jb1 = new JCheckBox(); + jb1.setOpaque(false); + jb1.setFocusPainted(false); + jb1.setForeground(Color.BLACK); + jb1 .setAlignmentX(Component.CENTER_ALIGNMENT); + jb1 .setMaximumSize(new Dimension(30, 30)); + contentPane.add(jb1 ); + contentPane.add(Box.createVerticalStrut(15)); + + // Zurücknahmeoption + JLabel jl3 = new JLabel("Sollen Zurücknahmen erlaubt sein?"); + jl3.setFont(new Font("Calibri", Font.BOLD, 20)); + jl3.setForeground(Color.BLACK); + jl3.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl3); + + JCheckBox jb2 = new JCheckBox(); + jb2.setOpaque(false); + jb2.setFocusPainted(false); + jb2.setForeground(Color.BLACK); + jb2.setAlignmentX(Component.CENTER_ALIGNMENT); + jb2.setMaximumSize(new Dimension(30, 30)); + contentPane.add(jb2); + contentPane.add(Box.createVerticalStrut(25)); + + // Spiel starten Button + JButton btnNewButton = new JButton("Spiel starten"); + btnNewButton .setBackground(Color.LIGHT_GRAY); + btnNewButton .setForeground(Color.BLACK); + btnNewButton .setFont(new Font("Tahoma", Font.BOLD, 16)); + btnNewButton .setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(btnNewButton ); + + // Button-Listener + btnNewButton .addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String modus = (String) jcb1.getSelectedItem(); + boolean rotieren = jb1.isSelected(); + boolean zuruecknahme = jb2.isSelected(); + + Game game = new Game(modus, rotieren, zuruecknahme); + + spiele.add(game); + + dispose(); + } + }); + + setVisible(true); + } +} diff --git a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java index 6fc32d8..3d722e2 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -8,14 +8,16 @@ import com.github.bhlangonijr.chesslib.move.Move; import de.mannheim.th.chess.App; import de.mannheim.th.chess.domain.Game; +import de.mannheim.th.chess.utl.Clock; import de.mannheim.th.chess.controller.ButtonMovePieceListener; import de.mannheim.th.chess.controller.ButtonSelectPieceListener; import de.mannheim.th.chess.controller.ButtonToNormalListener; -import java.awt.EventQueue; import java.awt.Font; import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; @@ -25,6 +27,7 @@ import javax.swing.JSplitPane; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -39,6 +42,7 @@ public class SpielFrame extends JFrame { private HashMap belegungen = new HashMap<>(); private JPanel panelLinks, panelRechts, contentPane; private Game game; + private Clock clock; private BoardMode mode; private Square selectedSquare; @@ -50,12 +54,15 @@ public class SpielFrame extends JFrame { /** * Create the frame. */ - public SpielFrame() { + public SpielFrame(Game game) { - game = new Game(); + this.game = game; + this.clock = game.getClock(); + this.clock.start(); + mode = BoardMode.normal; - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 1920, 1080); setTitle("Schach"); setAlwaysOnTop(true); @@ -71,15 +78,43 @@ public class SpielFrame extends JFrame { // Rechtes Panel für Steuerung oder zusätzliche Eingaben panelRechts = new JPanel(); - panelRechts.setBackground(Color.LIGHT_GRAY); - + panelRechts.setBackground(new Color(90,90,90)); + panelRechts.setLayout(new BoxLayout(panelRechts, BoxLayout.Y_AXIS)); + + + JLabel pl1 = new JLabel("Player 1:"); + pl1.setFont(new Font("Calibri", Font.BOLD, 35)); + pl1.setForeground(Color.BLACK); + pl1.setAlignmentX(Component.LEFT_ALIGNMENT); + panelRechts.add(pl1); + + contentPane.add(Box.createVerticalStrut(15)); + + //Zeitangabe Player1 + JLabel clock1 = clock.getClock2(); + panelRechts.add(clock1); + + contentPane.add(Box.createVerticalStrut(15)); + + JLabel pl2 = new JLabel("Player 2:"); + pl2.setFont(new Font("Calibri", Font.BOLD, 35)); + pl2.setForeground(Color.BLACK); + pl2.setAlignmentX(Component.LEFT_ALIGNMENT); + panelRechts.add(pl2); + + //Zeitangabe Player2 + JLabel clock2 = clock.getClock1(); + panelRechts.add(clock2); + // JSplitPane horizontal (linke und rechte Hälfte) JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLinks, panelRechts); - splitPane.setResizeWeight(0.70); - splitPane.setDividerSize(5); + splitPane.setResizeWeight(0.65); + splitPane.setBackground(Color.BLACK); + splitPane.setDividerSize(0); splitPane.setEnabled(false); contentPane.add(splitPane, BorderLayout.CENTER); + setVisible(true); } @@ -109,32 +144,6 @@ public class SpielFrame extends JFrame { panelLinks.revalidate(); panelLinks.repaint(); - // // Bild laden und Cursor im gesamten Frame setzen - // Image image = Toolkit.getDefaultToolkit().getImage(pfad); - // Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH); - // Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, - // new Point(0, 0), - // "figurCursor"); - // setCursor(figurCursor); - - // }else - // { - // - // // wenn gerade Figur ausgewählt wird... - // buttonChoosed = (JButton) e.getSource(); - // symbolChoosed = belegungen.get(buttonChoosed); - // // System.out.println(symbolChoosed+" wurde gewählt."); - // // setzt cursor auf spielfigur für die animation - // String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + - // ".png"; - // - // // Bild laden und Cursor im gesamten Frame setzen - // Image image = Toolkit.getDefaultToolkit().getImage(pfad); - // Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH); - // Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, - // new Point(0, 0), - // "figurCursor"); - // setCursor(figurCursor); } private int mirrowedGrid(int i) { @@ -205,10 +214,10 @@ public class SpielFrame extends JFrame { for (int i = 0; i < 64; i++) { JButton b = buttons.get(i); if ((i / 8 + i % 8) % 2 == 0) { - logger.info("Helles Feld erstellt." + i); + //logger.info("Helles Feld erstellt." + i); b.setBackground(new Color(90, 90, 90)); } else { - logger.info("Dunkles Feld erstellt." + i); + //logger.info("Dunkles Feld erstellt." + i); b.setBackground(new Color(65, 65, 65)); } } diff --git a/src/main/java/de/mannheim/th/chess/utl/Clock.java b/src/main/java/de/mannheim/th/chess/utl/Clock.java index c575d06..84d794b 100644 --- a/src/main/java/de/mannheim/th/chess/utl/Clock.java +++ b/src/main/java/de/mannheim/th/chess/utl/Clock.java @@ -17,17 +17,21 @@ import javax.swing.Timer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import de.mannheim.th.chess.ui.SpielFrame; + public class Clock extends Thread implements Runnable { private volatile boolean whiteToMove = true; private volatile boolean gameHasFinished = false; private static final Logger clockLogger = LogManager.getLogger(Clock.class); private int minutes; - + private StringBuilder clockShower; + private JLabel clock1, clock2; + public Clock(String mode) { + setMode(mode); - //run(); } - + public void pressClock() { whiteToMove = !whiteToMove; if (whiteToMove) { @@ -36,105 +40,109 @@ public class Clock extends Thread implements Runnable { clockLogger.info("Schwarz ist am Zug"); } } - + public void endGame() { gameHasFinished = true; } - + public void run() { - JFrame clockFrame = new JFrame("Clock"); - - JPanel player1Panel = new JPanel(); - player1Panel.setBackground(Color.BLACK); - JPanel player2Panel = new JPanel(); - player2Panel.setBackground(Color.BLACK); - clockFrame.setBounds(1000, 500, 10000, 10000); - clockFrame.setLayout(new BorderLayout()); - JLabel clock1 = new JLabel(" " + minutes + ":00 "); - clock1.setForeground(Color.WHITE); - clock1.setFont(new Font("Arial", Font.BOLD, 50)); - JLabel clock2 = new JLabel(" " + minutes + ":00 "); - clock2.setForeground(Color.WHITE); - clock2.setFont(new Font("Arial", Font.BOLD, 50)); - player1Panel.add(clock1); - player2Panel.add(clock2); - JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, player1Panel, player2Panel); - split.setFont(new Font("Arial", Font.BOLD, 50)); - clockFrame.add(split); - +// JFrame clockFrame = new JFrame("Clock"); +// +// JPanel player1Panel = new JPanel(); +// player1Panel.setBackground(Color.BLACK); +// JPanel player2Panel = new JPanel(); +// player2Panel.setBackground(Color.BLACK); +// clockFrame.setBounds(1000, 500, 10000, 10000); +// clockFrame.setLayout(new BorderLayout()); + clock1 = new JLabel(" " + minutes + ":00 "); + clock1.setForeground(Color.BLACK); + clock1.setFont(new Font("Calibri", Font.BOLD, 35)); + clock2 = new JLabel(" " + minutes + ":00 "); + clock2.setForeground(Color.BLACK); + clock2.setFont(new Font("Calibri", Font.BOLD, 35)); +// player1Panel.add(clock1); +// player2Panel.add(clock2); +// JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, player1Panel, player2Panel); +// split.setFont(new Font("Arial", Font.BOLD, 50)); +// clockFrame.add(split); + var min1 = new AtomicInteger(minutes); var sec1 = new AtomicInteger(0); var min2 = new AtomicInteger(minutes); var sec2 = new AtomicInteger(0); - clockFrame.pack(); - clockFrame.setVisible(true); var t = new Timer(1000, (ae) -> { - - if (!gameHasFinished) { - - StringBuilder clockShower = new StringBuilder(); - if (whiteToMove) { - if (sec1.intValue() == 00) { - sec1.set(60); - min1.decrementAndGet(); - } - if (min1.intValue() < 10) { - clockShower.append("0"); - } - clockShower.append(min1.get()); - clockShower.append(":"); - if (sec1.intValue() < 10) { - clockShower.append("0"); - } - clockShower.append(sec1.decrementAndGet()); - clock1.setText(clockShower.toString()); - } else { - if (sec2.intValue() == 00) { - sec2.set(60); - min2.decrementAndGet(); + if (!gameHasFinished) { + + clockShower = new StringBuilder(); + if (whiteToMove) { + if (sec1.intValue() == 00) { + sec1.set(60); + min1.decrementAndGet(); + } + if (min1.intValue() < 10) { + clockShower.append("0"); + } + clockShower.append(min1.get()); + clockShower.append(":"); + if (sec1.intValue() < 10) { + clockShower.append("0"); + } + clockShower.append(sec1.decrementAndGet()); + clock1.setText(clockShower.toString()); + + } else { + if (sec2.intValue() == 00) { + sec2.set(60); + min2.decrementAndGet(); + } + if (min2.intValue() < 10) { + clockShower.append("0"); + } + clockShower.append(min2.get()); + clockShower.append(":"); + if (sec2.intValue() < 10) { + clockShower.append("0"); + } + clockShower.append(sec2.decrementAndGet()); + clock2.setText(clockShower.toString()); } - if (min2.intValue() < 10) { - clockShower.append("0"); + //sp.repaint(); + if ((sec1.intValue() == 0 && min1.intValue() == 0) || (sec2.intValue() == 0 && min2.intValue() == 0)) { + endGame(); } - clockShower.append(min2.get()); - clockShower.append(":"); - if (sec2.intValue() < 10) { - clockShower.append("0"); - } - clockShower.append(sec2.decrementAndGet()); - clock2.setText(clockShower.toString()); } - clockFrame.repaint(); - if ((sec1.intValue() == 0 && min1.intValue() == 0) || (sec2.intValue() == 0 && min2.intValue() == 0)) { - endGame(); - } - } }); + }); t.start(); } - public static void main(String[] args) throws InterruptedException { - Clock st = new Clock("classic"); - st.start(); - st.pressClock(); - } - + private void setMode(String mode) { - switch(mode) { + switch (mode.toLowerCase()) { case "blitz": minutes = 5; clockLogger.info("Neue Blitz-Uhr wurde erstellt"); break; - case "rapid": + case "schnellschach": minutes = 10; clockLogger.info("Neue Schnellschach-Uhr wurde erstellt"); break; - case "classic": + case "klassisch": minutes = 120; clockLogger.info("Neue klassische Schachuhr wurde erstellt"); break; } } + public JLabel getClock1() { + + return clock1; + } + + public JLabel getClock2() { + + return clock2; + } + } \ No newline at end of file