Statistik ausgabe finished. Spiel laden und speichern funktioniert.

devUi
Matias 2025-06-21 15:28:32 +02:00
parent 0a46fc763f
commit cf4e465b10
13 changed files with 239 additions and 85 deletions

View File

@ -1,2 +1,2 @@
JFileChoser in UI: (mit GPT) JFileChoser in UI: (mit GPT)
Prompt: "Wie kann ich in swing in java Files aus einem Explorer auswählen?". Prompt: "Wie kann ich in swing in java Files aus einem Explorer auswählen und speichern?".

View File

@ -20,7 +20,6 @@ public class App {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
logger.info("Hello World.");
userinterface = new MainFrame(); userinterface = new MainFrame();
} }
} }

View File

@ -0,0 +1,22 @@
package de.mannheim.th.chess.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class ButtonAufgebenListener extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public ButtonAufgebenListener() {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,54 @@
package de.mannheim.th.chess.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import de.mannheim.th.chess.ui.ModeSelectionFrame;
public class ButtonFileLoaderListener implements ActionListener{
private ModeSelectionFrame msf;
public ButtonFileLoaderListener(ModeSelectionFrame msf) {
this.msf = msf;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser dateiWaehler = new JFileChooser();
JFrame jfFile = new JFrame();
int auswahl = dateiWaehler.showOpenDialog(jfFile);
if (auswahl == JFileChooser.APPROVE_OPTION) {
File ausgewaehlteDatei = dateiWaehler.getSelectedFile();
JOptionPane.showMessageDialog(jfFile, "Gewählte Datei:\n" + ausgewaehlteDatei.getAbsolutePath());
try {
BufferedReader br = new BufferedReader(new FileReader(ausgewaehlteDatei));
msf.setFen(br.readLine());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,61 @@
package de.mannheim.th.chess.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.move.Move;
import de.mannheim.th.chess.App;
import de.mannheim.th.chess.domain.Game;
public class ButtonFileSaverListener implements ActionListener{
private static final Logger logger = LogManager.getLogger(App.class);
private Game g;
private JFrame sf;
public ButtonFileSaverListener(JFrame sf, Game g) {
this.sf = sf;
this.g = g;
}
@Override
public void actionPerformed(ActionEvent e) {
logger.info("Spiel wird gespeichert.");
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.home") + "/Documents"));
chooser.setDialogTitle("Datei speichern");
int userSelection = chooser.showSaveDialog(sf);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = chooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileToSave))) {
writer.write(g.getFen());
logger.info(g.getFen());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
logger.info("Speichern fehlgeschlagen.");
}
}
}

View File

@ -24,8 +24,6 @@ public class ButtonMovePieceListener implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
this.game.playMove(this.mv); this.game.playMove(this.mv);
sf.getUndo().setText("Zug zurücknehmen");
sf.getUndo2().setText("Zug zurücknehmen");
if (this.game.isDraw()) { if (this.game.isDraw()) {
this.game.stopClock(); this.game.stopClock();
@ -39,5 +37,11 @@ public class ButtonMovePieceListener implements ActionListener {
this.sf.setBoardMode(BoardMode.normal); this.sf.setBoardMode(BoardMode.normal);
this.sf.setCursor(null); this.sf.setCursor(null);
this.sf.erstelleBrett(); this.sf.erstelleBrett();
if (game.getLastMove() != null) {
char[] z = game.getLastMove().toString().toCharArray();
String moveString = String.valueOf(z[0]) + String.valueOf(z[1]) + " -> " + String.valueOf(z[2]) + String.valueOf(z[3]);
sf.appendText(moveString);
}
} }
} }

View File

@ -3,6 +3,7 @@ package de.mannheim.th.chess.controller;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.ui.SpielFrame; import de.mannheim.th.chess.ui.SpielFrame;
import de.mannheim.th.chess.ui.SpielFrame.BoardMode; import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
@ -19,6 +20,7 @@ public class ButtonToNormalListener implements ActionListener {
this.sf.setSelectedSquare(null); this.sf.setSelectedSquare(null);
this.sf.setCursor(null); this.sf.setCursor(null);
this.sf.erstelleBrett(); this.sf.erstelleBrett();
} }
} }

View File

@ -45,6 +45,7 @@ public class ButtonUndoMoveListener implements ActionListener {
sf.getUndo2().setEnabled(false); sf.getUndo2().setEnabled(false);
game.undo(); game.undo();
sf.getClock().switchClock(); sf.getClock().switchClock();
sf.deleteLastAusgabe();
sf.erstelleBrett(); sf.erstelleBrett();
} }
} else if (source == sf.getUndo2()) { // Spieler 1 drückt seinen Button } else if (source == sf.getUndo2()) { // Spieler 1 drückt seinen Button
@ -59,6 +60,7 @@ public class ButtonUndoMoveListener implements ActionListener {
sf.getUndo().setEnabled(false); sf.getUndo().setEnabled(false);
game.undo(); game.undo();
sf.getClock().switchClock(); sf.getClock().switchClock();
sf.deleteLastAusgabe();
sf.erstelleBrett(); sf.erstelleBrett();
} }
} }

View File

@ -3,6 +3,9 @@ package de.mannheim.th.chess.domain;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Board; import com.github.bhlangonijr.chesslib.Board;
import com.github.bhlangonijr.chesslib.Side; import com.github.bhlangonijr.chesslib.Side;
import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.Square;
@ -10,6 +13,7 @@ import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList; import com.github.bhlangonijr.chesslib.move.MoveList;
import com.github.bhlangonijr.chesslib.pgn.PgnHolder; import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
import de.mannheim.th.chess.App;
import de.mannheim.th.chess.ui.SpielFrame; import de.mannheim.th.chess.ui.SpielFrame;
import de.mannheim.th.chess.utl.Clock; import de.mannheim.th.chess.utl.Clock;
@ -18,6 +22,8 @@ import de.mannheim.th.chess.utl.Clock;
* Steuerung davon. * Steuerung davon.
*/ */
public class Game { public class Game {
private static final Logger logger = LogManager.getLogger(App.class);
private Board board; private Board board;
private Clock clock; private Clock clock;
@ -30,13 +36,16 @@ public class Game {
/** /**
* Conststructs a new standard GameBoard. * Conststructs a new standard GameBoard.
*/ */
public Game(String modus, boolean rotieren, boolean zuruecknahme) { public Game(String modus, boolean rotieren, boolean zuruecknahme, String fen) {
this.modus = modus; this.modus = modus;
this.rotieren = rotieren; this.rotieren = rotieren;
this.zuruecknahme = zuruecknahme; this.zuruecknahme = zuruecknahme;
this.board = new Board(); this.board = new Board();
if(fen == null) fen = board.getFen();
this.board.loadFromFen(fen);
this.movelist = new MoveList(); this.movelist = new MoveList();
@ -44,8 +53,6 @@ public class Game {
sp = new SpielFrame(this); sp = new SpielFrame(this);
} }
/** /**
@ -186,4 +193,13 @@ public class Game {
} }
return false; return false;
} }
public String getFen() {
return this.board.getFen();
}
public Move getLastMove() {
logger.info(this.movelist.getLast().toString());
return this.movelist.getLast();
}
} }

View File

@ -7,7 +7,7 @@ import de.mannheim.th.chess.domain.Game;
*/ */
public class GameWindow{ public class GameWindow{
private Game gamelogic = new Game(); //private Game gamelogic = new Game();
public GameWindow() { public GameWindow() {

View File

@ -3,12 +3,23 @@ package de.mannheim.th.chess.ui;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.mannheim.th.chess.App;
import de.mannheim.th.chess.domain.Game;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.Box; import javax.swing.Box;
@ -21,6 +32,8 @@ import java.awt.Font;
import java.awt.Color; import java.awt.Color;
public class MainFrame extends JFrame { public class MainFrame extends JFrame {
private static final Logger logger = LogManager.getLogger(App.class);
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private JPanel contentPane; private JPanel contentPane;
@ -83,35 +96,6 @@ public class MainFrame extends JFrame {
contentPane.add(Box.createVerticalStrut(15)); contentPane.add(Box.createVerticalStrut(15));
JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden");
btnNewButton_1.setBackground(Color.LIGHT_GRAY);
btnNewButton_1.setForeground(Color.BLACK);
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
btnNewButton_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser dateiWaehler = new JFileChooser();
JFrame jfFile = new JFrame();
int auswahl = dateiWaehler.showOpenDialog(jfFile);
if (auswahl == JFileChooser.APPROVE_OPTION) {
File ausgewaehlteDatei = dateiWaehler.getSelectedFile();
JOptionPane.showMessageDialog(jfFile, "Gewählte Datei:\n" + ausgewaehlteDatei.getAbsolutePath());
// Uebergabe zu Logik zum extrahieren der Daten
}
}
});
contentPane.add(btnNewButton_1);
contentPane.add(Box.createVerticalStrut(15));
JButton btnNewButton_2 = new JButton("App beenden"); JButton btnNewButton_2 = new JButton("App beenden");
btnNewButton_2.setBackground(Color.LIGHT_GRAY); btnNewButton_2.setBackground(Color.LIGHT_GRAY);

View File

@ -3,17 +3,30 @@ package de.mannheim.th.chess.ui;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.mannheim.th.chess.App;
import de.mannheim.th.chess.controller.ButtonFileLoaderListener;
import de.mannheim.th.chess.domain.Game; import de.mannheim.th.chess.domain.Game;
public class ModeSelectionFrame extends JFrame { public class ModeSelectionFrame extends JFrame {
private static final Logger logger = LogManager.getLogger(App.class);
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final JPanel contentPane; private final JPanel contentPane;
private final ArrayList<Game> spiele = new ArrayList<>(); private final ArrayList<Game> spiele = new ArrayList<>();
private String fen;
public ModeSelectionFrame() { public ModeSelectionFrame() {
// Frame-Eigenschaften // Frame-Eigenschaften
@ -76,6 +89,19 @@ public class ModeSelectionFrame extends JFrame {
jb2.setAlignmentX(Component.CENTER_ALIGNMENT); jb2.setAlignmentX(Component.CENTER_ALIGNMENT);
jb2.setMaximumSize(new Dimension(30, 30)); jb2.setMaximumSize(new Dimension(30, 30));
contentPane.add(jb2); contentPane.add(jb2);
contentPane.add(Box.createVerticalStrut(15));
JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden");
btnNewButton_1.setBackground(Color.LIGHT_GRAY);
btnNewButton_1.setForeground(Color.BLACK);
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
btnNewButton_1.addActionListener(new ButtonFileLoaderListener(this));
contentPane.add(btnNewButton_1);
contentPane.add(Box.createVerticalStrut(25)); contentPane.add(Box.createVerticalStrut(25));
// Spiel starten Button // Spiel starten Button
@ -94,7 +120,7 @@ public class ModeSelectionFrame extends JFrame {
boolean rotieren = jb1.isSelected(); boolean rotieren = jb1.isSelected();
boolean zuruecknahme = jb2.isSelected(); boolean zuruecknahme = jb2.isSelected();
Game game = new Game(modus, rotieren, zuruecknahme); Game game = new Game(modus, rotieren, zuruecknahme, fen);
spiele.add(game); spiele.add(game);
@ -104,4 +130,8 @@ public class ModeSelectionFrame extends JFrame {
setVisible(true); setVisible(true);
} }
public void setFen(String fen) {
this.fen = fen;
}
} }

View File

@ -9,6 +9,8 @@ import com.github.bhlangonijr.chesslib.move.Move;
import de.mannheim.th.chess.App; import de.mannheim.th.chess.App;
import de.mannheim.th.chess.domain.Game; import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.utl.Clock; import de.mannheim.th.chess.utl.Clock;
import de.mannheim.th.chess.controller.ButtonAufgebenListener;
import de.mannheim.th.chess.controller.ButtonFileSaverListener;
import de.mannheim.th.chess.controller.ButtonMovePieceListener; import de.mannheim.th.chess.controller.ButtonMovePieceListener;
import de.mannheim.th.chess.controller.ButtonSelectPieceListener; import de.mannheim.th.chess.controller.ButtonSelectPieceListener;
import de.mannheim.th.chess.controller.ButtonToNormalListener; import de.mannheim.th.chess.controller.ButtonToNormalListener;
@ -48,6 +50,7 @@ public class SpielFrame extends JFrame {
private HashMap<JButton, String> belegungen = new HashMap<>(); private HashMap<JButton, String> belegungen = new HashMap<>();
private JPanel panelLinks, panelRechts, contentPane; private JPanel panelLinks, panelRechts, contentPane;
private JButton undo, undo2; private JButton undo, undo2;
private JTextArea ausgabe;
private Game game; private Game game;
private Clock clock; private Clock clock;
@ -110,7 +113,6 @@ public class SpielFrame extends JFrame {
splitPane.setEnabled(false); splitPane.setEnabled(false);
contentPane.add(splitPane, BorderLayout.CENTER); contentPane.add(splitPane, BorderLayout.CENTER);
setVisible(true); setVisible(true);
} }
@ -235,7 +237,7 @@ public class SpielFrame extends JFrame {
JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal())); JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal()));
s.setEnabled(true); s.setEnabled(true);
s.setBackground(new Color(165, 42, 42)); s.setBackground(new Color(165, 42, 42));
s.addActionListener(new ButtonToNormalListener(this)); // cancel action s.addActionListener(new ButtonToNormalListener(this));
selectables = game.getLegalMoveableSquares(selectedSquare); selectables = game.getLegalMoveableSquares(selectedSquare);
@ -341,12 +343,7 @@ public class SpielFrame extends JFrame {
aufgebenUndo.add(aufgeben); aufgebenUndo.add(aufgeben);
// Button-Listener // Button-Listener
aufgeben.addActionListener(new ActionListener() { aufgeben.addActionListener(new ButtonAufgebenListener());
@Override
public void actionPerformed(ActionEvent e) {
}
});
aufgebenUndo.add(Box.createHorizontalStrut(10)); aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -358,12 +355,7 @@ public class SpielFrame extends JFrame {
aufgebenUndo.add(safe); aufgebenUndo.add(safe);
// Button-Listener // Button-Listener
safe.addActionListener(new ActionListener() { safe.addActionListener(new ButtonFileSaverListener(this, this.game));
@Override
public void actionPerformed(ActionEvent e) {
}
});
playerTwo.add(aufgebenUndo); playerTwo.add(aufgebenUndo);
@ -378,14 +370,31 @@ public class SpielFrame extends JFrame {
statistik.setBackground(new Color(90, 90, 90)); statistik.setBackground(new Color(90, 90, 90));
statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS)); statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS));
JTextArea ausgabe = new JTextArea(); ausgabe = new JTextArea();
ausgabe.setEditable(false); ausgabe.setEditable(false);
ausgabe.setBackground(new Color(75, 75, 75)); ausgabe.setBackground(new Color(75, 75, 75));
ausgabe.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); ausgabe.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
ausgabe.setFont(new Font("Calibri", Font.BOLD, 26));
ausgabe.setForeground(Color.BLACK);
ausgabe.setText("\n Bisherige Züge:\n");
statistik.add(ausgabe); statistik.add(ausgabe);
return statistik; return statistik;
} }
public void appendText(String text) {
ausgabe.append(" "+ text + "\n");
}
public void deleteLastAusgabe() {
String[] ausgabe = this.ausgabe.getText().split("\n");
String textNeu = "";
for(int i=0;i<ausgabe.length-1;i++) {
textNeu += ausgabe[i]+"\n";
}
this.ausgabe.setText("");
appendText(textNeu);
}
private JPanel getUiPlayerOne() { private JPanel getUiPlayerOne() {
@ -423,12 +432,7 @@ public class SpielFrame extends JFrame {
aufgebenUndo.add(aufgeben); aufgebenUndo.add(aufgeben);
// Button-Listener // Button-Listener
aufgeben.addActionListener(new ActionListener() { aufgeben.addActionListener(new ButtonAufgebenListener());
@Override
public void actionPerformed(ActionEvent e) {
}
});
aufgebenUndo.add(Box.createHorizontalStrut(10)); aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -440,12 +444,7 @@ public class SpielFrame extends JFrame {
aufgebenUndo.add(safe); aufgebenUndo.add(safe);
// Button-Listener // Button-Listener
safe.addActionListener(new ActionListener() { safe.addActionListener(new ButtonFileSaverListener(this, this.game));
@Override
public void actionPerformed(ActionEvent e) {
}
});
playerOne.add(aufgebenUndo); playerOne.add(aufgebenUndo);
@ -466,25 +465,6 @@ public class SpielFrame extends JFrame {
return playerOne; return playerOne;
} }
public void undoMove() {
switch(this.undoMove) {
case white:
break;
case black:
break;
case nobody:
break;
default:
break;
}
}
public void setBoardMode(BoardMode bm) { public void setBoardMode(BoardMode bm) {
this.mode = bm; this.mode = bm;
} }