Statistik ausgabe finished. Spiel laden und speichern funktioniert.
parent
0a46fc763f
commit
cf4e465b10
2
quellen
2
quellen
|
|
@ -1,2 +1,2 @@
|
|||
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?".
|
||||
|
|
@ -20,7 +20,6 @@ public class App {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
logger.info("Hello World.");
|
||||
userinterface = new MainFrame();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,8 +24,6 @@ public class ButtonMovePieceListener implements ActionListener {
|
|||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
this.game.playMove(this.mv);
|
||||
sf.getUndo().setText("Zug zurücknehmen");
|
||||
sf.getUndo2().setText("Zug zurücknehmen");
|
||||
|
||||
if (this.game.isDraw()) {
|
||||
this.game.stopClock();
|
||||
|
|
@ -39,5 +37,11 @@ public class ButtonMovePieceListener implements ActionListener {
|
|||
this.sf.setBoardMode(BoardMode.normal);
|
||||
this.sf.setCursor(null);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package de.mannheim.th.chess.controller;
|
|||
import java.awt.event.ActionEvent;
|
||||
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.BoardMode;
|
||||
|
||||
|
|
@ -19,6 +20,7 @@ public class ButtonToNormalListener implements ActionListener {
|
|||
this.sf.setSelectedSquare(null);
|
||||
this.sf.setCursor(null);
|
||||
this.sf.erstelleBrett();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ public class ButtonUndoMoveListener implements ActionListener {
|
|||
sf.getUndo2().setEnabled(false);
|
||||
game.undo();
|
||||
sf.getClock().switchClock();
|
||||
sf.deleteLastAusgabe();
|
||||
sf.erstelleBrett();
|
||||
}
|
||||
} else if (source == sf.getUndo2()) { // Spieler 1 drückt seinen Button
|
||||
|
|
@ -59,6 +60,7 @@ public class ButtonUndoMoveListener implements ActionListener {
|
|||
sf.getUndo().setEnabled(false);
|
||||
game.undo();
|
||||
sf.getClock().switchClock();
|
||||
sf.deleteLastAusgabe();
|
||||
sf.erstelleBrett();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ package de.mannheim.th.chess.domain;
|
|||
import java.util.List;
|
||||
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.Side;
|
||||
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.pgn.PgnHolder;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.utl.Clock;
|
||||
|
||||
|
|
@ -19,6 +23,8 @@ import de.mannheim.th.chess.utl.Clock;
|
|||
*/
|
||||
public class Game {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private Board board;
|
||||
private Clock clock;
|
||||
private SpielFrame sp;
|
||||
|
|
@ -30,22 +36,23 @@ public class Game {
|
|||
/**
|
||||
* 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.rotieren = rotieren;
|
||||
this.zuruecknahme = zuruecknahme;
|
||||
|
||||
|
||||
this.board = new Board();
|
||||
|
||||
if(fen == null) fen = board.getFen();
|
||||
|
||||
this.board.loadFromFen(fen);
|
||||
|
||||
this.movelist = new MoveList();
|
||||
|
||||
clock = new Clock(modus);
|
||||
|
||||
sp = new SpielFrame(this);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -186,4 +193,13 @@ public class Game {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getFen() {
|
||||
return this.board.getFen();
|
||||
}
|
||||
|
||||
public Move getLastMove() {
|
||||
logger.info(this.movelist.getLast().toString());
|
||||
return this.movelist.getLast();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import de.mannheim.th.chess.domain.Game;
|
|||
*/
|
||||
public class GameWindow{
|
||||
|
||||
private Game gamelogic = new Game();
|
||||
//private Game gamelogic = new Game();
|
||||
|
||||
public GameWindow() {
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,23 @@ package de.mannheim.th.chess.ui;
|
|||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
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.JOptionPane;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
|
||||
import javax.swing.Box;
|
||||
|
|
@ -22,6 +33,8 @@ import java.awt.Color;
|
|||
|
||||
public class MainFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JPanel contentPane;
|
||||
|
||||
|
|
@ -83,35 +96,6 @@ public class MainFrame extends JFrame {
|
|||
|
||||
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");
|
||||
|
||||
btnNewButton_2.setBackground(Color.LIGHT_GRAY);
|
||||
|
|
|
|||
|
|
@ -3,17 +3,30 @@ package de.mannheim.th.chess.ui;
|
|||
import java.awt.*;
|
||||
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 java.util.ArrayList;
|
||||
import javax.swing.*;
|
||||
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;
|
||||
|
||||
public class ModeSelectionFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private final JPanel contentPane;
|
||||
private final ArrayList<Game> spiele = new ArrayList<>();
|
||||
private String fen;
|
||||
|
||||
public ModeSelectionFrame() {
|
||||
// Frame-Eigenschaften
|
||||
|
|
@ -76,6 +89,19 @@ public class ModeSelectionFrame extends JFrame {
|
|||
jb2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
jb2.setMaximumSize(new Dimension(30, 30));
|
||||
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));
|
||||
|
||||
// Spiel starten Button
|
||||
|
|
@ -94,7 +120,7 @@ public class ModeSelectionFrame extends JFrame {
|
|||
boolean rotieren = jb1.isSelected();
|
||||
boolean zuruecknahme = jb2.isSelected();
|
||||
|
||||
Game game = new Game(modus, rotieren, zuruecknahme);
|
||||
Game game = new Game(modus, rotieren, zuruecknahme, fen);
|
||||
|
||||
spiele.add(game);
|
||||
|
||||
|
|
@ -104,4 +130,8 @@ public class ModeSelectionFrame extends JFrame {
|
|||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void setFen(String fen) {
|
||||
this.fen = fen;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ 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.ButtonAufgebenListener;
|
||||
import de.mannheim.th.chess.controller.ButtonFileSaverListener;
|
||||
import de.mannheim.th.chess.controller.ButtonMovePieceListener;
|
||||
import de.mannheim.th.chess.controller.ButtonSelectPieceListener;
|
||||
import de.mannheim.th.chess.controller.ButtonToNormalListener;
|
||||
|
|
@ -48,6 +50,7 @@ public class SpielFrame extends JFrame {
|
|||
private HashMap<JButton, String> belegungen = new HashMap<>();
|
||||
private JPanel panelLinks, panelRechts, contentPane;
|
||||
private JButton undo, undo2;
|
||||
private JTextArea ausgabe;
|
||||
private Game game;
|
||||
private Clock clock;
|
||||
|
||||
|
|
@ -111,7 +114,6 @@ public class SpielFrame extends JFrame {
|
|||
|
||||
contentPane.add(splitPane, BorderLayout.CENTER);
|
||||
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
|
|
@ -235,7 +237,7 @@ public class SpielFrame extends JFrame {
|
|||
JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal()));
|
||||
s.setEnabled(true);
|
||||
s.setBackground(new Color(165, 42, 42));
|
||||
s.addActionListener(new ButtonToNormalListener(this)); // cancel action
|
||||
s.addActionListener(new ButtonToNormalListener(this));
|
||||
|
||||
selectables = game.getLegalMoveableSquares(selectedSquare);
|
||||
|
||||
|
|
@ -341,12 +343,7 @@ public class SpielFrame extends JFrame {
|
|||
aufgebenUndo.add(aufgeben);
|
||||
|
||||
// Button-Listener
|
||||
aufgeben.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
aufgeben.addActionListener(new ButtonAufgebenListener());
|
||||
|
||||
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
||||
|
||||
|
|
@ -358,12 +355,7 @@ public class SpielFrame extends JFrame {
|
|||
aufgebenUndo.add(safe);
|
||||
|
||||
// Button-Listener
|
||||
safe.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
safe.addActionListener(new ButtonFileSaverListener(this, this.game));
|
||||
|
||||
playerTwo.add(aufgebenUndo);
|
||||
|
||||
|
|
@ -378,15 +370,32 @@ public class SpielFrame extends JFrame {
|
|||
statistik.setBackground(new Color(90, 90, 90));
|
||||
statistik.setLayout(new BoxLayout(statistik, BoxLayout.Y_AXIS));
|
||||
|
||||
JTextArea ausgabe = new JTextArea();
|
||||
ausgabe = new JTextArea();
|
||||
ausgabe.setEditable(false);
|
||||
ausgabe.setBackground(new Color(75, 75, 75));
|
||||
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);
|
||||
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() {
|
||||
|
||||
JPanel playerOne = new JPanel();
|
||||
|
|
@ -423,12 +432,7 @@ public class SpielFrame extends JFrame {
|
|||
aufgebenUndo.add(aufgeben);
|
||||
|
||||
// Button-Listener
|
||||
aufgeben.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
aufgeben.addActionListener(new ButtonAufgebenListener());
|
||||
|
||||
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
||||
|
||||
|
|
@ -440,12 +444,7 @@ public class SpielFrame extends JFrame {
|
|||
aufgebenUndo.add(safe);
|
||||
|
||||
// Button-Listener
|
||||
safe.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
safe.addActionListener(new ButtonFileSaverListener(this, this.game));
|
||||
|
||||
playerOne.add(aufgebenUndo);
|
||||
|
||||
|
|
@ -466,25 +465,6 @@ public class SpielFrame extends JFrame {
|
|||
return playerOne;
|
||||
}
|
||||
|
||||
public void undoMove() {
|
||||
|
||||
switch(this.undoMove) {
|
||||
|
||||
case white:
|
||||
|
||||
break;
|
||||
|
||||
case black:
|
||||
break;
|
||||
|
||||
case nobody:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setBoardMode(BoardMode bm) {
|
||||
this.mode = bm;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue