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

Force a certain level of connectivity #26

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
23 changes: 20 additions & 3 deletions src/logic/generatePuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import { centerGrid } from "./centerGrid";
import { getMaxShifts } from "./getMaxShifts";
import { makePieces } from "./makePieces";
import { shuffleArray } from "@skedwards88/word_logic";
import { getConnectivityScore } from "./getConnectivityScore";

export function generatePuzzle({ gridSize, minLetters, seed }) {
export function generatePuzzle({ gridSize, minLetters, minConnectivity = 20, seed }) {
let count = 0;
let foundPuzzleWithAcceptableSingletons = false;
let foundPuzzleWithAcceptableConnectivity = false;
const maxFractionSingles = 0.1;

// Create a new seedable random number generator
let pseudoRandomGenerator = seed ? seedrandom(seed) : seedrandom();

while (!foundPuzzleWithAcceptableSingletons) {
while (
!foundPuzzleWithAcceptableSingletons ||
!foundPuzzleWithAcceptableConnectivity
) {
count++;

// Generate an interconnected grid of words
Expand Down Expand Up @@ -51,13 +56,25 @@ export function generatePuzzle({ gridSize, minLetters, seed }) {
foundPuzzleWithAcceptableSingletons =
numSingletons / numPieces < maxFractionSingles;

if (foundPuzzleWithAcceptableSingletons || count > 100) {
const connectivityScore = getConnectivityScore({
pieces: pieceData,
gridSize,
});
foundPuzzleWithAcceptableConnectivity = connectivityScore >= minConnectivity;

if (
(foundPuzzleWithAcceptableSingletons &&
foundPuzzleWithAcceptableConnectivity) ||
count > 100
) {
console.log(count)
return {
pieces: pieceData,
maxShiftLeft: maxShiftLeft,
maxShiftRight: maxShiftRight,
maxShiftUp: maxShiftUp,
maxShiftDown: maxShiftDown,
count,
};
}
}
Expand Down
66 changes: 66 additions & 0 deletions src/logic/generatePuzzle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { generatePuzzle } from "./generatePuzzle";
import {getConnectivityScore} from "./getConnectivityScore"

function getAverage(array) {
const sum = array.reduce(
(currentSum, currentValue) => currentSum + currentValue,
0
);
return sum / array.length;
}

// todo these tests were just playing around to see how grid size affects connectivity and count
// would like to take a fresh look to see:
// - if the count and connectivity (average and spread) changes as grid size changes for a set number of letters
// - if/how much the count changes as you force a higher connectivity
describe("generatePuzzle", () => {
test("count does not increase ", () => {
const numIterations = 500;
const numLetters = 20;
const gridSizes = [8, 7, 6];

let averages = {};

gridSizes.forEach((gridSize) => {
let connectivityScores = [];
let counts = [];

for (let iteration = 0; iteration < numIterations; iteration++) {
const { pieces, count } = generatePuzzle({
gridSize: gridSize,
minLetters: numLetters,
});
counts.push(count);
const connectivityScore = getConnectivityScore({ pieces, gridSize });
connectivityScores.push(connectivityScore);
}

averages[gridSize] = {
averageCount: getAverage(counts),
averageConnectivity: getAverage(connectivityScores),
};
});

// The average number of attempts to generate the puzzle
// for each grid size should not be more than this
// const countTolerance = 2;
// expect(
// Math.abs(
// averages[Math.min(...gridSizes)].averageCount -
// averages[Math.max(...gridSizes)].averageCount
// )
// ).toBeLessThanOrEqual(countTolerance);

// The average connectivity for the smaller grid should be higher than
// that of the larger grid
gridSizes.forEach((gridSize) =>
console.log(`${gridSize}: ${averages[gridSize].averageCount}`)
);
gridSizes.forEach((gridSize) =>
console.log(`${gridSize}: ${averages[gridSize].averageConnectivity}`)
);
// expect(
// averages[Math.min(...gridSizes)].averageConnectivity
// ).toBeGreaterThanOrEqual(averages[Math.max(...gridSizes)].averageConnectivity);
});
});
48 changes: 48 additions & 0 deletions src/logic/getConnectivityScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export function getConnectivityScore({ pieces, gridSize }) {
// Convert the pieces to a grid of bools indicating
// whether there is a letter at each position in the grid
let grid = Array.from({ length: gridSize }, () =>
Array(gridSize).fill(false)
);
for (let index = 0; index < pieces.length; index++) {
const letters = pieces[index].letters;
let top = pieces[index].solutionTop;
for (let rowIndex = 0; rowIndex < letters.length; rowIndex++) {
let left = pieces[index].solutionLeft;
for (let colIndex = 0; colIndex < letters[rowIndex].length; colIndex++) {
if (letters[rowIndex][colIndex]) {
grid[top][left] = true;
}
left += 1;
}
top += 1;
}
}

// The connectivity score is the percentage
// of letters with at least 2 neighbors that are in a different line
// (e.g.top and right counts, top and bottom does not)
let numLetters = 0;
let numLettersWithConnectivity = 0;
for (let rowIndex = 0; rowIndex < grid.length; rowIndex++) {
for (let colIndex = 0; colIndex < grid[0].length; colIndex++) {
const isLetter = grid[rowIndex][colIndex];
if (isLetter) {
numLetters++;
const hasRightNeighbor = grid?.[rowIndex + 1]?.[colIndex];
const hasLeftNeighbor = grid?.[rowIndex - 1]?.[colIndex];
const hasBottomNeighbor = grid?.[rowIndex]?.[colIndex + 1];
const hasTopNeighbor = grid?.[rowIndex]?.[colIndex - 1];
if (
(hasLeftNeighbor && hasTopNeighbor) ||
(hasTopNeighbor && hasRightNeighbor) ||
(hasRightNeighbor && hasBottomNeighbor) ||
(hasBottomNeighbor && hasLeftNeighbor)
) {
numLettersWithConnectivity++;
}
}
}
}
return 100 * (numLettersWithConnectivity / numLetters);
}