Skip to content

Commit

Permalink
Merge pull request #49 from skedwards88/cleanup
Browse files Browse the repository at this point in the history
Relocate some logic
  • Loading branch information
skedwards88 authored Oct 18, 2024
2 parents 1986afd + 88437b6 commit 11e0cd6
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 124 deletions.
6 changes: 2 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ For screenshots:
- add some custom daily puzzles

- add link to sponsors?
- relocate to new file:
- getWordValidityGrids
- countingGrid
- just rename dispatch params to dispatcher everywhere
- put arrayToGrid to common or to word-logic package
- piecesOverlapQ should use getGridFromPieces

- move common functions to word-logic package
File renamed without changes.
File renamed without changes.
119 changes: 4 additions & 115 deletions src/components/Board.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,8 @@
import React from "react";
import Piece from "./Piece";
import DragShadow from "./DragShadow";
import {getGridFromPieces} from "../logic/getGridFromPieces";
import {isKnown} from "@skedwards88/word_logic";
import {trie} from "../logic/trie";
import {getWordsFromPieces} from "../logic/getWordsFromPieces";
import {transposeGrid} from "@skedwards88/word_logic";

// Returns a grid with the number of letters at each location in the grid
export function countingGrid(height, width, pieces) {
let grid = Array(height)
.fill(undefined)
.map(() => Array(width).fill(0));

for (let piece of pieces) {
const letters = piece.letters;
let top = piece.boardTop ?? piece.dragGroupTop;
for (let rowIndex = 0; rowIndex < letters.length; rowIndex++) {
let left = piece.boardLeft ?? piece.dragGroupLeft;
for (let colIndex = 0; colIndex < letters[rowIndex].length; colIndex++) {
if (letters[rowIndex][colIndex]) {
grid[top][left]++;
}
left++;
}
top++;
}
}
return grid;
}

function getHorizontalValidityGrid({grid, originalWords}) {
// return a 2D array of bools indicating whether
// the position corresponds to a letter on the board
// that is part of a valid horizontal word
const height = grid.length;
const width = grid[0].length;

const horizontalValidityGrid = Array(height)
.fill(undefined)
.map(() => Array(width).fill(false));

for (const [rowIndex, row] of grid.entries()) {
let word = "";
let indexes = [];
for (const [columnIndex, letter] of row.entries()) {
if (letter != "") {
word += letter;
indexes.push(columnIndex);
} else {
if (word.length > 1) {
// If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
// Otherwise, check whether it is a word in the trie.
let isWord = originalWords.includes(word);
if (!isWord) {
({isWord} = isKnown(word, trie));
}
if (isWord) {
indexes.forEach(
(index) => (horizontalValidityGrid[rowIndex][index] = true),
);
}
}
word = "";
indexes = [];
}
}
// Also end the word if we reach the end of the row
if (word.length > 1) {
// If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
// Otherwise, check whether it is a word in the trie.
let isWord = originalWords.includes(word);
if (!isWord) {
({isWord} = isKnown(word, trie));
}
if (isWord) {
indexes.forEach(
(index) => (horizontalValidityGrid[rowIndex][index] = true),
);
}
}
}

return horizontalValidityGrid;
}

export function getWordValidityGrids({
pieces,
gridSize,
includeOriginalSolution = true,
}) {
const originalWords = includeOriginalSolution
? getWordsFromPieces({
pieces,
gridSize,
solution: true,
})
: [];

const grid = getGridFromPieces({pieces, gridSize, solution: false});

const horizontalValidityGrid = getHorizontalValidityGrid({
grid,
originalWords,
});

const transposedGrid = transposeGrid(grid);
const horizontalTransposedValidityGrid = getHorizontalValidityGrid({
grid: transposedGrid,
originalWords,
});
const verticalValidityGrid = transposeGrid(horizontalTransposedValidityGrid);

return [horizontalValidityGrid, verticalValidityGrid];
}
import {getLetterCountPerSquare} from "../logic/getLetterCountPerSquare";
import {getWordValidityGrids} from "../logic/getWordValidityGrids";

export default function Board({
pieces,
Expand All @@ -131,7 +20,7 @@ export default function Board({

const overlapGrid = customCreation
? undefined
: countingGrid(gridSize, gridSize, boardPieces);
: getLetterCountPerSquare(gridSize, gridSize, boardPieces);

const [horizontalValidityGrid, verticalValidityGrid] = indicateValidity
? getWordValidityGrids({
Expand Down Expand Up @@ -160,7 +49,7 @@ export default function Board({
const draggedPieces = pieces.filter((piece) =>
dragPieceIDs.includes(piece.id),
);
const grid = countingGrid(gridSize, gridSize, draggedPieces);
const grid = getLetterCountPerSquare(gridSize, gridSize, draggedPieces);
dragShadow = (
<DragShadow
grid={grid}
Expand Down
10 changes: 6 additions & 4 deletions src/components/Pool.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import Piece from "./Piece";
import DragShadow from "./DragShadow";
import {countingGrid} from "./Board";
import {getLetterCountPerSquare} from "../logic/getLetterCountPerSquare";

export default function Pool({pieces, dragDestination, dispatchGameState}) {
const poolPieces = pieces.filter((piece) => piece.poolIndex >= 0);
Expand Down Expand Up @@ -29,9 +29,11 @@ export default function Pool({pieces, dragDestination, dispatchGameState}) {
<div className="pool-slot shadow" key={piece.id}>
<DragShadow
key={`shadow-piece-${piece.id}`}
grid={countingGrid(piece.letters.length, piece.letters[0].length, [
{...piece, dragGroupTop: 0, dragGroupLeft: 0},
])}
grid={getLetterCountPerSquare(
piece.letters.length,
piece.letters[0].length,
[{...piece, dragGroupTop: 0, dragGroupLeft: 0}],
)}
/>
</div>
)),
Expand Down
2 changes: 1 addition & 1 deletion src/logic/convertRepresentativeStringToGrid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {arrayToGrid} from "./arrayToGrid";
import {arrayToGrid} from "../common/arrayToGrid";
import {cipherLetter} from "./cipherLetter";

// Converts a string of letters and integers into a square grid of single letters and empty strings.
Expand Down
22 changes: 22 additions & 0 deletions src/logic/getLetterCountPerSquare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Returns a grid with the number of letters at each location in the grid
export function getLetterCountPerSquare(height, width, pieces) {
let grid = Array(height)
.fill(undefined)
.map(() => Array(width).fill(0));

for (let piece of pieces) {
const letters = piece.letters;
let top = piece.boardTop ?? piece.dragGroupTop;
for (let rowIndex = 0; rowIndex < letters.length; rowIndex++) {
let left = piece.boardLeft ?? piece.dragGroupLeft;
for (let colIndex = 0; colIndex < letters[rowIndex].length; colIndex++) {
if (letters[rowIndex][colIndex]) {
grid[top][left]++;
}
left++;
}
top++;
}
}
return grid;
}
90 changes: 90 additions & 0 deletions src/logic/getWordValidityGrids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {getGridFromPieces} from "../logic/getGridFromPieces";
import {getWordsFromPieces} from "../logic/getWordsFromPieces";
import {transposeGrid} from "@skedwards88/word_logic";
import {isKnown} from "@skedwards88/word_logic";
import {trie} from "../logic/trie";

function getHorizontalValidityGrid({grid, originalWords}) {
// return a 2D array of bools indicating whether
// the position corresponds to a letter on the board
// that is part of a valid horizontal word
const height = grid.length;
const width = grid[0].length;

const horizontalValidityGrid = Array(height)
.fill(undefined)
.map(() => Array(width).fill(false));

for (const [rowIndex, row] of grid.entries()) {
let word = "";
let indexes = [];
for (const [columnIndex, letter] of row.entries()) {
if (letter != "") {
word += letter;
indexes.push(columnIndex);
} else {
if (word.length > 1) {
// If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
// Otherwise, check whether it is a word in the trie.
let isWord = originalWords.includes(word);
if (!isWord) {
({isWord} = isKnown(word, trie));
}
if (isWord) {
indexes.forEach(
(index) => (horizontalValidityGrid[rowIndex][index] = true),
);
}
}
word = "";
indexes = [];
}
}
// Also end the word if we reach the end of the row
if (word.length > 1) {
// If the word is one of the original words, always consider it valid (in case we updated the dictionary in the interim).
// Otherwise, check whether it is a word in the trie.
let isWord = originalWords.includes(word);
if (!isWord) {
({isWord} = isKnown(word, trie));
}
if (isWord) {
indexes.forEach(
(index) => (horizontalValidityGrid[rowIndex][index] = true),
);
}
}
}

return horizontalValidityGrid;
}

export function getWordValidityGrids({
pieces,
gridSize,
includeOriginalSolution = true,
}) {
const originalWords = includeOriginalSolution
? getWordsFromPieces({
pieces,
gridSize,
solution: true,
})
: [];

const grid = getGridFromPieces({pieces, gridSize, solution: false});

const horizontalValidityGrid = getHorizontalValidityGrid({
grid,
originalWords,
});

const transposedGrid = transposeGrid(grid);
const horizontalTransposedValidityGrid = getHorizontalValidityGrid({
grid: transposedGrid,
originalWords,
});
const verticalValidityGrid = transposeGrid(horizontalTransposedValidityGrid);

return [horizontalValidityGrid, verticalValidityGrid];
}

0 comments on commit 11e0cd6

Please sign in to comment.