Skip to content

Commit

Permalink
Merge pull request #842 from summerhenson/star-battle
Browse files Browse the repository at this point in the history
Star battle
  • Loading branch information
summerhenson authored Jul 19, 2024
2 parents 2b83775 + d93a06e commit 57f09be
Show file tree
Hide file tree
Showing 18 changed files with 857 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ColumnsWithinRegionsDirectRule extends DirectRule {
Expand Down Expand Up @@ -43,40 +46,54 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
// the regions that contain them
Set<Integer> regions = new HashSet<Integer>();
// columns and regions to process
Set<Integer> columnsToCheck = new HashSet<Integer>();
Set<Integer> regionsToCheck = new HashSet<Integer>();
List<Integer> columnsToCheck = new ArrayList<Integer>();
List<Integer> regionsToCheck = new ArrayList<Integer>();
int columnStars = 0;
int regionStars = 0;
regions.add(cell.getGroupIndex());
regionsToCheck.add(cell.getGroupIndex());

while (!columnsToCheck.isEmpty() || !regionsToCheck.isEmpty()) {
for (int r : regionsToCheck) {
for (int i = 0; i < regionsToCheck.size(); ++i) {
int r = regionsToCheck.get(i);
regionStars += board.getRegion(r).numStars();
for (PuzzleElement c : board.getRegion(r).getCells()) {
for (StarBattleCell c : board.getRegion(r).getCells()) {
int column = ((StarBattleCell) c).getLocation().x;
if (columns.add(column)) {
if (column != cell.getLocation().x && c.getType() == StarBattleCellType.UNKNOWN && columns.add(column)) {
columnsToCheck.add(column);
}
}
regionsToCheck.remove(r);
regionsToCheck.remove(i);
--i;
}
for (int c : columnsToCheck) {
for (int j = 0; j < columnsToCheck.size(); ++j) {
int c = columnsToCheck.get(j);
columnStars += board.columnStars(c);
for (int i = 0; i < board.getSize(); ++i) {
int region = board.getCell(c, i).getGroupIndex();
if (regions.add(region)) {
if (board.getCell(c,i).getType() == StarBattleCellType.UNKNOWN && regions.add(region)) {
regionsToCheck.add(region);
}
}
columnsToCheck.remove(c);
columnsToCheck.remove(j);
--j;
}
}
// are the columns and regions missing an equal amount of stars
if (board.getPuzzleNumber() * columns.size() - columnStars
!= board.getPuzzleNumber() * regions.size() - regionStars) {
return "The number of missing stars in the columns and regions must be equal and every extraneous cell must be black!";
}
if (columns.contains(cell.getLocation().x)) {
return "Only black out cells outside the column(s)!";
}
/*
for (int c: columns) {
if (c == cell.getLocation().x) {
return "Only black out cells outside the column(s)!";
}
}
*/
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ColumnsWithinRowsDirectRule extends DirectRule {
Expand Down Expand Up @@ -46,41 +49,48 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
// the rows that contain them
Set<Integer> rows = new HashSet<Integer>();
// columns and rows to process
Set<Integer> columnsToCheck = new HashSet<Integer>();
Set<Integer> rowsToCheck = new HashSet<Integer>();
List<Integer> columnsToCheck = new ArrayList<Integer>();
List<Integer> rowsToCheck = new ArrayList<Integer>();
int columnStars = 0;
int rowStars = 0;
int firstRow = cell.getLocation().y;
rows.add(firstRow);
rowsToCheck.add(firstRow);

while (!columnsToCheck.isEmpty() || !rowsToCheck.isEmpty()) {
for (int r : rowsToCheck) {
for (int i = 0; i < rowsToCheck.size(); ++i) {
int r = rowsToCheck.get(i);
rowStars += board.rowStars(r);
for (PuzzleElement c : board.getRow(r)) {
int column = ((StarBattleCell) c).getLocation().x;
if (columns.add(column)) {
for (StarBattleCell c : board.getRow(r)) {
int column = c.getLocation().x;
if (c.getType() == StarBattleCellType.UNKNOWN && columns.add(column)) {
columnsToCheck.add(column);
}
}
rowsToCheck.remove(r);
rowsToCheck.remove(i);
--i;
}
for (int c : columnsToCheck) {
for (int i = 0; i < columnsToCheck.size(); ++i) {
int c = columnsToCheck.get(i);
columnStars += board.columnStars(c);
for (PuzzleElement r : board.getCol(c)) {
int row = ((StarBattleCell) r).getLocation().y;
if (rows.add(row)) {
for (StarBattleCell r : board.getCol(c)) {
int row = r.getLocation().y;
if (r.getType() == StarBattleCellType.UNKNOWN && rows.add(row)) {
rowsToCheck.add(row);
}
}
columnsToCheck.remove(c);
columnsToCheck.remove(i);
--i;
}
}
// are the columns and regions missing an equal amount of stars
if (board.getPuzzleNumber() * columns.size() - columnStars
!= board.getPuzzleNumber() * rows.size() - rowStars) {
return "The number of missing stars in the columns and rows must be equal and every extraneous cell must be black!";
}
if (columns.contains(cell.getLocation().x)) {
return "Only black out cells outside the column(s)!";
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public RegionsWithinColumnsDirectRule() {
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
ColumnsWithinRegionsDirectRule correspondingRule = new ColumnsWithinRegionsDirectRule();
return correspondingRule.checkRuleRawAt(transition, puzzleElement);
String result = correspondingRule.checkRuleRawAt(transition, puzzleElement);
if (result != null && result.equals("Only black out cells outside the column(s)!")) {
return "Only black out cells outside the region(s)!";
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public RegionsWithinRowsDirectRule() {
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {

RowsWithinRegionsDirectRule correspondingRule = new RowsWithinRegionsDirectRule();
return correspondingRule.checkRuleRawAt(transition, puzzleElement);
String result = correspondingRule.checkRuleRawAt(transition, puzzleElement);
if (result != null && result.equals("Only black out cells outside the row(s)!")) {
return "Only black out cells outside the region(s)!";
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class RowsWithinColumnsDirectRule extends DirectRule {
public RowsWithinColumnsDirectRule() {
super(
"STBL-BASC-0007",
"Rows Withing Columns",
"Rows Within Columns",
"If a number of rows is fully contained by a number of columns with an equal number of missing stars, spaces of other rows in those columns must be black.",
"edu/rpi/legup/images/starbattle/rules/RowsWithinColumnsDirectRule.png");
}
Expand All @@ -29,7 +29,11 @@ public RowsWithinColumnsDirectRule() {
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {

ColumnsWithinRowsDirectRule correspondingRule = new ColumnsWithinRowsDirectRule();
return correspondingRule.checkRuleRawAt(transition, puzzleElement);
String result = correspondingRule.checkRuleRawAt(transition, puzzleElement);
if (result != null && result.equals("Only black out cells outside the column(s)!")) {
return "Only black out cells outside the row(s)!";
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RowsWithinRegionsDirectRule extends DirectRule {
Expand Down Expand Up @@ -44,40 +47,47 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
// the regions that contain them
Set<Integer> regions = new HashSet<Integer>();
// rows and regions to process
Set<Integer> rowsToCheck = new HashSet<Integer>();
Set<Integer> regionsToCheck = new HashSet<Integer>();
List<Integer> rowsToCheck = new ArrayList<Integer>();
List<Integer> regionsToCheck = new ArrayList<Integer>();
int rowStars = 0;
int regionStars = 0;
regions.add(cell.getGroupIndex());
regionsToCheck.add(cell.getGroupIndex());

while (!rowsToCheck.isEmpty() || !regionsToCheck.isEmpty()) {
for (int r : regionsToCheck) {
for (int i = 0; i < regionsToCheck.size(); ++i) {
int r = regionsToCheck.get(i);
regionStars += board.getRegion(r).numStars();
for (PuzzleElement ro : board.getRegion(r).getCells()) {
int row = ((StarBattleCell) ro).getLocation().y;
if (rows.add(row)) {
for (StarBattleCell ro : board.getRegion(r).getCells()) {
int row = ro.getLocation().y;
if (ro.getType() == StarBattleCellType.UNKNOWN && rows.add(row)) {
rowsToCheck.add(row);
}
}
regionsToCheck.remove(r);
regionsToCheck.remove(i);
--i;
}
for (int r : rowsToCheck) {
for (int j = 0; j < rowsToCheck.size(); ++j) {
int r = rowsToCheck.get(j);
rowStars += board.rowStars(r);
for (int i = 0; i < board.getSize(); ++i) {
int region = board.getCell(i, r).getGroupIndex();
if (regions.add(region)) {
if (board.getCell(i,r).getType() == StarBattleCellType.UNKNOWN && regions.add(region)) {
regionsToCheck.add(region);
}
}
rowsToCheck.remove(r);
rowsToCheck.remove(j);
--j;
}
}
// are the columns and regions missing an equal amount of stars
if (board.getPuzzleNumber() * rows.size() - rowStars
!= board.getPuzzleNumber() * regions.size() - regionStars) {
return "The number of missing stars in the rows and regions must be equal and every extraneous cell must be black!";
}
if (rows.contains(cell.getLocation().y)) {
return "Only black out cells outside the row(s)!";
}
return null;
}

Expand Down
Loading

0 comments on commit 57f09be

Please sign in to comment.