diff --git a/src/main/java/edu/rpi/legup/app/GameBoardFacade.java b/src/main/java/edu/rpi/legup/app/GameBoardFacade.java index c928c1209..fbd11fd86 100644 --- a/src/main/java/edu/rpi/legup/app/GameBoardFacade.java +++ b/src/main/java/edu/rpi/legup/app/GameBoardFacade.java @@ -175,44 +175,47 @@ public boolean validateTextInput(String game, String[] statements) throws Runtim * @param columns the number of columns on the board */ public void loadPuzzle(String game, int rows, int columns) throws RuntimeException { - String qualifiedClassName = config.getPuzzleClassForName(game); - LOGGER.debug("Loading " + qualifiedClassName); + if (!game.equals("")) { + String qualifiedClassName = config.getPuzzleClassForName(game); + LOGGER.debug("Loading " + qualifiedClassName); - try { - Class c = Class.forName(qualifiedClassName); - Constructor cons = c.getConstructor(); - Puzzle puzzle = (Puzzle) cons.newInstance(); + try { + Class c = Class.forName(qualifiedClassName); + Constructor cons = c.getConstructor(); + Puzzle puzzle = (Puzzle) cons.newInstance(); - PuzzleImporter importer = puzzle.getImporter(); - if (importer == null) { - LOGGER.error("Puzzle importer is null"); - throw new RuntimeException("Puzzle importer null"); - } + PuzzleImporter importer = puzzle.getImporter(); + if (importer == null) { + LOGGER.error("Puzzle importer is null"); + throw new RuntimeException("Puzzle importer null"); + } - // Theoretically, this exception should never be thrown, since LEGUP should not be - // allowing the user to give row/column input for a puzzle that doesn't support it - if (!importer.acceptsRowsAndColumnsInput()) { - throw new IllegalArgumentException( - puzzle.getName() + " does not accept rows and columns input"); - } + // Theoretically, this exception should never be thrown, since LEGUP should not be + // allowing the user to give row/column input for a puzzle that doesn't support it + if (!importer.acceptsRowsAndColumnsInput()) { + throw new IllegalArgumentException( + puzzle.getName() + " does not accept rows and columns input"); + } - setWindowTitle(puzzle.getName(), "New " + puzzle.getName() + " Puzzle"); - importer.initializePuzzle(rows, columns); + setWindowTitle(puzzle.getName(), "New " + puzzle.getName() + " Puzzle"); + importer.initializePuzzle(rows, columns); - puzzle.initializeView(); - // - // puzzle.getBoardView().onTreeElementChanged(puzzle.getTree().getRootNode()); - setPuzzleEditor(puzzle); - } catch (IllegalArgumentException exception) { - throw new IllegalArgumentException(exception.getMessage()); - } catch (ClassNotFoundException - | NoSuchMethodException - | InvocationTargetException - | IllegalAccessException - | InstantiationException e) { - LOGGER.error(e); - throw new RuntimeException("Puzzle creation error"); + puzzle.initializeView(); + // + // puzzle.getBoardView().onTreeElementChanged(puzzle.getTree().getRootNode()); + setPuzzleEditor(puzzle); + } catch (IllegalArgumentException exception) { + throw new IllegalArgumentException(exception.getMessage()); + } catch (ClassNotFoundException + | NoSuchMethodException + | InvocationTargetException + | IllegalAccessException + | InstantiationException e) { + LOGGER.error(e); + throw new RuntimeException("Puzzle creation error"); + } } + } public void loadPuzzle(String game, String[] statements) { diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/rules/EliminateTheImpossibleDirectRule.java b/src/main/java/edu/rpi/legup/puzzle/binary/rules/EliminateTheImpossibleDirectRule.java index f938e3119..c9a962973 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/rules/EliminateTheImpossibleDirectRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/binary/rules/EliminateTheImpossibleDirectRule.java @@ -20,7 +20,7 @@ public class EliminateTheImpossibleDirectRule extends DirectRule { public EliminateTheImpossibleDirectRule() { super( - "BINA-BASC-0003", + "BINA-BASC-0004", "Eliminate The Impossible", "If three adjacent empty cells are open, prevents a trio of numbers to exist", "edu/rpi/legup/images/binary/rules/OneTileGapDirectRule.png"); diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/rules/binary_reference_sheet.txt b/src/main/java/edu/rpi/legup/puzzle/binary/rules/binary_reference_sheet.txt index c8cb0d1b9..74358a767 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/rules/binary_reference_sheet.txt +++ b/src/main/java/edu/rpi/legup/puzzle/binary/rules/binary_reference_sheet.txt @@ -1,6 +1,7 @@ BINA-BASC-0001 : SurroundPairDirectRule BINA-BASC-0002 : OneTileGapDirectRule BINA-BASC-0003 : CompleteRowColumnDirectRule +BINA-BASC-0004 : EliminateTheImpossibleDirectRule BINA-CONT-0001 : ThreeAdjacentContradictionRule BINA-CONT-0002 : UnbalancedRowOrColumnContradictionRule diff --git a/src/main/java/edu/rpi/legup/puzzle/lightup/LightUp.java b/src/main/java/edu/rpi/legup/puzzle/lightup/LightUp.java index ab95c4658..a73806cd7 100644 --- a/src/main/java/edu/rpi/legup/puzzle/lightup/LightUp.java +++ b/src/main/java/edu/rpi/legup/puzzle/lightup/LightUp.java @@ -47,7 +47,7 @@ public Board generatePuzzle(int difficulty) { * @return true if the given dimensions are valid for Light Up, false otherwise */ public boolean isValidDimensions(int rows, int columns) { - return rows > 0 && columns > 0; + return rows >= 0 && columns >= 0; } /** diff --git a/src/main/java/edu/rpi/legup/ui/CreatePuzzleDialog.java b/src/main/java/edu/rpi/legup/ui/CreatePuzzleDialog.java index 70fbea033..636d5b36f 100644 --- a/src/main/java/edu/rpi/legup/ui/CreatePuzzleDialog.java +++ b/src/main/java/edu/rpi/legup/ui/CreatePuzzleDialog.java @@ -61,6 +61,9 @@ public void actionPerformed(ActionEvent ae) { Config.convertDisplayNameToClassName( (String) gameBox.getSelectedItem()); + if (game.equals("OpeningPuzzleEditor")) { + + } // Check if all 3 TextFields are filled if (game.equals("ShortTruthTable") && textArea.getText().isEmpty()) { System.out.println("Unfilled fields"); diff --git a/src/main/java/edu/rpi/legup/ui/HomePanel.java b/src/main/java/edu/rpi/legup/ui/HomePanel.java index 2e63b91e3..dd0061feb 100644 --- a/src/main/java/edu/rpi/legup/ui/HomePanel.java +++ b/src/main/java/edu/rpi/legup/ui/HomePanel.java @@ -136,8 +136,25 @@ private void initButtons() { this.buttons[0].setVerticalTextPosition(AbstractButton.BOTTOM); this.buttons[0].addActionListener(CursorController.createListener(this, openProofListener)); +// this.buttons[1] = +// new JButton("Create Puzzle") { +// { +// setSize(buttonSize, buttonSize); +// setMaximumSize(getSize()); +// } +// }; +// URL button1IconLocation = +// ClassLoader.getSystemClassLoader() +// .getResource("edu/rpi/legup/images/Legup/homepanel/new_puzzle_file.png"); +// ImageIcon button1Icon = new ImageIcon(button1IconLocation); +// this.buttons[1].setFocusPainted(false); +// this.buttons[1].setIcon(resizeButtonIcon(button1Icon, this.buttonSize, this.buttonSize)); +// this.buttons[1].setHorizontalTextPosition(AbstractButton.CENTER); +// this.buttons[1].setVerticalTextPosition(AbstractButton.BOTTOM); +// this.buttons[1].addActionListener(l -> this.openNewPuzzleDialog()); + this.buttons[1] = - new JButton("Create Puzzle") { + new JButton("Puzzle Editor") { { setSize(buttonSize, buttonSize); setMaximumSize(getSize()); @@ -151,7 +168,8 @@ private void initButtons() { this.buttons[1].setIcon(resizeButtonIcon(button1Icon, this.buttonSize, this.buttonSize)); this.buttons[1].setHorizontalTextPosition(AbstractButton.CENTER); this.buttons[1].setVerticalTextPosition(AbstractButton.BOTTOM); - this.buttons[1].addActionListener(l -> this.openNewPuzzleDialog()); + this.buttons[1].addActionListener(l -> this.openPuzzleEditorDialog()); + //this.buttons[1].addActionListener(l -> this.openNewPuzzleDialog()); this.buttons[2] = new JButton("Edit Existing Puzzle") { @@ -534,6 +552,10 @@ private void openNewPuzzleDialog() { cpd.setVisible(true); } + private void openPuzzleEditorDialog() { + PuzzleEditorDialog ped = new PuzzleEditorDialog(this); + } + private void checkProofAll() { /* * Select dir to grade; recursively grade sub-dirs using traverseDir() @@ -631,27 +653,34 @@ private void traverseDir(File folder, BufferedWriter writer, String path) throws public void openEditorWithNewPuzzle(String game, int rows, int columns) throws IllegalArgumentException { - // Validate the dimensions - GameBoardFacade facade = GameBoardFacade.getInstance(); - boolean isValidDimensions = facade.validateDimensions(game, rows, columns); - if (!isValidDimensions) { - JOptionPane.showMessageDialog( - null, - "The dimensions you entered are invalid. Please double check \n" - + "the number of rows and columns and try again.", - "ERROR: Invalid Dimensions", - JOptionPane.ERROR_MESSAGE); - throw new IllegalArgumentException("ERROR: Invalid dimensions given"); + if (game.equals("")) { + this.legupUI.displayPanel(2); + this.legupUI.getPuzzleEditor().loadPuzzleFromHome(game, rows, columns); } + else { + // Validate the dimensions + GameBoardFacade facade = GameBoardFacade.getInstance(); + boolean isValidDimensions = facade.validateDimensions(game, rows, columns); + if (!isValidDimensions) { + JOptionPane.showMessageDialog( + null, + "The dimensions you entered are invalid. Please double check \n" + + "the number of rows and columns and try again.", + "ERROR: Invalid Dimensions", + JOptionPane.ERROR_MESSAGE); + throw new IllegalArgumentException("ERROR: Invalid dimensions given"); + } - if (this.legupUI == null) { - System.err.println("Error: legupUI is null in HomePanel"); - return; + if (this.legupUI == null) { + System.err.println("Error: legupUI is null in HomePanel"); + return; + } + + // Set game type on the puzzle editor + this.legupUI.displayPanel(2); + this.legupUI.getPuzzleEditor().loadPuzzleFromHome(game, rows, columns); } - // Set game type on the puzzle editor - this.legupUI.displayPanel(2); - this.legupUI.getPuzzleEditor().loadPuzzleFromHome(game, rows, columns); } /** diff --git a/src/main/java/edu/rpi/legup/ui/PuzzleEditorDialog.java b/src/main/java/edu/rpi/legup/ui/PuzzleEditorDialog.java new file mode 100644 index 000000000..46c811732 --- /dev/null +++ b/src/main/java/edu/rpi/legup/ui/PuzzleEditorDialog.java @@ -0,0 +1,20 @@ +package edu.rpi.legup.ui; + +import javax.swing.*; + +public class PuzzleEditorDialog { + + public PuzzleEditorDialog(HomePanel homePanel) { + String game = ""; + int r = 0; + int c = 0; + + try { + homePanel.openEditorWithNewPuzzle(game, r, c); + } catch (IllegalArgumentException e) { + System.out.println("Failed to open editor with new puzzle"); + e.printStackTrace(System.out); + } + } + +} diff --git a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java index cfee707f2..723085658 100644 --- a/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java +++ b/src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java @@ -35,12 +35,14 @@ public class PuzzleEditorPanel extends LegupPanel implements IHistoryListener { private JMenu[] menus; private JMenuItem helpLegup, aboutLegup; private JMenuBar menuBar; - private JToolBar toolBar; + private JToolBar toolBar1; + private JToolBar toolBar2; private JFileChooser folderBrowser; private JFrame frame; private JButton[] buttons; JSplitPane splitPanel; - private JButton[] toolBarButtons; + private JButton[] toolBar1Buttons; + private JButton[] toolBar2Buttons; private JPanel elementPanel; private DynamicView dynamicBoardView; private BoardView boardView; @@ -52,7 +54,6 @@ public class PuzzleEditorPanel extends LegupPanel implements IHistoryListener { private JPanel treePanel; private LegupUI legupUI; private EditorElementController editorElementController; - static final int[] TOOLBAR_SEPARATOR_BEFORE = {2, 4, 8}; public PuzzleEditorPanel(FileDialog fileDialog, JFrame frame, LegupUI legupUI) { this.fileDialog = fileDialog; @@ -248,14 +249,67 @@ public void exitEditor() { @Override public void makeVisible() { this.removeAll(); - - setupToolBar(); + setupToolBar1(); setupContent(); setMenuBar(); } + private void setupToolBar1() { + setToolBar1Buttons(new JButton[2]); + + URL open_url = + ClassLoader.getSystemClassLoader() + .getResource("edu/rpi/legup/images/Legup/Open.png"); + ImageIcon OpenImageIcon = new ImageIcon(open_url); + Image OpenImage = OpenImageIcon.getImage(); + OpenImageIcon = + new ImageIcon( + OpenImage.getScaledInstance( + this.TOOLBAR_ICON_SCALE, + this.TOOLBAR_ICON_SCALE, + Image.SCALE_SMOOTH)); + + JButton open = new JButton("Open", OpenImageIcon); + open.setFocusPainted(false); + open.addActionListener((ActionEvent) -> loadPuzzle()); - private void setupToolBar() { - setToolBarButtons(new JButton[2]); + getToolBar1Buttons()[0] = open; + + toolBar1 = new JToolBar(); + toolBar1.setFloatable(false); + toolBar1.setRollover(true); + toolBar1.add(getToolBar1Buttons()[0]); + + URL create_url = + ClassLoader.getSystemClassLoader() + .getResource("edu/rpi/legup/images/Legup/Open Puzzle.png"); + ImageIcon CreateImageIcon = new ImageIcon(create_url); + Image CreateImage = CreateImageIcon.getImage(); + CreateImageIcon = + new ImageIcon( + CreateImage.getScaledInstance( + this.TOOLBAR_ICON_SCALE, + this.TOOLBAR_ICON_SCALE, + Image.SCALE_SMOOTH)); + + JButton create = new JButton("Create", CreateImageIcon); + create.setFocusPainted(false); + create.addActionListener((ActionEvent) -> { + HomePanel hp = new HomePanel(this.fileDialog, this.frame, this.legupUI); + CreatePuzzleDialog cpd = new CreatePuzzleDialog(this.frame, hp); + cpd.setLocationRelativeTo(null); + cpd.setVisible(true); + }); + getToolBar1Buttons()[1] = create; + + toolBar1.setFloatable(false); + toolBar1.setRollover(true); + toolBar1.add(getToolBar1Buttons()[1]); + + this.add(toolBar1, BorderLayout.NORTH); + } + + private void setupToolBar2() { + setToolBar2Buttons(new JButton[2]); URL save_as = ClassLoader.getSystemClassLoader() @@ -273,12 +327,12 @@ private void setupToolBar() { saveas.setFocusPainted(false); saveas.addActionListener((ActionEvent) -> savePuzzle()); - getToolBarButtons()[0] = saveas; + getToolBar2Buttons()[0] = saveas; - toolBar = new JToolBar(); - toolBar.setFloatable(false); - toolBar.setRollover(true); - toolBar.add(getToolBarButtons()[0]); + toolBar2 = new JToolBar(); + toolBar2.setFloatable(false); + toolBar2.setRollover(true); + toolBar2.add(getToolBar2Buttons()[0]); URL save_and_solve = ClassLoader.getSystemClassLoader() @@ -292,33 +346,35 @@ private void setupToolBar() { this.TOOLBAR_ICON_SCALE, Image.SCALE_SMOOTH)); - JButton saveandsolve = new JButton("Save And Solve", SaveSolveImageIcon); + JButton saveandsolve = new JButton("Save & Solve", SaveSolveImageIcon); saveandsolve.setFocusPainted(false); saveandsolve.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String filename = savePuzzle(); - File puzzlename = new File(filename); - System.out.println(filename); - - GameBoardFacade.getInstance().getLegupUI().displayPanel(1); - GameBoardFacade.getInstance() - .getLegupUI() - .getProofEditor() - .loadPuzzle(filename, new File(filename)); - String puzzleName = - GameBoardFacade.getInstance().getPuzzleModule().getName(); - frame.setTitle(puzzleName + " - " + puzzlename.getName()); + if (GameBoardFacade.getInstance().getPuzzleModule() != null) { + String filename = savePuzzle(); + File puzzlename = new File(filename); + System.out.println(filename); + + GameBoardFacade.getInstance().getLegupUI().displayPanel(1); + GameBoardFacade.getInstance() + .getLegupUI() + .getProofEditor() + .loadPuzzle(filename, new File(filename)); + String puzzleName = + GameBoardFacade.getInstance().getPuzzleModule().getName(); + frame.setTitle(puzzleName + " - " + puzzlename.getName()); + } } }); - getToolBarButtons()[1] = saveandsolve; + getToolBar2Buttons()[1] = saveandsolve; - toolBar.setFloatable(false); - toolBar.setRollover(true); - toolBar.add(getToolBarButtons()[1]); + toolBar2.setFloatable(false); + toolBar2.setRollover(true); + toolBar2.add(getToolBar2Buttons()[1]); - this.add(toolBar, BorderLayout.NORTH); + this.add(toolBar2, BorderLayout.NORTH); } public void loadPuzzleFromHome(String game, int rows, int columns) @@ -443,13 +499,20 @@ public void onClearHistory() { public BoardView getBoardView() { return boardView; } + public JButton[] getToolBar1Buttons() { + return toolBar1Buttons; + } - public JButton[] getToolBarButtons() { - return toolBarButtons; + public void setToolBar1Buttons(JButton[] toolBar1Buttons) { + this.toolBar1Buttons = toolBar1Buttons; } - public void setToolBarButtons(JButton[] toolBarButtons) { - this.toolBarButtons = toolBarButtons; + public JButton[] getToolBar2Buttons() { + return toolBar2Buttons; + } + + public void setToolBar2Buttons(JButton[] toolBar2Buttons) { + this.toolBar2Buttons = toolBar2Buttons; } private void repaintAll() { @@ -475,9 +538,8 @@ public void setPuzzleView(Puzzle puzzle) { if (this.elementFrame != null) { elementFrame.setElements(puzzle); } - - //toolBarButtons[ToolbarName.CHECK.ordinal()].setEnabled(true); - // toolBarButtons[ToolbarName.SAVE.ordinal()].setEnabled(true); + toolBar1.setVisible(false); + setupToolBar2(); } /** Saves a puzzle */ diff --git a/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementFrame.java b/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementFrame.java index 35ab65ff4..97c76919e 100644 --- a/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementFrame.java +++ b/src/main/java/edu/rpi/legup/ui/puzzleeditorui/elementsview/ElementFrame.java @@ -61,8 +61,8 @@ public ButtonGroup getButtonGroup() { // } public void setElements(Puzzle puzzle) { - if (placeableElementPanel.setElements(puzzle.getPlaceableElements()) == 0) { - + if (puzzle != null) { + placeableElementPanel.setElements(puzzle.getPlaceableElements()); } }