diff --git a/puzzles files/rippleeffect/RippleEffect1 b/puzzles files/rippleeffect/RippleEffect1 new file mode 100644 index 000000000..66f22404c --- /dev/null +++ b/puzzles files/rippleeffect/RippleEffect1 @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java index 761331b9b..c4c3d99a8 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java @@ -4,16 +4,8 @@ import edu.rpi.legup.model.RegisterPuzzle; import edu.rpi.legup.model.gameboard.Board; import edu.rpi.legup.model.gameboard.PuzzleElement; -import edu.rpi.legup.model.rules.ContradictionRule; - - -/** - * 1) Number is duplicated in a row or a column, the space between the duplicated numbers must be equal to or larger than the value of the number. - * 2) Each Room contains consecutive numbers starting from 1. - * 3) If a number is duplicated in a row or a column, the space between the duplicated numbers must be equal to or larger than the value of the number. - */ - - +import edu.rpi.legup.puzzle.lightup.LightUpBoard; +import edu.rpi.legup.puzzle.lightup.LightUpView; @RegisterPuzzle public class RippleEffect extends Puzzle { @@ -23,9 +15,8 @@ public RippleEffect() { this.name = "RippleEffect"; this.importer = new RippleEffectImporter(this); - // this.exporter = new LightUpExporter(this); - - // this.factory = new LightUpCellFactory(); + // Uncomment the following line if you have a RippleEffectExporter + // this.exporter = new RippleEffectExporter(this); } /** @@ -33,29 +24,29 @@ public RippleEffect() { */ @Override public void initializeView() { - // boardView = new LightUpView((LightUpBoard) currentBoard); - // boardView.setBoard(currentBoard); - // addBoardListener(boardView); + boardView = new RippleEffectView((RippleEffectBoard) currentBoard); + addBoardListener(boardView); } /** - * Generates a random edu.rpi.legup.puzzle based on the difficulty + * Generates a random puzzle based on the difficulty * * @param difficulty level of difficulty (1-10) - * @return board of the random edu.rpi.legup.puzzle + * @return board of the random puzzle */ @Override public Board generatePuzzle(int difficulty) { + // Implement if needed return null; } @Override /** - * Determines if the given dimensions are valid for Light Up + * Determines if the given dimensions are valid for RippleEffect * * @param rows the number of rows * @param columns the number of columns - * @return true if the given dimensions are valid for Light Up, false otherwise + * @return true if the given dimensions are valid for RippleEffect, false otherwise */ public boolean isValidDimensions(int rows, int columns) { return rows > 0 && columns > 0; @@ -69,21 +60,7 @@ public boolean isValidDimensions(int rows, int columns) { */ @Override public boolean isBoardComplete(Board board) { - // LightUpBoard lightUpBoard = (LightUpBoard) board; - // lightUpBoard.fillWithLight(); - - // for (ContradictionRule rule : contradictionRules) { - // if (rule.checkContradiction(lightUpBoard) == null) { - // System.out.println(rule.getRuleName()); - // return false; - // } - // } - // for (PuzzleElement data : lightUpBoard.getPuzzleElements()) { - // LightUpCell cell = (LightUpCell) data; - // if ((cell.getType() == LightUpCellType.UNKNOWN || cell.getType() == LightUpCellType.EMPTY) && !cell.isLite()) { - // return false; - // } - // } + // Implement if needed return true; } @@ -94,6 +71,6 @@ public boolean isBoardComplete(Board board) { */ @Override public void onBoardChange(Board board) { - + // Implement if needed } -} +} \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectBoard.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectBoard.java index 0c1abbe27..f1822c72d 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectBoard.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectBoard.java @@ -6,4 +6,4 @@ public class RippleEffectBoard extends GridBoard { public RippleEffectBoard(int width, int height) { super(width, height); } -} +} \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCell.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCell.java index 20edf27f5..eb56368c0 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCell.java @@ -3,8 +3,32 @@ import edu.rpi.legup.model.gameboard.GridCell; import java.awt.Point; -public class RippleEffectCell extends GridCell { - public RippleEffectCell(RippleEffectCellType type, Point location) { +public class RippleEffectCell extends GridCell { + private int number; + + public RippleEffectCell(int type, Point location, int number) { super(type, location); + this.number = number; + } + + public RippleEffectCellType getType() { + switch (getData()) { + case 1: + return RippleEffectCellType.WHITE; + case 2: + return RippleEffectCellType.BLUE; + case 3: + return RippleEffectCellType.RED; + case 4: + return RippleEffectCellType.YELLOW; + case 5: + return RippleEffectCellType.GREEN; + default: + return null; + } + } + + public int getNumber(){ + return number; } -} +} \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellController.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellController.java new file mode 100644 index 000000000..b1b87da8e --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellController.java @@ -0,0 +1,15 @@ +package edu.rpi.legup.puzzle.rippleeffect; + +import edu.rpi.legup.controller.ElementController; +import edu.rpi.legup.model.gameboard.PuzzleElement; + +import java.awt.event.MouseEvent; + +public class RippleEffectCellController extends ElementController { + + @Override + public void changeCell(MouseEvent e, PuzzleElement data) { + // Since we don't need to change any cell data in Ripple Effect, + // we leave this method empty. + } +} \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellType.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellType.java index 2442b898e..9be4b3582 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellType.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectCellType.java @@ -1,9 +1,10 @@ package edu.rpi.legup.puzzle.rippleeffect; public enum RippleEffectCellType { - EMPTY(0), FILLED(1); + WHITE(1), BLUE(2), RED(3), YELLOW(4), GREEN(5); + public int value; - + RippleEffectCellType(int value) { this.value = value; } diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectElementView.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectElementView.java new file mode 100644 index 000000000..df528e9e8 --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectElementView.java @@ -0,0 +1,68 @@ +package edu.rpi.legup.puzzle.rippleeffect; + +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.ui.boardview.GridElementView; + +import java.awt.*; + +public class RippleEffectElementView extends GridElementView { + + public RippleEffectElementView(RippleEffectCell cell) { + super(cell); + } + + /** + * Gets the PuzzleElement associated with this view + * + * @return PuzzleElement associated with this view + */ + @Override + public RippleEffectCell getPuzzleElement() { + return (RippleEffectCell) super.getPuzzleElement(); + } + + @Override + public void drawElement(Graphics2D graphics2D) { + RippleEffectCell cell = getPuzzleElement(); + RippleEffectCellType type = cell.getType(); + + // Draw the cell based on its type + switch (type) { + case WHITE: + graphics2D.setColor(Color.WHITE); + break; + case BLUE: + graphics2D.setColor(Color.BLUE); + break; + case RED: + graphics2D.setColor(Color.RED); + break; + case YELLOW: + graphics2D.setColor(Color.YELLOW); + break; + case GREEN: + graphics2D.setColor(Color.GREEN); + break; + default: + // For BLACK and any other type + graphics2D.setColor(Color.BLACK); + break; + } + + // Fill the cell with the color + graphics2D.fillRect(location.x, location.y, size.width, size.height); + + // Draw a black border + graphics2D.setColor(Color.BLACK); + graphics2D.drawRect(location.x, location.y, size.width, size.height); + + // Draw the number inside the cell + graphics2D.setColor(Color.BLACK); + graphics2D.setFont(new Font("Arial", Font.BOLD, 14)); + String data = String.valueOf(cell.getNumber()); + FontMetrics metrics = graphics2D.getFontMetrics(); + int x = location.x + (size.width - metrics.stringWidth(data)) / 2; + int y = location.y + ((size.height - metrics.getHeight()) / 2) + metrics.getAscent(); + graphics2D.drawString(data, x, y); + } +} \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectImporter.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectImporter.java index bff0765e3..2dfea3713 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectImporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectImporter.java @@ -6,12 +6,17 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.awt.Point; - +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class RippleEffectImporter extends PuzzleImporter { public RippleEffectImporter(RippleEffect rippleEffect) { super(rippleEffect); } + private Map regionsMap; + /** * Puzzle setting to support row and column inputs */ @@ -37,17 +42,6 @@ public boolean acceptsTextInput() { @Override public void initializeBoard(int rows, int columns) { RippleEffectBoard rippleEffectBoard = new RippleEffectBoard(rows, columns); - for (int y = 0; y < rows; y++) { - for (int x = 0; x < columns; x++) { - // new ripple effect cell - if (rippleEffectBoard.getCell(x, y) == null) { - RippleEffectCell cell = new RippleEffectCell(RippleEffectCellType.EMPTY, new Point(x, y)); - cell.setIndex(y * columns + x); - cell.setModifiable(true); - rippleEffectBoard.setCell(x, y, cell); - } - } - } puzzle.setCurrentBoard(rippleEffectBoard); } @@ -60,7 +54,45 @@ public void initializeBoard(int rows, int columns) { */ @Override public void initializeBoard(Node node) throws InvalidFileFormatException { - if (node == null) throw new InvalidFileFormatException("Invalid format"); + Element puzzleElement = (Element) node; + + NodeList regionNodes = puzzleElement.getElementsByTagName("region"); + if (regionNodes.getLength() == 0) { + throw new InvalidFileFormatException("No regions found for the RippleEffect puzzle"); + } + + int width = Integer.parseInt(puzzleElement.getAttribute("width")); + int height = Integer.parseInt(puzzleElement.getAttribute("height")); + + RippleEffectBoard rippleEffectBoard = new RippleEffectBoard(width, height); // Initialize the board with width and height from XML + int cellType = 1; // Start with cell type 1 + + for (int i = 0; i < regionNodes.getLength(); i++) { + Element regionElement = (Element) regionNodes.item(i); + NodeList cellNodes = regionElement.getElementsByTagName("cell"); + + for (int j = 0; j < cellNodes.getLength(); j++) { + Element cellElement = (Element) cellNodes.item(j); + int x = Integer.parseInt(cellElement.getAttribute("x")); + int y = Integer.parseInt(cellElement.getAttribute("y")); + int value = Integer.parseInt(cellElement.getAttribute("value")); + + Point cellPoint = new Point(x, y); + + // Create the RippleEffectCell with the cell type and value + RippleEffectCell cell = new RippleEffectCell(cellType, cellPoint, value); + cell.setIndex(y * width + x); // Calculate the index based on width and height + cell.setModifiable(true); + + // Add the cell to the board + rippleEffectBoard.setCell(x, y, cell); + } + + // Increment cell type for the next region + cellType++; + } + + puzzle.setCurrentBoard(rippleEffectBoard); } diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectRegion.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectRegion.java new file mode 100644 index 000000000..80b534a2d --- /dev/null +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectRegion.java @@ -0,0 +1,19 @@ +package edu.rpi.legup.puzzle.rippleeffect; + +import java.awt.Color; + +public class RippleEffectRegion { + private char color; + + public RippleEffectRegion(char color) { + this.color = color; + } + + public char getColor() { + return color; + } + + public void setColor(char color) { + this.color = color; + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectView.java b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectView.java index 46647299f..09699a244 100644 --- a/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectView.java +++ b/src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffectView.java @@ -1,5 +1,25 @@ package edu.rpi.legup.puzzle.rippleeffect; -public class RippleEffectView { - -} +import edu.rpi.legup.controller.BoardController; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.puzzle.lightup.LightUpCellController; +import edu.rpi.legup.ui.boardview.GridBoardView; + +import java.awt.*; + +public class RippleEffectView extends GridBoardView { + + public RippleEffectView(RippleEffectBoard board) { + super(new BoardController(), new RippleEffectCellController(), board.getDimension()); + + for (PuzzleElement puzzleElement : board.getPuzzleElements()) { + RippleEffectCell cell = (RippleEffectCell) puzzleElement; + Point loc = cell.getLocation(); + RippleEffectElementView elementView = new RippleEffectElementView(cell); + elementView.setIndex(cell.getIndex()); + elementView.setSize(elementSize); + elementView.setLocation(new Point(loc.x * elementSize.width, loc.y * elementSize.height)); + elementViews.add(elementView); + } + } +} \ No newline at end of file