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

Created GridRegion #716

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions bin/main/edu/rpi/legup/legup/config
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@
qualifiedClassName="edu.rpi.legup.puzzle.skyscrapers.Skyscrapers"
fileType=".xml"
fileCreationDisabled="true"/>
<puzzle name="RippleEffect"
qualifiedClassName="edu.rpi.legup.puzzle.rippleeffect.RippleEffect"
fileType=".xml"
fileCreationDisabled="false"/>
</puzzles>
</Legup>
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>
53 changes: 53 additions & 0 deletions src/main/java/edu/rpi/legup/model/gameboard/GridRegion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.rpi.legup.model.gameboard;

import java.util.ArrayList;
import java.util.List;

public abstract class GridRegion<T> {

protected List<T> regionCells;

/**
* Region Constructor
*/
public GridRegion() {
this.regionCells = new ArrayList<>();
}

/**
* Adds the cell to the region
* @param cell cell to be added to the region
*/
public void addCell(T cell) {
regionCells.add(cell);
}

/**
* Removes the cell from the region
* @param cell cell to be remove from the region
*/
public void removeCell(T cell) {
regionCells.remove(cell);
}

/**
* Returns the list of cells in the region
* @return list of cells in region
*/
public List<T> getCells() {
return regionCells;
}

/**
* Returns the number of cells in the region
* @return number of cells in the region
*/
public int getSize(){
return regionCells.size();
}

/*
public void colorRegion(){}
*/

}
76 changes: 76 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/rippleeffect/RippleEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package edu.rpi.legup.puzzle.rippleeffect;

import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.RegisterPuzzle;
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import edu.rpi.legup.puzzle.lightup.LightUpView;

@RegisterPuzzle
public class RippleEffect extends Puzzle {

public RippleEffect() {
super();
this.name = "RippleEffect";

this.importer = new RippleEffectImporter(this);
// 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 RippleEffectView((RippleEffectBoard) currentBoard);
addBoardListener(boardView);
}

/**
* Generates a random puzzle based on the difficulty
*
* @param difficulty level of difficulty (1-10)
* @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 RippleEffect
*
* @param rows the number of rows
* @param columns the number of columns
* @return true if the given dimensions are valid for RippleEffect, false otherwise
*/
public boolean isValidDimensions(int rows, int columns) {
return rows > 0 && columns > 0;
}

/**
* Determines if the current board is a valid state
*
* @param board board to check for validity
* @return true if board is valid, false otherwise
*/
@Override
public boolean isBoardComplete(Board board) {
// Implement if needed
return true;
}

/**
* Callback for when the board puzzleElement changes
*
* @param board the board that has changed
*/
@Override
public void onBoardChange(Board board) {
// Implement if needed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.rpi.legup.puzzle.rippleeffect;

import edu.rpi.legup.model.gameboard.GridBoard;

public class RippleEffectBoard extends GridBoard {
public RippleEffectBoard(int width, int height) {
super(width, height);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.rpi.legup.puzzle.rippleeffect;

import edu.rpi.legup.model.gameboard.GridCell;
import java.awt.Point;

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
@@ -0,0 +1,11 @@
package edu.rpi.legup.puzzle.rippleeffect;

public enum RippleEffectCellType {
WHITE(1), BLUE(2), RED(3), YELLOW(4), GREEN(5);

public int value;

RippleEffectCellType(int value) {
this.value = value;
}
}
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
@@ -0,0 +1,39 @@
package edu.rpi.legup.puzzle.rippleeffect;

import edu.rpi.legup.model.PuzzleExporter;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import org.w3c.dom.Document;

public class RippleEffectExporter extends PuzzleExporter {

public RippleEffectExporter(RippleEffect rippleEffect) {
super(rippleEffect);
}

@Override
protected org.w3c.dom.Element createBoardElement(Document newDocument) {
RippleEffectBoard board;
if (puzzle.getTree() != null) {
board = (RippleEffectBoard) puzzle.getTree().getRootNode().getBoard();
}
else {
board = (RippleEffectBoard) puzzle.getBoardView().getBoard();
}

org.w3c.dom.Element boardElement = newDocument.createElement("board");
boardElement.setAttribute("width", String.valueOf(board.getWidth()));
boardElement.setAttribute("height", String.valueOf(board.getHeight()));

org.w3c.dom.Element cellsElement = newDocument.createElement("cells");
for (PuzzleElement puzzleElement : board.getPuzzleElements()) {
RippleEffectCell cell = (RippleEffectCell) puzzleElement;
// if (cell.getData() != -2) {
// org.w3c.dom.Element cellElement = puzzle.getFactory().exportCell(newDocument, puzzleElement);
// cellsElement.appendChild(cellElement);
// }
}

boardElement.appendChild(cellsElement);
return boardElement;
}
}
Loading
Loading