Skip to content

Commit affa409

Browse files
author
skedwards88
committed
update functions to act on solution vs actual
1 parent 169f8ab commit affa409

File tree

4 files changed

+300
-20
lines changed

4 files changed

+300
-20
lines changed

src/logic/gameSolvedQ.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function gameSolvedQ(pieces, gridSize) {
4949
};
5050
}
5151

52-
const originalWords = getWordsFromPieces({ pieces, gridSize });
52+
const originalWords = getWordsFromPieces({ pieces, gridSize, solution:true });
5353

5454
const { gameIsSolved, reason } = crosswordValidQ({
5555
grid: grid,

src/logic/getGridFromPieces.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export function getSolutionFromPieces({ pieces, gridSize }) {
1+
export function getGridFromPieces({ pieces, gridSize, solution }) {
2+
// Compiles a 2D array representing the letter locations on the board
3+
// If solution is true, uses the solutionTop/solutionLeft value of each piece
4+
// otherwise, uses the boardTop/boardLeft value
25
if (pieces === undefined) {
36
throw new Error("Pieces must be defined.");
47
}
@@ -11,11 +14,17 @@ export function getSolutionFromPieces({ pieces, gridSize }) {
1114
Array.from({ length: gridSize }, () => "")
1215
);
1316

17+
const topKey = solution ? "solutionTop" : "boardTop";
18+
const leftKey = solution ? "solutionLeft" : "boardLeft";
19+
1420
for (const piece of pieces) {
21+
if (!solution && (piece[topKey] == undefined || piece[leftKey] == undefined)) {
22+
continue;
23+
}
1524
const letters = piece.letters;
16-
let top = piece.solutionTop;
25+
let top = piece[topKey];
1726
for (let rowIndex = 0; rowIndex < letters.length; rowIndex++) {
18-
let left = piece.solutionLeft;
27+
let left = piece[leftKey];
1928
for (let colIndex = 0; colIndex < letters[rowIndex].length; colIndex++) {
2029
if (letters[rowIndex][colIndex]) {
2130
if (grid[top][left] == undefined) {
@@ -28,6 +37,5 @@ export function getSolutionFromPieces({ pieces, gridSize }) {
2837
top += 1;
2938
}
3039
}
31-
3240
return grid;
3341
}

src/logic/getSolutionFromPieces.test.js

Lines changed: 284 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getSolutionFromPieces } from "./getSolutionFromPieces";
1+
import { getGridFromPieces } from "./getGridFromPieces";
22

3-
describe("getSolutionFromPieces", () => {
4-
test("it returns a 2D array representing the placement of the pieces in a grid", () => {
3+
describe("getGridFromPieces", () => {
4+
test("if `solution` is true, it returns a 2D array representing the placement of the pieces in a grid based on the solution", () => {
55
const pieces = [
66
{
77
letters: [
@@ -112,11 +112,243 @@ describe("getSolutionFromPieces", () => {
112112
["", "", "", "", "E", "", "E", "A", "R", "N", "S", ""],
113113
["V", "I", "S", "O", "R", "", "", "", "", "", "", ""],
114114
];
115-
expect(getSolutionFromPieces({ pieces, gridSize: 12 })).toEqual(
115+
expect(getGridFromPieces({ pieces, gridSize: 12, solution: true })).toEqual(
116116
expectedGrid
117117
);
118118
});
119119

120+
test("if `solution` is false, it returns a 2D array representing the placement of the pieces in a grid", () => {
121+
const pieces = [
122+
{
123+
letters: [
124+
["", "C", ""],
125+
["L", "A", "Y"],
126+
["", "R", ""],
127+
],
128+
boardTop: 5,
129+
boardLeft: 7,
130+
},
131+
{
132+
letters: [["K"], ["N"], ["O"]],
133+
boardTop: 2,
134+
boardLeft: 10,
135+
},
136+
{ letters: [["N", "S"]], boardTop: 10, boardLeft: 9 },
137+
{ letters: [["E", "A"]], boardTop: 10, boardLeft: 6 },
138+
{ letters: [["B"], ["S"]], boardTop: 5, boardLeft: 10 },
139+
{
140+
letters: [
141+
["I", "", ""],
142+
["V", "", ""],
143+
["E", "Z", "E"],
144+
],
145+
boardTop: 3,
146+
boardLeft: 3,
147+
},
148+
{ letters: [["A"], ["W"]], boardTop: 3, boardLeft: 5 },
149+
{
150+
letters: [
151+
["S", "P", "A"],
152+
["", "R", ""],
153+
["", "E", ""],
154+
],
155+
boardTop: 0,
156+
boardLeft: 0,
157+
},
158+
{
159+
letters: [
160+
["", "", "E"],
161+
["S", "O", "R"],
162+
],
163+
boardTop: 10,
164+
boardLeft: 2,
165+
},
166+
{
167+
letters: [
168+
["", "F", ""],
169+
["", "E", ""],
170+
["B", "R", "E"],
171+
],
172+
boardTop: 3,
173+
boardLeft: 0,
174+
},
175+
{ letters: [["V", "I"]], boardTop: 11, boardLeft: 0 },
176+
{
177+
letters: [
178+
["N", "S"],
179+
["A", ""],
180+
["T", "E"],
181+
],
182+
boardTop: 0,
183+
boardLeft: 3,
184+
},
185+
{
186+
letters: [
187+
["", "N", ""],
188+
["S", "I", "M"],
189+
["", "C", ""],
190+
],
191+
boardTop: 7,
192+
boardLeft: 3,
193+
},
194+
{
195+
letters: [
196+
["", "", "R"],
197+
["", "", "A"],
198+
["L", "L", "S"],
199+
],
200+
boardTop: 0,
201+
boardLeft: 5,
202+
},
203+
{ letters: [["F"]], boardTop: 1, boardLeft: 5 },
204+
{ letters: [["P"], ["Y"]], boardTop: 3, boardLeft: 7 },
205+
{ letters: [["D", "E"]], boardTop: 6, boardLeft: 5 },
206+
{
207+
letters: [
208+
["P", "L", "E"],
209+
["", "", "E"],
210+
["", "", "R"],
211+
],
212+
boardTop: 8,
213+
boardLeft: 6,
214+
},
215+
];
216+
217+
const expectedGrid = [
218+
["S", "P", "A", "N", "S", "", "", "R", "", "", "", ""],
219+
["", "R", "", "A", "", "F", "", "A", "", "", "", ""],
220+
["", "E", "", "T", "E", "L", "L", "S", "", "", "K", ""],
221+
["", "F", "", "I", "", "A", "", "P", "", "", "N", ""],
222+
["", "E", "", "V", "", "W", "", "Y", "", "", "O", ""],
223+
["B", "R", "E", "E", "Z", "E", "", "", "C", "", "B", ""],
224+
["", "", "", "", "", "D", "E", "L", "A", "Y", "S", ""],
225+
["", "", "", "", "N", "", "", "", "R", "", "", ""],
226+
["", "", "", "S", "I", "M", "P", "L", "E", "", "", ""],
227+
["", "", "", "", "C", "", "", "", "E", "", "", ""],
228+
["", "", "", "", "E", "", "E", "A", "R", "N", "S", ""],
229+
["V", "I", "S", "O", "R", "", "", "", "", "", "", ""],
230+
];
231+
expect(
232+
getGridFromPieces({ pieces, gridSize: 12, solution: false })
233+
).toEqual(expectedGrid);
234+
});
235+
236+
test("if `solution` is false, any pieces that are not on the board are excluded", () => {
237+
const pieces = [
238+
{
239+
letters: [
240+
["", "C", ""],
241+
["L", "A", "Y"],
242+
["", "R", ""],
243+
],
244+
boardTop: 5,
245+
boardLeft: 7,
246+
},
247+
{
248+
letters: [["K"], ["N"], ["O"]],
249+
boardTop: 2,
250+
boardLeft: 10,
251+
},
252+
{ letters: [["N", "S"]], boardTop: 10, boardLeft: 9 },
253+
{ letters: [["E", "A"]], boardTop: 10, boardLeft: 6 },
254+
{ letters: [["B"], ["S"]], boardTop: 5, boardLeft: 10 },
255+
{
256+
letters: [
257+
["I", "", ""],
258+
["V", "", ""],
259+
["E", "Z", "E"],
260+
],
261+
boardTop: undefined,
262+
boardLeft: undefined,
263+
},
264+
{ letters: [["A"], ["W"]], boardTop: 3, boardLeft: 5 },
265+
{
266+
letters: [
267+
["S", "P", "A"],
268+
["", "R", ""],
269+
["", "E", ""],
270+
],
271+
boardTop: 0,
272+
boardLeft: 0,
273+
},
274+
{
275+
letters: [
276+
["", "", "E"],
277+
["S", "O", "R"],
278+
],
279+
boardTop: 10,
280+
boardLeft: 2,
281+
},
282+
{
283+
letters: [
284+
["", "F", ""],
285+
["", "E", ""],
286+
["B", "R", "E"],
287+
],
288+
boardTop: 3,
289+
boardLeft: 0,
290+
},
291+
{ letters: [["V", "I"]], boardTop: 11, boardLeft: 0 },
292+
{
293+
letters: [
294+
["N", "S"],
295+
["A", ""],
296+
["T", "E"],
297+
],
298+
boardTop: 0,
299+
boardLeft: 3,
300+
},
301+
{
302+
letters: [
303+
["", "N", ""],
304+
["S", "I", "M"],
305+
["", "C", ""],
306+
],
307+
boardTop: 7,
308+
boardLeft: 3,
309+
},
310+
{
311+
letters: [
312+
["", "", "R"],
313+
["", "", "A"],
314+
["L", "L", "S"],
315+
],
316+
boardTop: 0,
317+
boardLeft: 5,
318+
},
319+
{ letters: [["F"]], boardTop: 1, boardLeft: 5 },
320+
{ letters: [["P"], ["Y"]], boardTop: 3, boardLeft: 7 },
321+
{ letters: [["D", "E"]], boardTop: 6, boardLeft: 5 },
322+
{
323+
letters: [
324+
["P", "L", "E"],
325+
["", "", "E"],
326+
["", "", "R"],
327+
],
328+
boardTop: undefined,
329+
boardLeft: undefined,
330+
},
331+
];
332+
333+
const expectedGrid = [
334+
["S", "P", "A", "N", "S", "", "", "R", "", "", "", ""],
335+
["", "R", "", "A", "", "F", "", "A", "", "", "", ""],
336+
["", "E", "", "T", "E", "L", "L", "S", "", "", "K", ""],
337+
["", "F", "", "", "", "A", "", "P", "", "", "N", ""],
338+
["", "E", "", "", "", "W", "", "Y", "", "", "O", ""],
339+
["B", "R", "E", "", "", "", "", "", "C", "", "B", ""],
340+
["", "", "", "", "", "D", "E", "L", "A", "Y", "S", ""],
341+
["", "", "", "", "N", "", "", "", "R", "", "", ""],
342+
["", "", "", "S", "I", "M", "", "", "", "", "", ""],
343+
["", "", "", "", "C", "", "", "", "", "", "", ""],
344+
["", "", "", "", "E", "", "E", "A", "", "N", "S", ""],
345+
["V", "I", "S", "O", "R", "", "", "", "", "", "", ""],
346+
];
347+
expect(
348+
getGridFromPieces({ pieces, gridSize: 12, solution: false })
349+
).toEqual(expectedGrid);
350+
});
351+
120352
test("any overlapping positions are overwritten", () => {
121353
const pieces = [
122354
{
@@ -135,7 +367,7 @@ describe("getSolutionFromPieces", () => {
135367
["L", "A", "Y"],
136368
["", "R", ""],
137369
];
138-
expect(getSolutionFromPieces({ pieces, gridSize: 3 })).toEqual(
370+
expect(getGridFromPieces({ pieces, gridSize: 3, solution: true })).toEqual(
139371
expectedGrid
140372
);
141373
});
@@ -147,7 +379,7 @@ describe("getSolutionFromPieces", () => {
147379
["", "", ""],
148380
["", "", ""],
149381
];
150-
expect(getSolutionFromPieces({ pieces, gridSize: 3 })).toEqual(
382+
expect(getGridFromPieces({ pieces, gridSize: 3, solution: true })).toEqual(
151383
expectedGrid
152384
);
153385
});
@@ -166,18 +398,58 @@ describe("getSolutionFromPieces", () => {
166398
{ letters: [["", "T", "O"]], solutionTop: 0, solutionLeft: 0 },
167399
];
168400

169-
expect(() => getSolutionFromPieces({ pieces, gridSize: 2 })).toThrow(
170-
"A piece falls outside of the grid boundary."
171-
);
401+
expect(() =>
402+
getGridFromPieces({ pieces, gridSize: 2, solution: true })
403+
).toThrow("A piece falls outside of the grid boundary.");
172404
});
173405

174-
test("an error is thrown if either input is undefined", () => {
175-
expect(() => getSolutionFromPieces({ gridSize: 2 })).toThrow(
406+
test("an error is thrown if either pieces or gridSize is undefined", () => {
407+
expect(() => getGridFromPieces({ gridSize: 2 })).toThrow(
176408
"Pieces must be defined."
177409
);
178410

179-
expect(() => getSolutionFromPieces({ pieces: [] })).toThrow(
411+
expect(() => getGridFromPieces({ pieces: [] })).toThrow(
180412
"Grid size must be defined."
181413
);
182414
});
183415
});
416+
417+
test("if `solution` is false, and no pieces are on the board, an empty grid is returned", () => {
418+
const pieces = [
419+
{
420+
letters: [
421+
["", "C", ""],
422+
["L", "A", "Y"],
423+
["", "R", ""],
424+
],
425+
boardTop: undefined,
426+
boardLeft: undefined,
427+
},
428+
{
429+
letters: [["K"], ["N"], ["O"]],
430+
boardTop: undefined,
431+
boardLeft: undefined,
432+
},
433+
{ letters: [["N", "S"]], boardTop: undefined, boardLeft: undefined },
434+
{ letters: [["E", "A"]], boardTop: undefined, boardLeft: undefined },
435+
{ letters: [["B"], ["S"]], boardTop: undefined, boardLeft: undefined },
436+
];
437+
438+
const expectedGrid = [
439+
["", "", "", "", "", "", "", "", "", "", "", ""],
440+
["", "", "", "", "", "", "", "", "", "", "", ""],
441+
["", "", "", "", "", "", "", "", "", "", "", ""],
442+
["", "", "", "", "", "", "", "", "", "", "", ""],
443+
["", "", "", "", "", "", "", "", "", "", "", ""],
444+
["", "", "", "", "", "", "", "", "", "", "", ""],
445+
["", "", "", "", "", "", "", "", "", "", "", ""],
446+
["", "", "", "", "", "", "", "", "", "", "", ""],
447+
["", "", "", "", "", "", "", "", "", "", "", ""],
448+
["", "", "", "", "", "", "", "", "", "", "", ""],
449+
["", "", "", "", "", "", "", "", "", "", "", ""],
450+
["", "", "", "", "", "", "", "", "", "", "", ""],
451+
];
452+
expect(getGridFromPieces({ pieces, gridSize: 12, solution: false })).toEqual(
453+
expectedGrid
454+
);
455+
});

src/logic/getWordsFromPieces.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getSolutionFromPieces } from "./getSolutionFromPieces";
1+
import { getGridFromPieces } from "./getGridFromPieces";
22
import { getWordsFromGrid } from "./getWordsFromGrid";
33

4-
export function getWordsFromPieces({ pieces, gridSize }) {
5-
const grid = getSolutionFromPieces({ pieces, gridSize });
4+
export function getWordsFromPieces({ pieces, gridSize, solution }) {
5+
const grid = getGridFromPieces({ pieces, gridSize, solution });
66

77
const words = getWordsFromGrid(grid);
88

0 commit comments

Comments
 (0)