From 765893f084fb3fd657c100067a02c0b950ff8b29 Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Thu, 2 Feb 2023 15:19:59 +0100 Subject: [PATCH] refactorings and it is npos possible to create a new game at sandbox and then load them with the menu bar load new game for all difficulties. its even possible to walk through the games of that difficulty with next button. this button is queuing --- src/main/java/src/GameField.java | 10 ++++- src/main/java/src/MainFrame.java | 66 ++++++++++++++++++++++++++++- src/main/java/src/Model.java | 2 +- src/main/java/src/SandboxFrame.java | 3 +- src/test/java/ModelTest.java | 5 ++- 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/main/java/src/GameField.java b/src/main/java/src/GameField.java index 653f516..01e3bea 100644 --- a/src/main/java/src/GameField.java +++ b/src/main/java/src/GameField.java @@ -122,8 +122,14 @@ public class GameField extends JPanel { public void setValue(int x, int y, int value) { JLabel jLabel = gameField[y][x].getjLabel(); - jLabel.setText(String.valueOf(value)); - jLabel.setVisible(true); + + if (value <= 0) { + jLabel.setText("0"); + jLabel.setVisible(false); + } else { + jLabel.setText(String.valueOf(value)); + jLabel.setVisible(true); + } } diff --git a/src/main/java/src/MainFrame.java b/src/main/java/src/MainFrame.java index f9b2d7c..98378ee 100644 --- a/src/main/java/src/MainFrame.java +++ b/src/main/java/src/MainFrame.java @@ -19,6 +19,8 @@ public class MainFrame extends JFrame { private JSONArray currentGameDifficulty; private Model currentModel; + private JLabel currentGameLabel; + private int currentPosition; public void run() { @@ -79,10 +81,37 @@ public class MainFrame extends JFrame { JButton back = new JButton(); back.setVisible(true); - back.setBounds(5, 5, 100, 25); + back.setBounds(120, 525, 100, 25); rootPanel.add(back); back.setText("Back"); + currentGameLabel = new JLabel(); + currentGameLabel.setVisible(true); + currentGameLabel.setBounds(250, 525, 40, 25); + currentGameLabel.setText("0/0"); + rootPanel.add(currentGameLabel); + + JButton next = new JButton(); + next.setVisible(true); + next.setBounds(280, 525, 100, 25); + rootPanel.add(next); + next.setText("Next"); + next.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + if (currentPosition+1 < currentGameDifficulty.size()) { + currentPosition++; + } else { + currentPosition = 0; + } + JSONObject jsonObject = (JSONObject) currentGameDifficulty.get(currentPosition); + loadDataToGameField(jsonObject); + + currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + } + }); + JButton newButton = new JButton(); newButton.setVisible(true); newButton.setBounds(395, 5, 100, 25); @@ -206,11 +235,46 @@ public class MainFrame extends JFrame { } currentGameDifficulty = (JSONArray) object.get("easyGame"); loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); + currentPosition = 0; + currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); } }); JMenuItem menu_i5=new JMenuItem("Mittel"); + menu_i5.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String data = Model.loadAllData(); + JSONObject object = null; + try { + object = (JSONObject)new JSONParser().parse(data); + } catch (ParseException ex) { + throw new RuntimeException(ex); + } + currentGameDifficulty = (JSONArray) object.get("mediumGame"); + loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); + currentPosition = 0; + currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + } + }); + JMenuItem menu_i6=new JMenuItem("Schwer"); + menu_i6.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String data = Model.loadAllData(); + JSONObject object = null; + try { + object = (JSONObject)new JSONParser().parse(data); + } catch (ParseException ex) { + throw new RuntimeException(ex); + } + currentGameDifficulty = (JSONArray) object.get("hardGame"); + loadDataToGameField((JSONObject) currentGameDifficulty.get(0)); + currentPosition = 0; + currentGameLabel.setText((currentPosition+1)+"/"+currentGameDifficulty.size()); + } + }); m_menu.add(menu_i1); m_menu.add(menu_i2); diff --git a/src/main/java/src/Model.java b/src/main/java/src/Model.java index 66c659a..0cd35fb 100644 --- a/src/main/java/src/Model.java +++ b/src/main/java/src/Model.java @@ -207,7 +207,7 @@ public class Model { JSONObject jsonObject = new JSONObject(); jsonObject.put("x", j); jsonObject.put("y", i); - jsonObject.put("value", getField(i, j)); + jsonObject.put("value", getField(j, i)); values.add(jsonObject); } diff --git a/src/main/java/src/SandboxFrame.java b/src/main/java/src/SandboxFrame.java index 4fffa86..5a9ded2 100644 --- a/src/main/java/src/SandboxFrame.java +++ b/src/main/java/src/SandboxFrame.java @@ -23,7 +23,6 @@ public class SandboxFrame extends JFrame { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setupLayout(); - } private void setupLayout() { @@ -58,6 +57,7 @@ public class SandboxFrame extends JFrame { } private JButton setupSaveButton() { + JButton save = new JButton("Save"); save.setVisible(true); @@ -72,6 +72,7 @@ public class SandboxFrame extends JFrame { } }); + return save; } diff --git a/src/test/java/ModelTest.java b/src/test/java/ModelTest.java index b8fe6ba..f4af955 100644 --- a/src/test/java/ModelTest.java +++ b/src/test/java/ModelTest.java @@ -46,6 +46,7 @@ public class ModelTest extends MainFrame { "8 | 8 8 8 8 8 8 8 8 8\n"; + assertEquals(model.toString(), staticCase, "tested Overridden toString of Model.class"); } @@ -247,7 +248,7 @@ public class ModelTest extends MainFrame { GameField gameField = new GameField(360); for (int i = 0, k = 13; i < 9; i++) { for (int j = 0, l = 17; j < 9; j++) { - gameField.setValue(i, j, (i*l+j*k)%9); + gameField.setValue(j, i, (i*l+j*k)%9); } } Model model = new Model(gameField); @@ -273,7 +274,7 @@ public class ModelTest extends MainFrame { GameField gf = new GameField(360); for (int i = 0, k = 13; i < 9; i++) { for (int j = 0, l = 17; j < 9; j++) { - gf.setValue(i, j, (i*l+j*k)%9); + gf.setValue(j, i, (i*l+j*k)%9); } } Model m = new Model(gf);