I am trying to write an algorithm to check if a nonogram has been solved. I have already create multiple classes to have helper methods. The design for what I have so far is a class for Clues
where the constructor looks like this:
public Clues(int[][] rowClues, int[][] colClues) { // constructor stuff }
I am storing the clues in these 2D arrays. For example, this puzzle would have the clues stored like this:
int[][] rowClues =
new int[][] {
new int[] {0, 2},
new int[] {1, 2},
new int[] {0, 3},
new int[] {0, 3},
new int[] {1, 1},
};
int[][] colClues =
new int[][] {
new int[] {1, 1},
new int[] {0, 1},
new int[] {0, 3},
new int[] {0, 3},
new int[] {3, 1},
};
I created another class called Puzzle
that is composed of a board (for the game) and the clues. I am trying to write a function called isSolved
to check if the Puzzle has been solved, but I am struggling on how to come up with an algorithm.
I currently have this:
public boolean isSolved() {
boolean flag = false;
for (int i=0; i<clue.getColCluesLength(); i++) {
flag = checkColumn(getBoard().getBoardColumn(i), getClues().getColClues(i));
if (flag == false) {
return false;
}
}
for (int i=0; i<clue.getRowCluesLength(); i++) {
flag = checkRow(getBoard().getBoardRow(i), getClues().getRowClues(i));
if (flag == false) {
return false;
}
}
return flag;
}
public boolean isRowSolved() {
for (int i=0; i< clue.getRowCluesLength(); i++) {
for (int j=0; j< clue.getRowClues(i).length; j++) {
}
}
return false;
}
The board (2D array) I have stores int's, and certain values represent elimination, space, and shaded.
I'm thinking I could compare the existing array in the board with the clues array, but I'm not sure how to exactly do that.
Some helper methods for this part are: int[] getRowClues(int index)
, int getColRowCluesLength()
, int getWidth()
(# of cells horizontally in each row of puzzle), getBoard().isShaded()
, getBoard().isEliminated()
, getBoard().isSpace()