Ausgabe von gezogenen Zügen verschönert.

devUi
Your Name 2025-06-23 18:30:34 +02:00
parent 6c748bcaa5
commit d4e56ea2b6
3 changed files with 52 additions and 47 deletions

View File

@ -1,6 +1,5 @@
package de.mannheim.th.chess.controller; package de.mannheim.th.chess.controller;
import java.awt.Color;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -39,16 +38,8 @@ public class ButtonMovePieceListener implements ActionListener {
this.sf.erstelleBrett(); this.sf.erstelleBrett();
if (game.getLastMove() != null) { if (game.getLastMove() != null) {
char[] z = game.getLastMove().toString().toCharArray();
String moveString = "";
if(game.getActivePlayer() == 1) {
moveString = " " + String.valueOf(z[0]) + String.valueOf(z[1]) + " -> " + String.valueOf(z[2]) + String.valueOf(z[3]);
}else if(game.getActivePlayer() == 2){
moveString = String.valueOf(z[0]) + String.valueOf(z[1]) + " -> " + String.valueOf(z[2]) + String.valueOf(z[3]+" ");
}
sf.aktualisiereAusgabe();
sf.appendText(moveString);
} }
} }
} }

View File

@ -202,4 +202,8 @@ public class Game {
logger.info(this.movelist.getLast().toString()); logger.info(this.movelist.getLast().toString());
return this.movelist.getLast(); return this.movelist.getLast();
} }
public MoveList getMoveList() {
return this.movelist;
}
} }

View File

@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.Move;
import com.github.bhlangonijr.chesslib.move.MoveList;
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;
@ -56,16 +57,11 @@ public class SpielFrame extends JFrame {
private Clock clock; private Clock clock;
private BoardMode mode; private BoardMode mode;
private Zuruecknahme undoMove;
private Square selectedSquare; private Square selectedSquare;
public enum BoardMode { public enum BoardMode {
normal, pieceSelected, finished normal, pieceSelected, finished
} }
public enum Zuruecknahme {
white, black, nobody
}
/** /**
* Create the frame. * Create the frame.
@ -114,7 +110,7 @@ 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);
} }
@ -176,7 +172,7 @@ public class SpielFrame extends JFrame {
* with new blank ones. * with new blank ones.
*/ */
private void clearButtons() { private void clearButtons() {
buttons.clear(); buttons.clear();
panelLinks.removeAll(); panelLinks.removeAll();
@ -221,7 +217,7 @@ public class SpielFrame extends JFrame {
switch (this.mode) { switch (this.mode) {
case BoardMode.normal: case BoardMode.normal:
selectables = game.getAllLegalMoveableSquares(); selectables = game.getAllLegalMoveableSquares();
for (Square square : selectables) { for (Square square : selectables) {
@ -238,7 +234,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)); s.addActionListener(new ButtonToNormalListener(this));
selectables = game.getLegalMoveableSquares(selectedSquare); selectables = game.getLegalMoveableSquares(selectedSquare);
@ -331,8 +327,7 @@ public class SpielFrame extends JFrame {
// Button-Listener // Button-Listener
undo.addActionListener(new ButtonUndoMoveListener(this, this.game)); undo.addActionListener(new ButtonUndoMoveListener(this, this.game));
} }
aufgebenUndo.add(Box.createHorizontalStrut(10)); aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -375,30 +370,49 @@ public class SpielFrame extends JFrame {
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.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
ausgabe.setForeground(Color.BLACK); ausgabe.setForeground(Color.BLACK);
ausgabe.setText("\n Bisherige Züge:\n"); ausgabe.setText("\n Bisherige Züge:\n");
JScrollPane scrollPane = new JScrollPane(ausgabe); JScrollPane scrollPane = new JScrollPane(ausgabe);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
statistik.add(scrollPane); statistik.add(scrollPane);
return statistik; return statistik;
} }
public void appendText(String text) { public void aktualisiereAusgabe() {
ausgabe.append(" "+ text + "\n"); StringBuilder sb = new StringBuilder();
sb.append("\n Bisherige Züge:\n");
sb.append(" +------------------+------------------+\n");
sb.append(" | Player 1: | Player 2: |\n");
sb.append(" +------------------+------------------+\n");
MoveList l = game.getMoveList();
for (int i = 0; i < l.size(); i += 2) {
String p1 = l.get(i).toString();
String p2 = (i + 1 < l.size()) ? l.get(i + 1).toString() : "";
sb.append(String.format(" | %-17s| %-17s|\n", p1, p2));
sb.append(" +------------------+------------------+\n");
}
ausgabe.setText(sb.toString());
} }
public void deleteLastAusgabe() { public void deleteLastAusgabe() {
String[] ausgabe = this.ausgabe.getText().split("\n"); String[] zeilen = ausgabe.getText().split("\n");
String textNeu = "";
for(int i=0;i<ausgabe.length-1;i++) { //es müssen immer mind 5 Zeilen existieren, dass also 1 Zug löschbar ist
textNeu += ausgabe[i]+"\n"; if (zeilen.length <= 5) return;
}
this.ausgabe.setText(""); StringBuilder sb = new StringBuilder();
appendText(textNeu); for (int i = 0; i < zeilen.length - 2; i++) {
sb.append(zeilen[i]).append("\n");
}
ausgabe.setText(sb.toString());
} }
private JPanel getUiPlayerOne() { private JPanel getUiPlayerOne() {
@ -424,7 +438,7 @@ public class SpielFrame extends JFrame {
// Button-Listener // Button-Listener
undo2.addActionListener(new ButtonUndoMoveListener(this, this.game)); undo2.addActionListener(new ButtonUndoMoveListener(this, this.game));
} }
aufgebenUndo.add(Box.createHorizontalStrut(10)); aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -469,7 +483,7 @@ public class SpielFrame extends JFrame {
return playerOne; return playerOne;
} }
public void setBoardMode(BoardMode bm) { public void setBoardMode(BoardMode bm) {
this.mode = bm; this.mode = bm;
} }
@ -481,23 +495,19 @@ public class SpielFrame extends JFrame {
public HashMap<JButton, String> getBelegung() { public HashMap<JButton, String> getBelegung() {
return this.belegungen; return this.belegungen;
} }
public void setZuruecknahme(Zuruecknahme z) {
this.undoMove = z;
}
public JButton getUndo() { public JButton getUndo() {
return undo; return undo;
} }
public JButton getUndo2() { public JButton getUndo2() {
return undo2; return undo2;
} }
public BoardMode getMode() { public BoardMode getMode() {
return mode; return mode;
} }
public Clock getClock() { public Clock getClock() {
return clock; return clock;
} }