Skip to content

Commit

Permalink
Merge branch 'binary' into binary
Browse files Browse the repository at this point in the history
  • Loading branch information
ContemporaryNietzsche authored Jul 16, 2024
2 parents 35610eb + b18857f commit 1d31f48
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/main/java/edu/rpi/legup/history/CommandError.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum CommandError {
ONE_SELECTED_VIEW("The selection must have exactly one tree element."),
UNMODIFIABLE_BOARD("The selection contains a board which is not modifiable."),
UNMODIFIABLE_DATA("The selection contains a board where the data element is not modifiable."),
UNMODIFIABLE_DATA_CASE_RULE("The proof tree contains a future case rule, causing previous transitions to become unmodifiable."),
CONTAINS_ROOT("The selection contains the root tree node."),
ONE_CHILD("The selection contains a tree node that does not have exactly one child."),
ADD_WITH_CHILD("The selection contains a tree transition that already has a child tree node."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String getErrorString() {
return null;
}

/** Undoes an command */
/** Undoes a command */
@Override
public void undoCommand() {
Puzzle puzzle = GameBoardFacade.getInstance().getPuzzleModule();
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/edu/rpi/legup/history/EditDataCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import edu.rpi.legup.model.tree.*;
import edu.rpi.legup.ui.boardview.BoardView;
import edu.rpi.legup.ui.boardview.ElementView;
import edu.rpi.legup.ui.lookandfeel.materialdesign.MaterialColors;
import edu.rpi.legup.ui.proofeditorui.treeview.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.List;

Expand All @@ -26,7 +30,7 @@ public class EditDataCommand extends PuzzleCommand {
* EditDataCommand Constructor create a puzzle command for editing a board
*
* @param elementView currently selected puzzle puzzleElement view that is being edited
* @param selection currently selected tree puzzleElement views that is being edited
* @param selection currently selected tree puzzleElement views that are being edited
* @param event mouse event
*/
public EditDataCommand(ElementView elementView, TreeViewSelection selection, MouseEvent event) {
Expand Down Expand Up @@ -54,16 +58,13 @@ public void executeCommand() {

if (treeElement.getType() == TreeElementType.NODE) {
TreeNode treeNode = (TreeNode) treeElement;

if (treeNode.getChildren().isEmpty()) {
if (transition == null) {
transition = tree.addNewTransition(treeNode);
}
puzzle.notifyTreeListeners(listener -> listener.onTreeElementAdded(transition));
}

board = transition.getBoard();

puzzleElement = board.getPuzzleElement(selectedPuzzleElement);
savePuzzleElement = puzzleElement.copy();
} else {
Expand All @@ -73,7 +74,6 @@ public void executeCommand() {
}

Board prevBoard = transition.getParents().get(0).getBoard();

boardView.getElementController().changeCell(event, puzzleElement);

if (prevBoard.getPuzzleElement(selectedPuzzleElement).equalsData(puzzleElement)) {
Expand Down Expand Up @@ -102,35 +102,50 @@ public void executeCommand() {
public String getErrorString() {
List<TreeElementView> selectedViews = selection.getSelectedViews();
if (selectedViews.size() != 1) {
flashTreeViewRed();
return CommandError.ONE_SELECTED_VIEW.toString();
}
TreeElementView selectedView = selection.getFirstSelection();
Board board = selectedView.getTreeElement().getBoard();
PuzzleElement selectedPuzzleElement = elementView.getPuzzleElement();
if (selectedView.getType() == TreeElementType.NODE) {

TreeNodeView nodeView = (TreeNodeView) selectedView;
if (!nodeView.getChildrenViews().isEmpty()) {
flashTreeViewRed();
return CommandError.UNMODIFIABLE_BOARD.toString();
} else {
if (!board.getPuzzleElement(selectedPuzzleElement).isModifiable()) {
return CommandError.UNMODIFIABLE_DATA.toString();
}
} else if (!board.getPuzzleElement(selectedPuzzleElement).isModifiable()) {
flashTreeViewRed();
return CommandError.UNMODIFIABLE_DATA.toString();
}
} else {
TreeTransitionView transitionView = (TreeTransitionView) selectedView;
if (!transitionView.getTreeElement().getBoard().isModifiable()) {
flashTreeViewRed();
return CommandError.UNMODIFIABLE_BOARD.toString();
} else {
if (!board.getPuzzleElement(selectedPuzzleElement).isModifiable()) {
flashTreeViewRed();
return CommandError.UNMODIFIABLE_DATA.toString();
} else if (!board.getPuzzleElement(selectedPuzzleElement).isModifiableCaseRule()) {
flashTreeViewRed();
return CommandError.UNMODIFIABLE_DATA_CASE_RULE.toString();
}
}
}
return null;
}

/** Undoes an command */
/** Causes the TreeView background to flash red for a short duration. */
private void flashTreeViewRed() {
TreeView treeView = getInstance().getLegupUI().getTreePanel().getTreeView();
Color originalColor = treeView.getBackground();
treeView.setBackground(MaterialColors.RED_700);
Timer timer = new Timer(400, e -> treeView.setBackground(originalColor));
timer.setRepeats(false);
timer.start();
}

/** Undoes a command */
@SuppressWarnings("unchecked")
@Override
public void undoCommand() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/edu/rpi/legup/history/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void undo() {
ICommand command = history.get(curIndex--);
command.undo();
LOGGER.info("Undoed " + command.getClass().getSimpleName());


GameBoardFacade.getInstance()
.notifyHistoryListeners(
l -> l.onUndo(curIndex < 0, curIndex == history.size() - 1));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/history/MergeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MergeCommand(TreeViewSelection selection) {
this.transition = null;
}

/** Executes an command */
/** Executes a command */
@Override
public void executeCommand() {
List<TreeElementView> selectedViews = selection.getSelectedViews();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/edu/rpi/legup/model/gameboard/PuzzleElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public abstract class PuzzleElement<T> {
protected T data;
protected boolean isModifiable;
protected boolean isModified;
protected boolean isModifiableCaseRule;
protected boolean isGiven;
protected boolean isValid;
protected int casesDepended;
Expand All @@ -17,6 +18,7 @@ public PuzzleElement() {
this.index = -1;
this.data = null;
this.isModifiable = true;
this.isModifiableCaseRule = true;
this.isModified = false;
this.isGiven = false;
this.isValid = true;
Expand Down Expand Up @@ -73,6 +75,24 @@ public void setModifiable(boolean isModifiable) {
this.isModifiable = isModifiable;
}

/**
* Gets whether this puzzle element is modifiable as a result of a case rule.
*
* @return true if this puzzle element is modifiable, false otherwise
*/
public boolean isModifiableCaseRule() {
return isModifiableCaseRule;
}

/**
* Sets whether this puzzle element is modifiable as a result of a case rule.
*
* @param isModifiableCaseRule true if this puzzle element is modifiable, false otherwise
*/
public void setModifiableCaseRule(boolean isModifiableCaseRule) {
this.isModifiableCaseRule = isModifiableCaseRule;
}

/**
* Gets whether the puzzle element has been modified.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/edu/rpi/legup/model/tree/Tree.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package edu.rpi.legup.model.tree;

import edu.rpi.legup.controller.TreeController;
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.ui.proofeditorui.treeview.TreeView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -95,6 +99,9 @@ public void removeTreeElement(TreeElement element) {
System.out.println("Deleted arrow: " + transition);

transition.getParents().forEach(n -> n.removeChild(transition));
TreeController treeController = new TreeController();
TreeView treeView = new TreeView(treeController);
treeView.removeTreeTransition(transition);
transition.getParents().get(0).getChildren().forEach(TreeTransition::reverify);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CompleteRowColumnDirectRule extends DirectRule {
public CompleteRowColumnDirectRule() {
super(
"BINA-BASC-0003",
"Complete Row Column",
"Complete Row/Column",
"If a row/column of length n contains n/2 of a single value, the remaining cells must contain the other value",
"edu/rpi/legup/images/binary/rules/CompleteRowColumnDirectRule.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import edu.rpi.legup.puzzle.binary.BinaryType;
import java.util.ArrayList;

public class DuplicateRowsOrColumnsContradictionRule extends ContradictionRule {
public class DuplicateRowsColumnsContradictionRule extends ContradictionRule {
private final String NO_CONTRADICTION_MESSAGE =
"Does not contain a contradiction at this index";
private final String INVALID_USE_MESSAGE = "Row or column must have a value in each cell";

public DuplicateRowsOrColumnsContradictionRule() {
public DuplicateRowsColumnsContradictionRule() {
super(
"BINA-CONT-0003",
"Duplicate Rows Or Columns",
"There must not be two rows or two columns that are duplicates",
"edu/rpi/legup/images/binary/rules/DuplicateRowOrColumnContradictionRule.png");
"Duplicate Rows/Columns",
"There must not be two of the same row or two of the same column in the puzzle",
"edu/rpi/legup/images/binary/rules/DuplicateRowsColumnsContradictionRule.png");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public EliminateTheImpossibleDirectRule() {
super(
"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");
"Out of the remaining empty cells in this row or column, this digit must go here, otherwise there will be a future contradiction",
"edu/rpi/legup/images/binary/rules/EliminateTheImpossibleDirectRule.png");
}

// Function to generate all binary strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
import edu.rpi.legup.puzzle.binary.BinaryCell;
import edu.rpi.legup.puzzle.binary.BinaryType;

import java.util.ArrayList;
import java.util.Set;

public class UnbalancedRowOrColumnContradictionRule extends ContradictionRule {
public class UnbalancedRowColumnContradictionRule extends ContradictionRule {

private final String NO_CONTRADICTION_MESSAGE =
"Does not contain a contradiction at this index";
private final String INVALID_USE_MESSAGE = "Row or column must have a value in each cell";

public UnbalancedRowOrColumnContradictionRule() {
public UnbalancedRowColumnContradictionRule() {
super(
"BINA-CONT-0002",
"Unbalanced Row Or Column",
"Unbalanced Row/Column",
"Each row or column must contain an equal number of zeros and ones",
"edu/rpi/legup/images/binary/rules/UnbalancedRowColumnContradictionRule.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public FinishRoomCaseRule() {
"Room can be finished in up to nine ways",
"edu/rpi/legup/images/nurikabe/cases/FinishRoom.png");
this.MAX_CASES = 9;
this.MIN_CASES = 2;
this.MIN_CASES = 1;
this.uniqueCases = new HashSet<>();
}

Expand All @@ -49,7 +49,7 @@ public String checkRuleRaw(TreeTransition transition) {
}
if (childTransitions.size() < MIN_CASES) {
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must have 2 or more children.";
+ ": This case rule must have 1 or more children.";
}
if (childTransitions.size() != legitCases) {
return super.getInvalidUseOfRuleMessage()
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/edu/rpi/legup/ui/ProofEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.PuzzleExporter;
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.tree.Tree;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.binary.BinaryType;
import edu.rpi.legup.save.ExportFileException;
import edu.rpi.legup.save.InvalidFileFormatException;
import edu.rpi.legup.ui.boardview.BoardView;
Expand Down Expand Up @@ -170,7 +168,7 @@ public JMenuBar getMenuBar() {
}
proof.add(add);

delete = new JMenuItem("D\"Check All\"elete");
delete = new JMenuItem("Delete");
delete.addActionListener(a -> treePanel.delete());
if (os.equals("mac")) {
delete.setAccelerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,39 @@ private void removeTreeNode(TreeNode node) {
node.getChildren().forEach(t -> removeTreeTransition(t));
}

private void removeTreeTransition(TreeTransition trans) {
public void removeTreeTransition(TreeTransition trans) {
viewMap.remove(trans);
if (trans.getChildNode() != null) {
removeTreeNode(trans.getChildNode());
}

// Update transition modifiability if removing a case rule
List<TreeNode> parents = trans.getParents();
for (TreeNode parent : parents) {
// if transition is a case rule, unlock ancestor elements up until latest case rule or root node
boolean nextAncestorIsCaseRule = false;
Rule rule = trans.getRule();
if (rule instanceof CaseRule) {
List<TreeNode> ancestors = parent.getAncestors();
for (int i = 0; i < ancestors.size(); i++) {
if (ancestors.get(i).getParent() == null) {
continue;
}
if (nextAncestorIsCaseRule) {
break;
}
for (PuzzleElement element : parent.getBoard().getPuzzleElements()) {
PuzzleElement curElement = ancestors.get(i).getParent().getBoard().getPuzzleElement(element);
if (!curElement.isModifiableCaseRule()) {
curElement.setModifiableCaseRule(true);
}
}
if (ancestors.get(i).getParent().getRule() instanceof CaseRule) {
nextAncestorIsCaseRule = true;
}
}
}
}
}

private void addTreeNode(TreeNode node) {
Expand Down Expand Up @@ -519,21 +547,25 @@ private void addTreeTransition(TreeTransition trans) {
// if transition is a new case rule, lock dependent ancestor elements
Rule rule = trans.getRule();
if (rule instanceof CaseRule && parent.getChildren().size() == 1) {
CaseRule caseRule = (CaseRule) rule;
//CaseRule caseRule = (CaseRule) rule;

List<TreeNode> ancestors = parent.getAncestors();
for (TreeNode ancestor : ancestors) {
// for all ancestors but root
if (ancestor.getParent() == null) {
continue;
}
for (PuzzleElement element :
caseRule.dependentElements(parent.getBoard(), trans.getSelection())) {
// increment and lock
PuzzleElement oldElement =
ancestor.getParent().getBoard().getPuzzleElement(element);
oldElement.setCasesDepended(oldElement.getCasesDepended() + 1);

for (PuzzleElement element : parent.getBoard().getPuzzleElements()) {
PuzzleElement curElement = ancestor.getParent().getBoard().getPuzzleElement(element);
curElement.setModifiableCaseRule(false);
}
// for (PuzzleElement element : caseRule.dependentElements(parent.getBoard(), trans.getSelection())) {
// // increment and lock
// PuzzleElement oldElement = ancestor.getParent().getBoard().getPuzzleElement(element);
// oldElement.setCasesDepended(oldElement.getCasesDepended() + 1);
// oldElement.setModifiable(false);
// }
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1d31f48

Please sign in to comment.