Improved collision checker
Also removed verbose logging
This commit is contained in:
parent
472106ba3a
commit
79dbff72c1
2 changed files with 24 additions and 13 deletions
|
@ -80,6 +80,11 @@ public class Generator {
|
|||
}
|
||||
System.out.println("Possible placements: " + possiblePlacements.size() + " * 2");
|
||||
|
||||
int totalCharacters = Arrays.stream(words).map(w -> w.length() + 1).reduce(0, Integer::sum);
|
||||
if (totalCharacters > possiblePlacements.size()) {
|
||||
System.out.printf("Warning: there might be no solution (%d > %d)", totalCharacters, possiblePlacements.size());
|
||||
}
|
||||
|
||||
/*for (Word word : words) {
|
||||
PlacedWord placedWord = null;
|
||||
|
||||
|
@ -132,13 +137,13 @@ public class Generator {
|
|||
}
|
||||
|
||||
private Set<PlacedWord> follow(Grid grid, Set<PlacedWord> placedChain, int wordIndex) {
|
||||
System.out.println("Depth " + wordIndex);
|
||||
//System.out.println("Depth " + wordIndex);
|
||||
|
||||
if (wordIndex > peakDepth)
|
||||
peakDepth = wordIndex;
|
||||
|
||||
if (wordIndex == words.length) {
|
||||
System.out.println(" Completed");
|
||||
//System.out.println(" Completed");
|
||||
return placedChain;
|
||||
}
|
||||
|
||||
|
@ -163,8 +168,8 @@ public class Generator {
|
|||
|
||||
PlacedWord candidate = new PlacedWord(placement, vertical, word);
|
||||
|
||||
if (grid.canPlace(candidate)) {
|
||||
System.out.println(" Found candidate");
|
||||
if (grid.canPlace(candidate) != -1) {
|
||||
//System.out.println(" Found candidate");
|
||||
Set<PlacedWord> potentialChain = new HashSet<>(placedChain);
|
||||
potentialChain.add(candidate);
|
||||
|
||||
|
@ -172,20 +177,20 @@ public class Generator {
|
|||
newGrid.placeWord(candidate);
|
||||
|
||||
Set<PlacedWord> newChain = follow(newGrid, potentialChain, wordIndex + 1);
|
||||
System.out.println("Back to depth " + wordIndex);
|
||||
//System.out.println("Back to depth " + wordIndex);
|
||||
|
||||
// if it's null it means there's no good placement, and we should continue searching at this depth
|
||||
if (newChain != null) {
|
||||
System.out.println(" Unfolding"); // is that a correct word?
|
||||
//System.out.println(" Unfolding"); // is that a correct word?
|
||||
return newChain;
|
||||
} else {
|
||||
System.out.println(" Looking further");
|
||||
//System.out.println(" Looking further");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(" No solution");
|
||||
//System.out.println(" No solution");
|
||||
// no placement at this depth
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -46,9 +46,11 @@ public class Grid implements Cloneable {
|
|||
* Can a word be placed, will it not collide
|
||||
*
|
||||
* @param word The word
|
||||
* @return can it be placed
|
||||
* @return can it be placed. -1 = no, 0 = yes, >0 = yes and will be over n characters
|
||||
*/
|
||||
public boolean canPlace(PlacedWord word) {
|
||||
public int canPlace(PlacedWord word) {
|
||||
int cols = 0;
|
||||
|
||||
int x = word.pos().x();
|
||||
int y = word.pos().y();
|
||||
|
||||
|
@ -67,12 +69,16 @@ public class Grid implements Cloneable {
|
|||
c = word.word().text().charAt(i);
|
||||
}
|
||||
|
||||
if (charArray[_x][_y] != 0 && charArray[_x][_y] != c) {
|
||||
return false;
|
||||
if (charArray[_x][_y] != 0) {
|
||||
if (charArray[_x][_y] != c) {
|
||||
return -1;
|
||||
} else {
|
||||
cols++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return cols;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue