From f931a52dc05b64f833603b482b33fa6604879012 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 9 Feb 2023 20:37:58 +0100 Subject: [PATCH 1/9] 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 From 05b4ceec56b94760f6b5f965d66e9aa08bd25f7d Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 9 Feb 2023 20:44:38 +0100 Subject: [PATCH 2/9] refactor: test_history() change --- src/test/java/MainFrameTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/MainFrameTest.java b/src/test/java/MainFrameTest.java index 220f606..bbfee1d 100644 --- a/src/test/java/MainFrameTest.java +++ b/src/test/java/MainFrameTest.java @@ -20,7 +20,7 @@ public class MainFrameTest { GameField gameField = mainFrame.getGameField(); gameField.setValue(5, 5, 5); mainFrame.saveCurrentGameFieldStateToHistory(); - int field = mainFrame.getHistory().getFirst().getField(5, 5); + int field = mainFrame.getHistory().getLast().getField(5, 5); assertEquals(5, field); } From 2c458a625c568df88a2376da32e9601a1369894a Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 9 Feb 2023 20:50:25 +0100 Subject: [PATCH 3/9] added gui controll or back button --- src/main/java/src/MainFrame.java | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/src/MainFrame.java b/src/main/java/src/MainFrame.java index 2e3a77d..7551c8c 100644 --- a/src/main/java/src/MainFrame.java +++ b/src/main/java/src/MainFrame.java @@ -18,6 +18,7 @@ import java.util.LinkedList; public class MainFrame extends JFrame { LinkedList history; + int historyPosition; private GameField gameField; @@ -30,6 +31,7 @@ public class MainFrame extends JFrame { history = new LinkedList<>(); + this.setLayout(null); this.setVisible(true); this.setBounds(300, 200, 500, 600); @@ -52,6 +54,8 @@ public class MainFrame extends JFrame { gameField.setBounds(70, 50, 360, 360); rootPanel.add(gameField); + history.add(new Model(gameField)); + int j = 0; JPanel boxPanel = setupJPanel(); @@ -118,17 +122,28 @@ public class MainFrame extends JFrame { } }); - JButton newButton = new JButton(); - newButton.setVisible(true); - newButton.setBounds(395, 5, 100, 25); - newButton.setText("Vorwärts"); - rootPanel.add(newButton); +// JButton newButton = new JButton(); +// newButton.setVisible(true); +// newButton.setBounds(395, 5, 100, 25); +// 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); + backButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int size = history.size(); + if (size > 1) { + Model model = history.get(size - 2); + history.removeLast(); + model.modelToGameField(); + } + } + }); JButton retryButton = new JButton(); retryButton.setVisible(true); @@ -183,8 +198,6 @@ public class MainFrame extends JFrame { numLabel1.setVisible(true); jPanel.add(numLabel1); - - return jPanel; } @@ -264,6 +277,7 @@ public class MainFrame extends JFrame { loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); currentPosition = 0; currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + history.add(new Model(gameField)); } }); @@ -282,6 +296,7 @@ public class MainFrame extends JFrame { loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); currentPosition = 0; currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + history.add(new Model(gameField)); } }); @@ -300,6 +315,7 @@ public class MainFrame extends JFrame { loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); currentPosition = 0; currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + history.add(new Model(gameField)); } }); From 85d338bf944f6c045a6c64c5b6f2ef0d72dcfcee Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 9 Feb 2023 21:32:45 +0100 Subject: [PATCH 4/9] recursive fibonacci implemented in TasksFrame --- src/main/java/src/TasksFrame.java | 54 +++++++++++++++++++++++++++ src/test/java/src/TasksFrameTest.java | 9 +++++ 2 files changed, 63 insertions(+) diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index 9d32072..b3e75c6 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -64,6 +64,8 @@ public class TasksFrame extends JFrame { public void run() { + setupFib(); + setupHelloWorld(); setupDate(); @@ -107,7 +109,59 @@ public class TasksFrame extends JFrame { setupBmi(); setupReverse(); + } + + private void setupFib() { + JPanel jPanel = setupPanel("Fibonacci"); + + JTextField input = new JTextField(); + input.setBounds(5, 60, 50, 30); + input.setVisible(true); + jPanel.add(input); + JTextField output = new JTextField(); + output.setBounds(5, 100, 200, 30); + output.setVisible(true); + jPanel.add(output); + + JButton button = new JButton("Calculate Fibonacci"); + button.setBounds(70, 60, 150, 30); + button.setVisible(true); + jPanel.add(button); + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + output.setText(""); + Integer n = Integer.valueOf(input.getText()); + int[] a = new int[n+1]; + recursiveFibonacci(a, n); + + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < a.length; i++) { + stringBuffer.append(a[i]); + if (i==a.length-1) { + stringBuffer.append(""); + } else { + stringBuffer.append(", "); + } + } + output.setText(stringBuffer.toString()); + } + }); + } + + public int recursiveFibonacci(int[] a, int n) { + if (n == 0) { + a[n] = 0; + return 0; + } else if (n == 1) { + a[n] = 1; + return 1; + } else { + int i = recursiveFibonacci(a, n - 1) + recursiveFibonacci(a, n-2); + a[n] = i; + return a[n]; + } } private void setupReverse() { diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index c99c3db..19358f6 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -7,6 +7,15 @@ import static org.junit.jupiter.api.Assertions.*; class TasksFrameTest { + @Test + void test_recursiveFib() { + TasksFrame tasksFrame = new TasksFrame(); + int[] ints = new int[10 + 1]; + tasksFrame.recursiveFibonacci(ints, 10); + + assertArrayEquals(new int[]{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}, ints); + } + @Test void bin1() { TasksFrame tasksFrame = new TasksFrame(); From 23d03640e7e6d0a4359221a3b9b87161e497a2bd Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 10 Feb 2023 13:18:44 +0100 Subject: [PATCH 5/9] new ntoi function --- src/main/java/src/TasksFrame.java | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index b3e75c6..9e166cd 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -64,6 +64,8 @@ public class TasksFrame extends JFrame { public void run() { + setupAtoi(); + setupFib(); setupHelloWorld(); @@ -111,6 +113,62 @@ public class TasksFrame extends JFrame { setupReverse(); } + private void setupAtoi(){ + JPanel jPanel = setupPanel("atoi"); + + JLabel jLabel = new JLabel(); + jLabel.setVisible(true); + jLabel.setText("Enter a Integer and get and a Bit"); + jPanel.setBounds(5, 20, 200, 30); + jPanel.add(jLabel); + + JTextField input = new JTextField(); + input.setVisible(true); + input.setBounds(5, 30, 200, 30); + jPanel.add(input); + + JTextField output = new JTextField(); + output.setVisible(true); + output.setBounds(5, 60, 200, 30); + jPanel.add(output); + + JButton jButton = new JButton("iton"); + jButton.setVisible(true); + jButton.setBounds(210, 30, 100, 30); + jPanel.add(jButton); + jButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String text = input.getText(); + int value = Integer.valueOf(text); + + String binary = calcAtoi(value); + output.setText(binary); + + } + }); + +// JButton jButton2 = new JButton("ntoi"); +// jButton2.setVisible(true); +// jButton2.setBounds(210, 30, 100, 30); +// jPanel.add(jButton2); +// jButton2.addActionListener(new ActionListener() { +// @Override +// public void actionPerformed(ActionEvent e) { +// String text = output.getText(); +// int value = Integer.valueOf(text); +// +// String binary = calcAtoi(value); +// output.setText(binary); +// +// } +// }); + } + + public String calcAtoi(int value) { + return Integer.toBinaryString(value); + } + private void setupFib() { JPanel jPanel = setupPanel("Fibonacci"); From 64ed383fe3f53a6fc710a4c5f0f075bc2a1b2606 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 10 Feb 2023 13:40:33 +0100 Subject: [PATCH 6/9] binary to int --- src/main/java/src/TasksFrame.java | 59 ++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index 9e166cd..d449b2d 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -114,7 +114,7 @@ public class TasksFrame extends JFrame { } private void setupAtoi(){ - JPanel jPanel = setupPanel("atoi"); + JPanel jPanel = setupPanel("Interger / Binary Parsing"); JLabel jLabel = new JLabel(); jLabel.setVisible(true); @@ -132,9 +132,9 @@ public class TasksFrame extends JFrame { output.setBounds(5, 60, 200, 30); jPanel.add(output); - JButton jButton = new JButton("iton"); + JButton jButton = new JButton("int to binary"); jButton.setVisible(true); - jButton.setBounds(210, 30, 100, 30); + jButton.setBounds(210, 30, 160, 30); jPanel.add(jButton); jButton.addActionListener(new ActionListener() { @Override @@ -142,30 +142,49 @@ public class TasksFrame extends JFrame { String text = input.getText(); int value = Integer.valueOf(text); - String binary = calcAtoi(value); + String binary = calcItoB(value); + + output.setText(""); output.setText(binary); } }); -// JButton jButton2 = new JButton("ntoi"); -// jButton2.setVisible(true); -// jButton2.setBounds(210, 30, 100, 30); -// jPanel.add(jButton2); -// jButton2.addActionListener(new ActionListener() { -// @Override -// public void actionPerformed(ActionEvent e) { -// String text = output.getText(); -// int value = Integer.valueOf(text); -// -// String binary = calcAtoi(value); -// output.setText(binary); -// -// } -// }); + JButton jButton2 = new JButton("binary to int"); + jButton2.setVisible(true); + jButton2.setBounds(210, 60, 160, 30); + jPanel.add(jButton2); + jButton2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String text = output.getText(); + int value = Integer.valueOf(text); + + String binary = calcBinaryToInt(value); + + input.setText(""); + input.setText(binary); + + } + }); + } + + public String calcBinaryToInt(int value) { + + LinkedList integers = new LinkedList<>(); + + int sum = 0; + int i = 0; + while (value > 0) { + int rest = value % 10; + sum += rest * Math.pow(2, i++); + value/=10; + } + + return String.valueOf(sum); } - public String calcAtoi(int value) { + public String calcItoB(int value) { return Integer.toBinaryString(value); } From e59ba19b9c4e410f06c1de3004ffef1e52956de4 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 10 Feb 2023 13:45:13 +0100 Subject: [PATCH 7/9] binary to int test --- src/test/java/MainFrameTest.java | 5 +---- src/test/java/src/TasksFrameTest.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/test/java/MainFrameTest.java b/src/test/java/MainFrameTest.java index bbfee1d..8a8fbcc 100644 --- a/src/test/java/MainFrameTest.java +++ b/src/test/java/MainFrameTest.java @@ -1,8 +1,5 @@ import org.junit.jupiter.api.*; -import src.GameField; -import src.Main; -import src.MainFrame; -import src.Model; +import src.*; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index 19358f6..f0dc17b 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -7,6 +7,29 @@ import static org.junit.jupiter.api.Assertions.*; class TasksFrameTest { + @Test + public void test_intToBinary() { + TasksFrame tasksFrame = new TasksFrame(); + String s = tasksFrame.calcBinaryToInt(1010); + assertEquals("10", s); + s = tasksFrame.calcBinaryToInt(10); + assertEquals("2", s); + s = tasksFrame.calcBinaryToInt(0); + assertEquals("0", s); + s = tasksFrame.calcBinaryToInt(1); + assertEquals("1", s); + } + +// @Test +// public void test_binaryToInt() { +// +// } +// +// @Test +// public void test_binaryToIntAndIntToBinary() { +// +// } + @Test void test_recursiveFib() { TasksFrame tasksFrame = new TasksFrame(); From e9a594a1a9d45d00066c9419868b6a7c09a655b6 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 10 Feb 2023 13:47:00 +0100 Subject: [PATCH 8/9] int to binary test --- src/test/java/src/TasksFrameTest.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index f0dc17b..05f160b 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -20,10 +20,18 @@ class TasksFrameTest { assertEquals("1", s); } -// @Test -// public void test_binaryToInt() { -// -// } + @Test + public void test_binaryToInt() { + TasksFrame tasksFrame = new TasksFrame(); + String s = tasksFrame.calcItoB(10); + assertEquals("1010", s); + s = tasksFrame.calcBinaryToInt(1); + assertEquals("1", s); + s = tasksFrame.calcBinaryToInt(0); + assertEquals("0", s); + s = tasksFrame.calcBinaryToInt(1); + assertEquals("1", s); + } // // @Test // public void test_binaryToIntAndIntToBinary() { From a706b8377bf13e26191f690726701530f2dfdd2c Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Fri, 10 Feb 2023 13:50:12 +0100 Subject: [PATCH 9/9] from binaray to int and then from int to binary again... works test complete --- src/test/java/src/TasksFrameTest.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index 05f160b..beab9b5 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -32,11 +32,15 @@ class TasksFrameTest { s = tasksFrame.calcBinaryToInt(1); assertEquals("1", s); } -// -// @Test -// public void test_binaryToIntAndIntToBinary() { -// -// } + + @Test + public void test_binaryToIntAndIntToBinary() { + TasksFrame tasksFrame = new TasksFrame(); + String s = tasksFrame.calcBinaryToInt(1010); + String s1 = tasksFrame.calcItoB(Integer.valueOf(s)); + int of = Integer.valueOf(s1); + assertEquals(1010, of); + } @Test void test_recursiveFib() {