Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented general empty puzzle functionality and started with sudoku #179

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/main/java/edu/rpi/legup/app/GameBoardFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,61 @@ public void initializeUI() {
puzzleSolver = legupUI.getProofEditor();
puzzleEditor = legupUI.getPuzzleEditor();
addHistoryListener(legupUI.getProofEditor());
addHistoryListener(legupUI.getPuzzleEditor());
});
}

public void setPuzzle(Puzzle puzzle) {
this.puzzle = puzzle;
this.puzzleSolver.setPuzzleView(puzzle);
this.puzzleEditor.setPuzzleView((puzzle));
this.history.clear();
}

public void setPuzzleEditor(Puzzle puzzle) {
this.puzzle = puzzle;
this.puzzleEditor.setPuzzleView((puzzle));
// this.history.clear();
}

public void setConfig(Config config) {
this.config = config;
}

/**
* Loads an empty puzzle
*
* @param game name of the puzzle
* @param width width of the puzzle
* @param height height of the puzzle
*/
public void loadPuzzle(String game, int width, int height) throws RuntimeException {
String qualifiedClassName = config.getPuzzleClassForName(game);
LOGGER.debug("Loading " + qualifiedClassName);

try {
Class<?> c = Class.forName(qualifiedClassName);
Constructor<?> cons = c.getConstructor();
Puzzle puzzle = (Puzzle) cons.newInstance();
setWindowTitle(puzzle.getName(), "New " + puzzle.getName() + " Puzzle");

PuzzleImporter importer = puzzle.getImporter();
if (importer == null) {
LOGGER.error("Puzzle importer is null");
throw new RuntimeException("Puzzle importer null");
}

importer.initializePuzzle(width, height);
puzzle.initializeView();
// puzzle.getBoardView().onTreeElementChanged(puzzle.getTree().getRootNode());
setPuzzleEditor(puzzle);
}
catch(ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException | InstantiationException e) {
LOGGER.error(e);
throw new RuntimeException("Puzzle creation error");
}
}

/**
* Loads a puzzle file
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/edu/rpi/legup/model/PuzzleImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public PuzzleImporter(Puzzle puzzle) {
this.puzzle = puzzle;
}

/**
* Initializes an empty puzzle
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
public void initializePuzzle(int width, int height) throws RuntimeException {
initializeBoard(width, height);
}

/**
* Initializes the puzzle attributes
*
Expand Down Expand Up @@ -80,6 +91,15 @@ public void initializePuzzle(Node node) throws InvalidFileFormatException {
/**
* Creates the board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
public abstract void initializeBoard(int width, int height) throws RuntimeException;

/**
* Creates an empty board for building
*
* @param node xml document node
* @throws InvalidFileFormatException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public BattleShipImporter(BattleShip battleShip) {
super(battleShip);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/fillapix/FillapixImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public FillapixImporter(Fillapix fillapix) {
super(fillapix);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/heyawake/HeyawakeImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public HeyawakeImporter(Heyawake heyawake) {
super(heyawake);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/lightup/LightUpImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public LightUpImporter(LightUp lightUp) {
super(lightUp);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/masyu/MasyuImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public MasyuImporter(Masyu masyu) {
super(masyu);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public NurikabeImporter(Nurikabe nurikabe) {
super(nurikabe);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,17 @@ private void setGivenCells(ShortTruthTableBoard sttBoard,

}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

//STATEMENT IMPORTER

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public SkyscrapersImporter(Skyscrapers treeTent) {
super(treeTent);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/sudoku/SudokuImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ public SudokuImporter(Sudoku sudoku) {
super(sudoku);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {
SudokuBoard sudokuBoard;
int minorSize = (int) Math.sqrt(width);
if(width != height || minorSize * minorSize != width) throw new RuntimeException("Sudoku Importer: invalid board dimensions");
sudokuBoard = new SudokuBoard(width);

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (sudokuBoard.getCell(x, y) == null) {
int groupIndex = x / minorSize + y / minorSize * minorSize;
SudokuCell cell = new SudokuCell(0, new Point(x, y), groupIndex, width);
cell.setIndex(y * width + x);
cell.setModifiable(true);
sudokuBoard.setCell(x, y, cell);
}
}
}

puzzle.setCurrentBoard(sudokuBoard);
}

/**
* Creates the board for building
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/treetent/TreeTentImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public TreeTentImporter(TreeTent treeTent) {
super(treeTent);
}

/**
* Creates an empty board for building
*
* @param width width of puzzle
* @param height height of puzzle
* @throws RuntimeException
*/
@Override
public void initializeBoard(int width, int height) throws RuntimeException {

}

/**
* Creates the board for building
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/ui/CreatePuzzleDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void initPuzzles() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
String game = (String) gameBox.getSelectedItem();
this.homePanel.openEditorWithNewPuzzle(game);
this.homePanel.openEditorWithNewPuzzle(game, Integer.valueOf(width.getText()), Integer.valueOf(height.getText()));
setVisible(false);
}
else if (e.getSource() == cancel) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/rpi/legup/ui/HomePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ private void openNewPuzzleDialog() {
cpd.setVisible(true);
}

public void openEditorWithNewPuzzle(String game) {
public void openEditorWithNewPuzzle(String game, int width, int height) {
// Set game type on the puzzle editor
this.legupUI.displayPanel(2);
this.legupUI.getPuzzleEditor().loadPuzzleFromHome(game, width, height);
}
}
2 changes: 2 additions & 0 deletions src/main/java/edu/rpi/legup/ui/LegupUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public PuzzleEditorPanel getPuzzleEditor() {
return (PuzzleEditorPanel) panels[2];
}

// public PuzzleEditorPanel getPuzzleEditor() { return (PuzzleEditorPanel) panels[2]; }

public static boolean profFlag(int flag) {
return !((PROF_FLAGS[CONFIG_INDEX] & flag) == 0);
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/edu/rpi/legup/ui/PuzzleEditorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import edu.rpi.legup.controller.BoardController;
import edu.rpi.legup.controller.EditorElementController;
import edu.rpi.legup.controller.ElementController;
import edu.rpi.legup.controller.RuleController;
import edu.rpi.legup.history.ICommand;
import edu.rpi.legup.history.IHistoryListener;
import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.save.InvalidFileFormatException;
import edu.rpi.legup.ui.boardview.BoardView;
import edu.rpi.legup.ui.proofeditorui.rulesview.RuleFrame;
import edu.rpi.legup.ui.puzzleeditorui.elementsview.ElementFrame;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -25,7 +23,7 @@

public class PuzzleEditorPanel extends LegupPanel implements IHistoryListener {

private final static Logger LOGGER = LogManager.getLogger(ProofEditorPanel.class.getName());
private final static Logger LOGGER = LogManager.getLogger(PuzzleEditorPanel.class.getName());
private JMenu[] menus;
private JMenuBar menuBar;
private JToolBar toolBar;
Expand Down Expand Up @@ -74,7 +72,7 @@ protected void setupContent() {
this.add(elementBox);

splitPanel.setDividerLocation(splitPanel.getMaximumDividerLocation()+100);

this.splitPanel = splitPanel;
revalidate();
}

Expand Down Expand Up @@ -172,7 +170,15 @@ private void setupToolBar() {

this.add(toolBar, BorderLayout.NORTH);
}

public void loadPuzzleFromHome(String game, int width, int height) {
GameBoardFacade facade = GameBoardFacade.getInstance();
try {
facade.loadPuzzle(game, width, height);
}
catch (RuntimeException e){
LOGGER.error(e.getMessage());
}
}
public void promptPuzzle() {
GameBoardFacade facade = GameBoardFacade.getInstance();
if (facade.getBoard() != null) {
Expand Down Expand Up @@ -240,8 +246,8 @@ private void repaintAll() {

public void setPuzzleView(Puzzle puzzle) {
this.boardView = puzzle.getBoardView();

dynamicBoardView = new DynamicView(boardView);

this.splitPanel.setRightComponent(dynamicBoardView);
this.splitPanel.setVisible(true);

Expand Down