Skip to content

Commit

Permalink
use transposeGrid function
Browse files Browse the repository at this point in the history
  • Loading branch information
skedwards88 committed Nov 7, 2023
1 parent f9a68cb commit 7775141
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 32 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "",
"dependencies": {
"@skedwards88/word_lists": "latest",
"@skedwards88/word_logic": "2.0.4",
"@skedwards88/word_logic": "2.0.7",
"lodash.clonedeep": "^4.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
9 changes: 3 additions & 6 deletions src/logic/centerGrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cloneDeep from "lodash.clonedeep";
import { getMaxShifts } from "./getMaxShifts";
import { transposeGrid } from "@skedwards88/word_logic";

export function centerGrid(grid) {
let shiftedGrid = cloneDeep(grid);
Expand Down Expand Up @@ -29,9 +30,7 @@ export function centerGrid(grid) {
];

// transpose
shiftedGrid = shiftedGrid.map((_, index) =>
shiftedGrid.map((row) => row[index])
);
shiftedGrid = transposeGrid(shiftedGrid);

// trim the empty rows, then pad with empty rows to center
const cutLeftRight = shiftedGrid.slice(
Expand All @@ -45,9 +44,7 @@ export function centerGrid(grid) {
];

// un transpose
shiftedGrid = shiftedGrid.map((_, index) =>
shiftedGrid.map((row) => row[index])
);
shiftedGrid = transposeGrid(shiftedGrid);

return shiftedGrid;
}
4 changes: 2 additions & 2 deletions src/logic/generateGrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getPatternsForRow from "./getRegexForRow.js";
import { shuffleArray } from "@skedwards88/word_logic";
import { shuffleArray, transposeGrid } from "@skedwards88/word_logic";
import {
commonWordsLen4,
commonWordsLen5,
Expand Down Expand Up @@ -77,7 +77,7 @@ export function generateGrid({ gridSize, minLetters, pseudoRandomGenerator }) {
//
while (letterCount < minLetters && count < 20) {
// transpose the grid to start searching in the opposite orientation
grid = grid.map((_, index) => grid.map((row) => row[index]));
grid = transposeGrid(grid);
orientationIsRows = !orientationIsRows;

// to keep the puzzle spread out and to be more likely to find a match,
Expand Down
4 changes: 3 additions & 1 deletion src/logic/getMaxShifts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { transposeGrid } from "@skedwards88/word_logic";

export function getMaxShifts(grid) {
const transposedGrid = grid.map((_, index) => grid.map((row) => row[index]));
const transposedGrid = transposeGrid(grid);

let maxShiftUp = 0;
for (let index = 0; index < grid.length; index++) {
Expand Down
18 changes: 3 additions & 15 deletions src/logic/getWordsFromGrid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { transposeGrid } from "@skedwards88/word_logic";

function getHorizontalWordsFromGrid(grid) {
let words = [];

Expand All @@ -23,21 +25,7 @@ function getHorizontalWordsFromGrid(grid) {
}

export function getWordsFromGrid(grid) {
// The simple method of grid transposition used in this function
// relies on an equal number of rows and columns,
// so throw errors if that isn't the case
if (grid.length != grid[0]?.length) {
throw new Error(
`The number of columns and number of rows in the grid must be equal.`
);
}

const numColumnsPerRow = grid.map((row) => row.length);
if (Math.min(...numColumnsPerRow) != Math.max(...numColumnsPerRow)) {
throw new Error(`All of the rows in the grid must have the same length.`);
}

const transposedGrid = grid.map((_, index) => grid.map((row) => row[index]));
const transposedGrid = transposeGrid(grid);

return [
...getHorizontalWordsFromGrid(grid),
Expand Down

0 comments on commit 7775141

Please sign in to comment.