parent
fb40f3019b
commit
0a46fc763f
|
|
@ -1,5 +1,6 @@
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -23,6 +24,9 @@ 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();
|
||||||
this.sf.setBoardMode(BoardMode.finished);
|
this.sf.setBoardMode(BoardMode.finished);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package de.mannheim.th.chess.controller;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
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 de.mannheim.th.chess.ui.SpielFrame;
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||||
|
|
||||||
|
public class ButtonUndoMoveListener implements ActionListener {
|
||||||
|
|
||||||
|
private static final Logger logger = LogManager.getLogger(App.class);
|
||||||
|
|
||||||
|
private SpielFrame sf;
|
||||||
|
private Game game;
|
||||||
|
|
||||||
|
public ButtonUndoMoveListener(SpielFrame sf, Game game) {
|
||||||
|
this.sf = sf;
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
Object source = e.getSource();
|
||||||
|
|
||||||
|
if (sf.getMode() != BoardMode.normal || !game.movesNotNull()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source == sf.getUndo()) { // Spieler 2 drückt seinen Button
|
||||||
|
if (sf.getUndo().getText().equals("Zug zurücknehmen") && game.getActivePlayer() == 1) {
|
||||||
|
sf.getUndo2().setText("Zurücknahme genehmigen?");
|
||||||
|
sf.getUndo2().setEnabled(true);
|
||||||
|
logger.info("Spieler 2 hat zurücknahme angefordert.");
|
||||||
|
} else if (sf.getUndo().getText().equals("Zurücknahme genehmigen?")) {
|
||||||
|
logger.info("Zug zurücknehmen wurde von Spieler 2 genehmigt.");
|
||||||
|
sf.getUndo().setText("Zug zurücknehmen");
|
||||||
|
sf.getUndo2().setText("Zug zurücknehmen");
|
||||||
|
sf.getUndo2().setEnabled(false);
|
||||||
|
game.undo();
|
||||||
|
sf.getClock().switchClock();
|
||||||
|
sf.erstelleBrett();
|
||||||
|
}
|
||||||
|
} else if (source == sf.getUndo2()) { // Spieler 1 drückt seinen Button
|
||||||
|
if (sf.getUndo2().getText().equals("Zug zurücknehmen") && game.getActivePlayer() == 2) {
|
||||||
|
sf.getUndo().setText("Zurücknahme genehmigen?");
|
||||||
|
sf.getUndo().setEnabled(true);
|
||||||
|
logger.info("Spieler 1 hat zurücknahme angefordert.");
|
||||||
|
} else if (sf.getUndo2().getText().equals("Zurücknahme genehmigen?")) {
|
||||||
|
logger.info("Zug zurücknehmen wurde von Spieler 1 genehmigt.");
|
||||||
|
sf.getUndo2().setText("Zug zurücknehmen");
|
||||||
|
sf.getUndo().setText("Zug zurücknehmen");
|
||||||
|
sf.getUndo().setEnabled(false);
|
||||||
|
game.undo();
|
||||||
|
sf.getClock().switchClock();
|
||||||
|
sf.erstelleBrett();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -106,6 +106,11 @@ public class Game {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void undo() {
|
||||||
|
this.board.undoMove();
|
||||||
|
this.movelist.removeLast();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isMate() {
|
public boolean isMate() {
|
||||||
return board.isMated();
|
return board.isMated();
|
||||||
}
|
}
|
||||||
|
|
@ -174,4 +179,11 @@ public class Game {
|
||||||
public boolean isZuruecknahme() {
|
public boolean isZuruecknahme() {
|
||||||
return zuruecknahme;
|
return zuruecknahme;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean movesNotNull() {
|
||||||
|
if(movelist.getLast() != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import de.mannheim.th.chess.utl.Clock;
|
||||||
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;
|
||||||
|
import de.mannheim.th.chess.controller.ButtonUndoMoveListener;
|
||||||
|
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
|
|
||||||
|
|
@ -46,16 +47,22 @@ public class SpielFrame extends JFrame {
|
||||||
private ArrayList<JButton> buttons = new ArrayList<>();
|
private ArrayList<JButton> buttons = new ArrayList<>();
|
||||||
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 Game game;
|
private Game game;
|
||||||
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.
|
||||||
*/
|
*/
|
||||||
|
|
@ -92,7 +99,7 @@ public class SpielFrame extends JFrame {
|
||||||
// Panel für Statistikanzeigen
|
// Panel für Statistikanzeigen
|
||||||
panelRechts.add(getUiStatistik());
|
panelRechts.add(getUiStatistik());
|
||||||
|
|
||||||
//Panel für alle Eingaben von Player 1
|
// Panel für alle Eingaben von Player 1
|
||||||
panelRechts.add(getUiPlayerOne());
|
panelRechts.add(getUiPlayerOne());
|
||||||
|
|
||||||
// JSplitPane horizontal (linke und rechte Hälfte)
|
// JSplitPane horizontal (linke und rechte Hälfte)
|
||||||
|
|
@ -104,21 +111,10 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
contentPane.add(splitPane, BorderLayout.CENTER);
|
contentPane.add(splitPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBoardMode(BoardMode bm) {
|
|
||||||
this.mode = bm;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSelectedSquare(Square sq) {
|
|
||||||
this.selectedSquare = sq;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashMap<JButton, String> getBelegung() {
|
|
||||||
return this.belegungen;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erstellt alle Buttons und fügt sie dem Frame hinzu.
|
* Erstellt alle Buttons und fügt sie dem Frame hinzu.
|
||||||
*/
|
*/
|
||||||
|
|
@ -177,6 +173,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,6 +218,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) {
|
||||||
|
|
@ -321,7 +319,7 @@ public class SpielFrame extends JFrame {
|
||||||
aufgebenUndo.setLayout(new BoxLayout(aufgebenUndo, BoxLayout.X_AXIS));
|
aufgebenUndo.setLayout(new BoxLayout(aufgebenUndo, BoxLayout.X_AXIS));
|
||||||
|
|
||||||
if (game.isZuruecknahme()) {
|
if (game.isZuruecknahme()) {
|
||||||
JButton undo = new JButton("Zug zurücknehmen");
|
undo = new JButton("Zug zurücknehmen");
|
||||||
undo.setBackground(Color.LIGHT_GRAY);
|
undo.setBackground(Color.LIGHT_GRAY);
|
||||||
undo.setForeground(Color.BLACK);
|
undo.setForeground(Color.BLACK);
|
||||||
undo.setFont(new Font("Tahoma", Font.BOLD, 16));
|
undo.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||||
|
|
@ -329,13 +327,9 @@ public class SpielFrame extends JFrame {
|
||||||
aufgebenUndo.add(undo);
|
aufgebenUndo.add(undo);
|
||||||
|
|
||||||
// Button-Listener
|
// Button-Listener
|
||||||
undo.addActionListener(new ActionListener() {
|
undo.addActionListener(new ButtonUndoMoveListener(this, this.game));
|
||||||
@Override
|
}
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
||||||
|
|
||||||
|
|
@ -386,7 +380,7 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
JTextArea ausgabe = new JTextArea();
|
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));
|
||||||
|
|
||||||
statistik.add(ausgabe);
|
statistik.add(ausgabe);
|
||||||
|
|
@ -407,21 +401,17 @@ public class SpielFrame extends JFrame {
|
||||||
aufgebenUndo.setLayout(new BoxLayout(aufgebenUndo, BoxLayout.X_AXIS));
|
aufgebenUndo.setLayout(new BoxLayout(aufgebenUndo, BoxLayout.X_AXIS));
|
||||||
|
|
||||||
if (game.isZuruecknahme()) {
|
if (game.isZuruecknahme()) {
|
||||||
JButton undo = new JButton("Zug zurücknehmen");
|
undo2 = new JButton("Zug zurücknehmen");
|
||||||
undo.setBackground(Color.LIGHT_GRAY);
|
undo2.setBackground(Color.LIGHT_GRAY);
|
||||||
undo.setForeground(Color.BLACK);
|
undo2.setForeground(Color.BLACK);
|
||||||
undo.setFont(new Font("Tahoma", Font.BOLD, 16));
|
undo2.setFont(new Font("Tahoma", Font.BOLD, 16));
|
||||||
undo.setAlignmentX(Component.CENTER_ALIGNMENT);
|
undo2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
aufgebenUndo.add(undo);
|
aufgebenUndo.add(undo2);
|
||||||
|
|
||||||
// Button-Listener
|
// Button-Listener
|
||||||
undo.addActionListener(new ActionListener() {
|
undo2.addActionListener(new ButtonUndoMoveListener(this, this.game));
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
aufgebenUndo.add(Box.createHorizontalStrut(10));
|
||||||
|
|
||||||
|
|
@ -459,7 +449,7 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
playerOne.add(aufgebenUndo);
|
playerOne.add(aufgebenUndo);
|
||||||
|
|
||||||
playerOne.add(Box.createVerticalStrut(10));
|
playerOne.add(Box.createVerticalStrut(15));
|
||||||
|
|
||||||
JLabel clock1 = clock.getClock1();
|
JLabel clock1 = clock.getClock1();
|
||||||
playerOne.add(clock1);
|
playerOne.add(clock1);
|
||||||
|
|
@ -476,4 +466,55 @@ 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) {
|
||||||
|
this.mode = bm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectedSquare(Square sq) {
|
||||||
|
this.selectedSquare = sq;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap<JButton, String> getBelegung() {
|
||||||
|
return this.belegungen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZuruecknahme(Zuruecknahme z) {
|
||||||
|
this.undoMove = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getUndo() {
|
||||||
|
return undo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JButton getUndo2() {
|
||||||
|
return undo2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BoardMode getMode() {
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Clock getClock() {
|
||||||
|
return clock;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,13 +59,13 @@ public class Clock extends Thread implements Runnable {
|
||||||
clock1 = new JLabel("" + minutes + ":00 ");
|
clock1 = new JLabel("" + minutes + ":00 ");
|
||||||
clock1.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
clock1.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||||
clock1.setForeground(Color.BLACK);
|
clock1.setForeground(Color.BLACK);
|
||||||
clock1.setFont(new Font("Calibri", Font.BOLD, 35));
|
clock1.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||||
clock1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
clock1.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
|
|
||||||
clock2 = new JLabel("" + minutes + ":00 ");
|
clock2 = new JLabel("" + minutes + ":00 ");
|
||||||
clock2.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
clock2.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
|
||||||
clock2.setForeground(Color.BLACK);
|
clock2.setForeground(Color.BLACK);
|
||||||
clock2.setFont(new Font("Calibri", Font.BOLD, 35));
|
clock2.setFont(new Font("Calibri", Font.BOLD, 40));
|
||||||
clock2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
clock2.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||||
// player1Panel.add(clock1);
|
// player1Panel.add(clock1);
|
||||||
// player2Panel.add(clock2);
|
// player2Panel.add(clock2);
|
||||||
|
|
@ -142,6 +142,10 @@ public class Clock extends Thread implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void switchClock() {
|
||||||
|
whiteToMove = !whiteToMove;
|
||||||
|
}
|
||||||
|
|
||||||
public JLabel getClock1() {
|
public JLabel getClock1() {
|
||||||
|
|
||||||
return clock1;
|
return clock1;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue