From f931a52dc05b64f833603b482b33fa6604879012 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 9 Feb 2023 20:37:58 +0100 Subject: [PATCH] added the possibility to save history in sudoku, now we can implement back button to go back and forward in game history --- src/main/java/src/MainFrame.java | 33 +++++++++++++++++++++++++++++--- src/test/java/MainFrameTest.java | 14 ++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/src/MainFrame.java b/src/main/java/src/MainFrame.java index 98378ee..2e3a77d 100644 --- a/src/main/java/src/MainFrame.java +++ b/src/main/java/src/MainFrame.java @@ -12,9 +12,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.beans.PropertyChangeListener; +import java.util.LinkedList; public class MainFrame extends JFrame { + LinkedList history; + private GameField gameField; private JSONArray currentGameDifficulty; @@ -24,6 +28,8 @@ public class MainFrame extends JFrame { public void run() { + history = new LinkedList<>(); + this.setLayout(null); this.setVisible(true); this.setBounds(300, 200, 500, 600); @@ -115,9 +121,15 @@ public class MainFrame extends JFrame { JButton newButton = new JButton(); newButton.setVisible(true); newButton.setBounds(395, 5, 100, 25); - newButton.setText("New"); + newButton.setText("Vorwärts"); rootPanel.add(newButton); + JButton backButton = new JButton(); + backButton.setVisible(true); + backButton.setBounds(10, 5, 100, 25); + backButton.setText("Rückwärts"); + rootPanel.add(backButton); + JButton retryButton = new JButton(); retryButton.setVisible(true); retryButton.setText("Retry"); @@ -146,8 +158,8 @@ public class MainFrame extends JFrame { @Override public void mousePressed(MouseEvent e) { - gameField.getActivePanel().setValue(Integer.valueOf(number)); - + gameField.getActivePanel().setValue(Integer.valueOf(number)); + saveCurrentGameFieldStateToHistory(); } @Override @@ -171,9 +183,24 @@ public class MainFrame extends JFrame { numLabel1.setVisible(true); jPanel.add(numLabel1); + + return jPanel; } + public void saveCurrentGameFieldStateToHistory() { + Model model = new Model(gameField); + history.add(model); + } + + public LinkedList getHistory() { + return history; + } + + public GameField getGameField() { + return gameField; + } + public JPanel setupJPanel() { JPanel panel = new JPanel(); panel.setBounds(100, 450, 300, 30); diff --git a/src/test/java/MainFrameTest.java b/src/test/java/MainFrameTest.java index 85059c6..220f606 100644 --- a/src/test/java/MainFrameTest.java +++ b/src/test/java/MainFrameTest.java @@ -1,4 +1,7 @@ import org.junit.jupiter.api.*; +import src.GameField; +import src.Main; +import src.MainFrame; import src.Model; import static org.junit.jupiter.api.Assertions.*; @@ -10,4 +13,15 @@ public class MainFrameTest { Model.initJsonFile(); } + @Test + public void test_history() { + MainFrame mainFrame = new MainFrame(); + mainFrame.run(); + GameField gameField = mainFrame.getGameField(); + gameField.setValue(5, 5, 5); + mainFrame.saveCurrentGameFieldStateToHistory(); + int field = mainFrame.getHistory().getFirst().getField(5, 5); + + assertEquals(5, field); + } } \ No newline at end of file