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

New puzzle skyscrapers #247

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ public List<SkyscrapersCell> getDiagonals(SkyscrapersCell cell, SkyscrapersType
return dia;
}

/**
* Gets the cells of a certain type in a given row/column
*
* @param index: y pos of row or x pos of col,
* type of cell to collect,
* boolean true if row, false if col
* @return list of cells of the given type, ordered west to east or north to south
*/
public List<SkyscrapersCell> getRowCol(int index, SkyscrapersType type, boolean isRow) {
List<SkyscrapersCell> list = new ArrayList<>();
if (isRow) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package edu.rpi.legup.puzzle.skyscrapers.rules;

import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.skyscrapers.*;

import java.util.List;

public class CellForNumberCaseRule extends CaseRule {
//select a certain row/col? select a certain number?
public CellForNumberCaseRule() {
super("SKYS-CASE-0002", "Cell For Number",
"A number (1-n) must appear in any given row/column",
"edu/rpi/legup/images/skyscrapers/cases/CellForNumber.png");
}

@Override
public CaseBoard getCaseBoard(Board board) {
SkyscrapersBoard currentBoard = (SkyscrapersBoard) board.copy();
currentBoard.setModifiable(false);
CaseBoard caseBoard = new CaseBoard(currentBoard, this);
for (SkyscrapersClue data : currentBoard.getRow()) {
System.out.println(data.getType());
caseBoard.addPickableElement(data);
}
for (SkyscrapersClue data : currentBoard.getCol()) {
System.out.println(data.getType());
caseBoard.addPickableElement(data);
}
return caseBoard;
}

@Override
public List<Board> getCases(Board board, PuzzleElement puzzleElement) {
return null;
}

@Override
public String checkRuleRaw(TreeTransition transition) {
return null;
}

@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public DuplicateNumberContradictionRule() {
*/
@Override
public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
//TODO:? Refactor to count each row/col once rather than per cell (override checkContradiction)
SkyscrapersCell cell = (SkyscrapersCell) puzzleElement;
SkyscrapersBoard skyscrapersboard = (SkyscrapersBoard) board;
Point loc = cell.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import edu.rpi.legup.puzzle.skyscrapers.SkyscrapersType;

import java.awt.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ExceedingVisibilityContradictionRule extends ContradictionRule {
Expand All @@ -29,7 +31,6 @@ public ExceedingVisibilityContradictionRule() {
*/
@Override
public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
//why is this called for every cell?
SkyscrapersCell cell = (SkyscrapersCell) puzzleElement;
SkyscrapersBoard skyscrapersboard = (SkyscrapersBoard) board;
Point loc = cell.getLocation();
Expand All @@ -41,38 +42,33 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
int south = skyscrapersboard.getColClues().get(loc.x).getData();

//check row
//from west border
int max = 0;
int count = 0;
boolean complete = true;
for (int i = 0; i < skyscrapersboard.getWidth(); i++) {
SkyscrapersCell c = skyscrapersboard.getCell(i, loc.y);
if (c.getType() == SkyscrapersType.Number && c.getData() > max) {
//System.out.print(c.getData());
//System.out.println(cell.getData());
max = c.getData();
count++;
}
if (c.getType() == SkyscrapersType.UNKNOWN) {
complete = false;
break;
}
}
if (count > west && complete) {
return null;
}
List<SkyscrapersCell> row = skyscrapersboard.getRowCol(loc.y,SkyscrapersType.Number,true);
if(row.size()==skyscrapersboard.getWidth()){
//from west border
for(SkyscrapersCell c : row){
if (c.getData() > max) {
System.out.print(c.getData());
//System.out.println(cell.getData());
max = c.getData();
count++;
}
}
if (count > west) {
return null;
}

if(complete) {
//from east border
max = 0;
count = 0;
for (int i = skyscrapersboard.getWidth() - 1; i >= 0; i--) {
SkyscrapersCell c = skyscrapersboard.getCell(i, loc.y);
if (c.getType() == SkyscrapersType.Number && c.getData() > max) {
//System.out.print(c.getData());
//from east border
Collections.reverse(row);
for(SkyscrapersCell c : row){
if (c.getData() > max) {
System.out.print(c.getData());
//System.out.println(cell.getData());
max = c.getData();
count = count + 1;
count++;
}
}
if (count > east) {
Expand All @@ -81,37 +77,35 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
}

//check column
//from north border
max = 0;
count = 0;
complete = true;
for (int i = 0; i < skyscrapersboard.getHeight(); i++) {
SkyscrapersCell c = skyscrapersboard.getCell(loc.x, i);
if (c.getType() == SkyscrapersType.Number && c.getData() > max) {
//System.out.print(c.getData());
//System.out.println(cell.getData());
max = c.getData();
count = count + 1;
}
if (c.getType() == SkyscrapersType.UNKNOWN) {
complete = false;
}
}
if (count > north && complete) {
return null;
}
List<SkyscrapersCell> col = skyscrapersboard.getRowCol(loc.x,SkyscrapersType.Number,false);
if(col.size()==skyscrapersboard.getHeight()){
//from north border
max = 0;
count = 0;
for(SkyscrapersCell c : col){
System.out.println(c.getData());
if (c.getData() > max) {

//System.out.println(cell.getData());
max = c.getData();
count++;
}
}
if (count > north) {
return null;
}

if(complete) {
//from south border
max = 0;
count = 0;
for (int i = skyscrapersboard.getHeight() - 1; i >= 0; i--) {
SkyscrapersCell c = skyscrapersboard.getCell(loc.x, i);
if (c.getType() == SkyscrapersType.Number && c.getData() > max) {
//System.out.print(c.getData());
Collections.reverse(col);
for(SkyscrapersCell c : col){
System.out.println(c.getData());
if (c.getData() > max) {

//System.out.println(cell.getData());
max = c.getData();
count = count + 1;
count++;
}
}
if (count > south) {
Expand All @@ -122,4 +116,25 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
//System.out.print("Does not contain a contradiction at this index");
return super.getNoContradictionMessage();
}

/**
* Checks whether the tree node has a contradiction using this rule
*
* @param board board to check contradiction
* @return null if the tree node contains a contradiction, otherwise error message
*/
@Override
public String checkContradiction(Board board) {
SkyscrapersBoard skyscrapersBoard = (SkyscrapersBoard) board;
for (int i = 0; i < skyscrapersBoard.getWidth(); i++) {
//checks the middle diagonal (checkContradictionAt checks row/col off each)
String checkStr = checkContradictionAt(board, skyscrapersBoard.getCell(i,i));
if (checkStr == null) {
return checkStr;
}
}
return "No instance of the contradiction " + this.ruleName + " here";
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
SkyscrapersCell initCell = (SkyscrapersCell) initialBoard.getPuzzleElement(puzzleElement);
SkyscrapersBoard finalBoard = (SkyscrapersBoard) transition.getBoard();
SkyscrapersCell finalCell = (SkyscrapersCell) finalBoard.getPuzzleElement(puzzleElement);
if (!(initCell.getType() == SkyscrapersType.UNKNOWN && finalCell.getType() == SkyscrapersType.Number)) {
return super.getInvalidUseOfRuleMessage() + ": Modified cells must be number";
if (initCell.getType() != SkyscrapersType.UNKNOWN || finalCell.getType() != SkyscrapersType.Number) {
return super.getInvalidUseOfRuleMessage() + ": Modified cells must transition from unknown to number";
}

SkyscrapersBoard emptyCase = initialBoard.copy();
Expand Down Expand Up @@ -91,20 +91,21 @@ private boolean isForced(SkyscrapersBoard board, SkyscrapersCell cell) {
@Override
public Board getDefaultBoard(TreeNode node) {
SkyscrapersBoard initialBoard = (SkyscrapersBoard) node.getBoard();
SkyscrapersBoard lightUpBoard = (SkyscrapersBoard) node.getBoard().copy();
System.out.println(lightUpBoard.getPuzzleElements().size());
for (PuzzleElement element : lightUpBoard.getPuzzleElements()) {
System.out.println("123");
SkyscrapersBoard modBoard = (SkyscrapersBoard) node.getBoard().copy();
System.out.println(modBoard.getPuzzleElements().size());
for (PuzzleElement element : modBoard.getPuzzleElements()) {
System.out.println("123");
SkyscrapersCell cell = (SkyscrapersCell) element;
if (cell.getType() == SkyscrapersType.UNKNOWN && isForced(initialBoard, cell)) {
//cell.setData(SkyscrapersType.BULB.value);
lightUpBoard.addModifiedData(cell);
modBoard.addModifiedData(cell);
}
}
if (lightUpBoard.getModifiedData().isEmpty()) {
System.out.println(modBoard.getModifiedData().isEmpty());
if (modBoard.getModifiedData().isEmpty()) {
return null;
} else {
return lightUpBoard;
return modBoard;
}
}
}
Loading