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 0375307..a375d29 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.stream.Collectors; import com.github.bhlangonijr.chesslib.Board; +import com.github.bhlangonijr.chesslib.Side; import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.MoveList; @@ -18,8 +19,7 @@ import de.mannheim.th.chess.utl.Clock; public class Game { private Board board; - private Clock clockPlayer1; - private Clock clockPlayer2; + private Clock clock; private MoveList movelist; @@ -31,8 +31,8 @@ public class Game { this.movelist = new MoveList(); - // this.clockPlayer1 = new Clock(); - // this.clockPlayer2 = new Clock(); + clock = new Clock("blitz"); + clock.start(); } @@ -77,6 +77,7 @@ public class Game { public void playMove(Move move) { this.board.doMove(move); this.movelist.add(move); + clock.pressClock(); } /** @@ -89,6 +90,22 @@ public class Game { 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; } /** @@ -104,6 +121,10 @@ public class Game { .collect(Collectors.toList()); } + + public void stopClock() { + clock.endGame(); + } /** * Retrieves a list of all legal moveable squares from the current board state. 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 4f41945..a8bd089 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -4,6 +4,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.github.bhlangonijr.chesslib.Square; +import com.github.bhlangonijr.chesslib.game.Player; import com.github.bhlangonijr.chesslib.move.Move; import de.mannheim.th.chess.App; @@ -16,8 +17,12 @@ import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; + import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; @@ -49,7 +54,7 @@ public class SpielFrame extends JFrame { private Square selectedSquare; enum BoardMode { - normal, pieceSelected, + normal, pieceSelected, finished } /** @@ -222,6 +227,8 @@ public class SpielFrame extends JFrame { } } } + + /* * Switches the button actions depending on the boardmode @@ -290,6 +297,15 @@ public class SpielFrame extends JFrame { @Override public void actionPerformed(ActionEvent e) { game.playMove(move); + if (game.isDraw()) { + game.stopClock(); + mode = BoardMode.finished; + showDraw(); + } else if (game.isMate()) { + game.stopClock(); + mode = BoardMode.finished; + showWin(game.getActivePlayer()); + } mode = BoardMode.normal; setCursor(null); erstelleBrett(); @@ -298,6 +314,10 @@ public class SpielFrame extends JFrame { } break; + + case finished: + clearButtons(); + break; default: break; @@ -307,5 +327,32 @@ public class SpielFrame extends JFrame { panelLinks.add(b); } } + + private void showDraw() { + JFrame frame = new JFrame("Result"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(300, 150); + frame.setLayout(null); + + JLabel jl = new JLabel("1/2 - 1/2"); + jl.setBounds(50, 30, 200, 25); + jl.setFont(new Font("Tahoma", Font.BOLD, 20)); + frame.add(jl); + frame.setVisible(true); + + } + + private void showWin(int player) { + JFrame frame = new JFrame("Result"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(300, 150); + frame.setLayout(null); + + JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2)); + jl.setBounds(50, 30, 200, 25); + jl.setFont(new Font("Tahoma", Font.BOLD, 20)); + frame.add(jl); + frame.setVisible(true); + } }