Browse Source

added the possibility to save history in sudoku, now we can implement back button to go back and forward in game history

main
fdai6499 2 years ago
parent
commit
f931a52dc0
  1. 33
      src/main/java/src/MainFrame.java
  2. 14
      src/test/java/MainFrameTest.java

33
src/main/java/src/MainFrame.java

@ -12,9 +12,13 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.beans.PropertyChangeListener;
import java.util.LinkedList;
public class MainFrame extends JFrame { public class MainFrame extends JFrame {
LinkedList<Model> history;
private GameField gameField; private GameField gameField;
private JSONArray currentGameDifficulty; private JSONArray currentGameDifficulty;
@ -24,6 +28,8 @@ public class MainFrame extends JFrame {
public void run() { public void run() {
history = new LinkedList<>();
this.setLayout(null); this.setLayout(null);
this.setVisible(true); this.setVisible(true);
this.setBounds(300, 200, 500, 600); this.setBounds(300, 200, 500, 600);
@ -115,9 +121,15 @@ public class MainFrame extends JFrame {
JButton newButton = new JButton(); JButton newButton = new JButton();
newButton.setVisible(true); newButton.setVisible(true);
newButton.setBounds(395, 5, 100, 25); newButton.setBounds(395, 5, 100, 25);
newButton.setText("New");
newButton.setText("Vorwärts");
rootPanel.add(newButton); 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(); JButton retryButton = new JButton();
retryButton.setVisible(true); retryButton.setVisible(true);
retryButton.setText("Retry"); retryButton.setText("Retry");
@ -146,8 +158,8 @@ public class MainFrame extends JFrame {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
gameField.getActivePanel().setValue(Integer.valueOf(number));
gameField.getActivePanel().setValue(Integer.valueOf(number));
saveCurrentGameFieldStateToHistory();
} }
@Override @Override
@ -171,9 +183,24 @@ public class MainFrame extends JFrame {
numLabel1.setVisible(true); numLabel1.setVisible(true);
jPanel.add(numLabel1); jPanel.add(numLabel1);
return jPanel; return jPanel;
} }
public void saveCurrentGameFieldStateToHistory() {
Model model = new Model(gameField);
history.add(model);
}
public LinkedList<Model> getHistory() {
return history;
}
public GameField getGameField() {
return gameField;
}
public JPanel setupJPanel() { public JPanel setupJPanel() {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setBounds(100, 450, 300, 30); panel.setBounds(100, 450, 300, 30);

14
src/test/java/MainFrameTest.java

@ -1,4 +1,7 @@
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import src.GameField;
import src.Main;
import src.MainFrame;
import src.Model; import src.Model;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -10,4 +13,15 @@ public class MainFrameTest {
Model.initJsonFile(); 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);
}
} }
Loading…
Cancel
Save