1
0

Compare commits

...

2 Commits

2 changed files with 39 additions and 6 deletions

View File

@ -138,6 +138,24 @@ class CrosswordPuzzle {
clue.horizontal)); clue.horizontal));
} }
} }
get width() {
let colOffset = -Math.min(...this.clues.map(clue => clue.col));
let numCols = colOffset+Math.max(...this.clues.map(clue => clue.afterCol));
return numCols;
}
get height() {
let rowOffset = -Math.min(...this.clues.map(clue => clue.row));
let numRows = rowOffset+Math.max(...this.clues.map(clue => clue.afterRow));
return numRows;
}
get largestDimension() {
return Math.max(this.width, this.height);
}
} }
class Crossword { class Crossword {
constructor(letters, clues, minGuessLength, allTopWords) { constructor(letters, clues, minGuessLength, allTopWords) {
@ -310,6 +328,22 @@ class Crossword {
return {'maxIntersections': maxIntersections, return {'maxIntersections': maxIntersections,
'options': possibleCluesForWord}; 'options': possibleCluesForWord};
} }
function selectRandomCrossword(crosswords) {
let best = [];
let smallestSize = null;
crosswords.forEach(crossword => {
let size = crossword.largestDimension;
if (smallestSize == null || size < smallestSize) {
best = [crossword];
smallestSize = size;
} else if(size == smallestSize) {
best.push(crossword);
}
});
return best[getRandomInt(best.length)];
}
function tryAugmentCrosswords(crosswords) { function tryAugmentCrosswords(crosswords) {
let defaultResult = null; let defaultResult = null;
for (let topWordsIdx in matchingTopWords) { for (let topWordsIdx in matchingTopWords) {
@ -333,8 +367,7 @@ class Crossword {
}).filter(possibleClue => possibleClue.newClue.intersections }).filter(possibleClue => possibleClue.newClue.intersections
== maxIntersections)); == maxIntersections));
// TODO Other considerations for selecting best crossword? // TODO Other considerations for selecting best crossword?
let selectedClue = possibleClues[getRandomInt(possibleClues.length)]; return selectRandomCrossword(possibleClues.map(selectedClue => selectedClue.baseCrossword.withClue(selectedClue.newClue)));
return selectedClue.baseCrossword.withClue(selectedClue.newClue);
} else if (defaultResult === null && possibleClues.some(crosswordOptions => crosswordOptions.possibleClues.options.length > 0)) { } else if (defaultResult === null && possibleClues.some(crosswordOptions => crosswordOptions.possibleClues.options.length > 0)) {
defaultResult = possibleClues; defaultResult = possibleClues;
} }
@ -358,7 +391,7 @@ class Crossword {
.withClue(newClue))); .withClue(newClue)));
} }
} }
return crosswords[getRandomInt(crosswords.length)]; return selectRandomCrossword(crosswords);
} }
let crossword = new CrosswordPuzzle() let crossword = new CrosswordPuzzle()

View File

@ -4,11 +4,11 @@
background-color: black; background-color: black;
} }
#crossword td { #crossword td {
width: 5vw; width: 4ex;
height: 5vw; height: 4ex;
border-collapse: collapse; border-collapse: collapse;
text-align: center; text-align: center;
font-size: 5vw; font-size: 4ex;
} }
#crossword td.blank { #crossword td.blank {
background-color: white; background-color: white;