Skip to content

Commit

Permalink
Merge pull request #752 from Bram-Hub/gridPK
Browse files Browse the repository at this point in the history
Initializing Grid Region Colors
  • Loading branch information
Chase-Grajeda authored Mar 12, 2024
2 parents 4f5e1c2 + 0b260c0 commit 30203e1
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 59 deletions.
29 changes: 29 additions & 0 deletions puzzles files/rippleeffect/RippleEffect1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="RippleEffect">
<board height="3" width="3">
<region>
<cells>
<cell value="1" x="0" y="0"/>
<cell value="2" x="1" y="0"/>
</cells>
</region>
<region>
<cells>
<cell value="4" x="0" y="1"/>
<cell value="3" x="0" y="2"/>
<cell value="5" x="1" y="2"/>
</cells>
</region>
<region>
<cells>
<cell value="6" x="2" y="0"/>
<cell value="7" x="1" y="1"/>
<cell value="8" x="2" y="1"/>
<cell value="9" x="2" y="2"/>
</cells>
</region>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
51 changes: 14 additions & 37 deletions src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,39 +15,38 @@ 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);
}

/**
* Initializes the game board. Called by the invoker of the class
*/
@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;
Expand All @@ -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;
}

Expand All @@ -94,6 +71,6 @@ public boolean isBoardComplete(Board board) {
*/
@Override
public void onBoardChange(Board board) {

// Implement if needed
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public class RippleEffectBoard extends GridBoard {
public RippleEffectBoard(int width, int height) {
super(width, height);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,32 @@
import edu.rpi.legup.model.gameboard.GridCell;
import java.awt.Point;

public class RippleEffectCell extends GridCell<RippleEffectCellType> {
public RippleEffectCell(RippleEffectCellType type, Point location) {
public class RippleEffectCell extends GridCell<Integer> {
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point, RippleEffectRegion> regionsMap;

/**
* Puzzle setting to support row and column inputs
*/
Expand All @@ -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);
}

Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 30203e1

Please sign in to comment.