From 7775141e3c1cbfa12a3de1ce6a90c9e3e6cbe720 Mon Sep 17 00:00:00 2001 From: skedwards88 Date: Tue, 7 Nov 2023 08:21:25 -0800 Subject: [PATCH] use transposeGrid function --- package-lock.json | 14 +++++++------- package.json | 2 +- src/logic/centerGrid.js | 9 +++------ src/logic/generateGrid.js | 4 ++-- src/logic/getMaxShifts.js | 4 +++- src/logic/getWordsFromGrid.js | 18 +++--------------- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0977919..c105b4b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.0.29", "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", @@ -3100,9 +3100,9 @@ "integrity": "sha512-mrOLSLsPwQmH+gnpRPALwvu0e3QZ/VdN1PlmLPfGAZbUgMu8V2TszyT9nMm5fKnj7J7GbtKlZ6Nb1eJ/LHhJ2g==" }, "node_modules/@skedwards88/word_logic": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/@skedwards88/word_logic/-/word_logic-2.0.4.tgz", - "integrity": "sha512-I1w7AQv6ivvW2sE6jcbAdos2FCfWOUqjlR7G8lvZN7QbZcN0VjFB51dVkumvK+anCJpqU35tRDTJEc31xj71Cg==", + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@skedwards88/word_logic/-/word_logic-2.0.7.tgz", + "integrity": "sha512-MYF9ohnl1IH2JCJZaNpz+A/Cdk1NGg2i849Ea9EDEArJAUoTFWGtq6tDlRWjQJ3wN/IQnWOOrw6NP5M7Gfy1Mw==", "dependencies": { "seedrandom": "^3.0.5" } @@ -16360,9 +16360,9 @@ "integrity": "sha512-mrOLSLsPwQmH+gnpRPALwvu0e3QZ/VdN1PlmLPfGAZbUgMu8V2TszyT9nMm5fKnj7J7GbtKlZ6Nb1eJ/LHhJ2g==" }, "@skedwards88/word_logic": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/@skedwards88/word_logic/-/word_logic-2.0.4.tgz", - "integrity": "sha512-I1w7AQv6ivvW2sE6jcbAdos2FCfWOUqjlR7G8lvZN7QbZcN0VjFB51dVkumvK+anCJpqU35tRDTJEc31xj71Cg==", + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@skedwards88/word_logic/-/word_logic-2.0.7.tgz", + "integrity": "sha512-MYF9ohnl1IH2JCJZaNpz+A/Cdk1NGg2i849Ea9EDEArJAUoTFWGtq6tDlRWjQJ3wN/IQnWOOrw6NP5M7Gfy1Mw==", "requires": { "seedrandom": "^3.0.5" } diff --git a/package.json b/package.json index 0de17cd..38f5077 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/logic/centerGrid.js b/src/logic/centerGrid.js index 542f531..8ff0f8c 100644 --- a/src/logic/centerGrid.js +++ b/src/logic/centerGrid.js @@ -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); @@ -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( @@ -45,9 +44,7 @@ export function centerGrid(grid) { ]; // un transpose - shiftedGrid = shiftedGrid.map((_, index) => - shiftedGrid.map((row) => row[index]) - ); + shiftedGrid = transposeGrid(shiftedGrid); return shiftedGrid; } diff --git a/src/logic/generateGrid.js b/src/logic/generateGrid.js index 128e113..c8e6a70 100644 --- a/src/logic/generateGrid.js +++ b/src/logic/generateGrid.js @@ -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, @@ -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, diff --git a/src/logic/getMaxShifts.js b/src/logic/getMaxShifts.js index 6401d11..85c6b85 100644 --- a/src/logic/getMaxShifts.js +++ b/src/logic/getMaxShifts.js @@ -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++) { diff --git a/src/logic/getWordsFromGrid.js b/src/logic/getWordsFromGrid.js index ae789ad..c61a75b 100644 --- a/src/logic/getWordsFromGrid.js +++ b/src/logic/getWordsFromGrid.js @@ -1,3 +1,5 @@ +import { transposeGrid } from "@skedwards88/word_logic"; + function getHorizontalWordsFromGrid(grid) { let words = []; @@ -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),