Skip to content

Commit

Permalink
initialiseCells accept object for params and include allowMissingSolu…
Browse files Browse the repository at this point in the history
…tions logic
  • Loading branch information
tblackwell-tm committed Mar 6, 2024
1 parent 817abbf commit 85d3385
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 88 deletions.
12 changes: 6 additions & 6 deletions src/components/Crossword/Crossword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ export default function Crossword({
React.useEffect(() => {
try {
// initialise cells
const initCells = initialiseCells(
data.dimensions.cols,
data.dimensions.rows,
data.entries,
loadGrid ?? guessGrid,
);
const initCells = initialiseCells({
cols: data.dimensions.cols,
rows: data.dimensions.rows,
entries: data.entries,
guessGrid: loadGrid ?? guessGrid,
});
dispatch(cellsActionUpdateGrid(initCells));

// initialise clues
Expand Down
10 changes: 5 additions & 5 deletions src/redux/cellsSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function getState() {
}

test('it updates grid', () => {
const cells = initialiseCells(
testData.dimensions.cols,
testData.dimensions.rows,
testData.entries,
);
const cells = initialiseCells({
cols: testData.dimensions.cols,
rows: testData.dimensions.rows,
entries: testData.entries,
});
store.dispatch(updateGrid(cells));
expect(getState()).toStrictEqual(cells);
});
Expand Down
10 changes: 5 additions & 5 deletions src/redux/cluesSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function getState() {
}

test('it updates grid', () => {
const cells = initialiseCells(
testData.dimensions.cols,
testData.dimensions.rows,
testData.entries,
);
const cells = initialiseCells({
cols: testData.dimensions.cols,
rows: testData.dimensions.rows,
entries: testData.entries,
});

const clues = initialiseClues(testData.entries, cells);

Expand Down
90 changes: 45 additions & 45 deletions src/utils/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,80 +95,80 @@ test('blankNeighbours without neighbours', () => {

test('initialiseCells with invalid data 1', () => {
expect(() =>
initialiseCells(
invalidData1.dimensions.cols,
invalidData1.dimensions.rows,
invalidData1.entries,
),
initialiseCells({
cols: invalidData1.dimensions.cols,
rows: invalidData1.dimensions.rows,
entries: invalidData1.entries,
}),
).toThrow('Crossword data error: solution length mismatch');
});

test('initialiseCells with invalid data 2', () => {
expect(() =>
initialiseCells(
invalidData2.dimensions.cols,
invalidData2.dimensions.rows,
invalidData2.entries,
),
initialiseCells({
cols: invalidData2.dimensions.cols,
rows: invalidData2.dimensions.rows,
entries: invalidData2.entries,
}),
).toThrow('Crossword data error: out of bounds');
});

test('initialiseCells with invalid data 3', () => {
expect(() =>
initialiseCells(
invalidData3.dimensions.cols,
invalidData3.dimensions.rows,
invalidData3.entries,
),
initialiseCells({
cols: invalidData3.dimensions.cols,
rows: invalidData3.dimensions.rows,
entries: invalidData3.entries,
}),
).toThrow('Crossword data error: solution character clash');
});

test('initialiseCells with invalid data 4', () => {
expect(() =>
initialiseCells(
invalidData4.dimensions.cols,
invalidData4.dimensions.rows,
invalidData4.entries,
),
initialiseCells({
cols: invalidData4.dimensions.cols,
rows: invalidData4.dimensions.rows,
entries: invalidData4.entries,
}),
).toThrow('Crossword data error: overlapping across solutions');
});

test('initialiseCells with invalid data 5', () => {
expect(() =>
initialiseCells(
invalidData5.dimensions.cols,
invalidData5.dimensions.rows,
invalidData5.entries,
),
initialiseCells({
cols: invalidData5.dimensions.cols,
rows: invalidData5.dimensions.rows,
entries: invalidData5.entries,
}),
).toThrow('Crossword data error: overlapping down solutions');
});

test('initialiseCells with invalid data 6', () => {
expect(() =>
initialiseCells(
invalidData6.dimensions.cols,
invalidData6.dimensions.rows,
invalidData6.entries,
),
initialiseCells({
cols: invalidData6.dimensions.cols,
rows: invalidData6.dimensions.rows,
entries: invalidData6.entries,
}),
).toThrow('Crossword data error: clue id missing from group');
});

test('initialiseCells with invalid data 7', () => {
expect(() =>
initialiseCells(
invalidData7.dimensions.cols,
invalidData7.dimensions.rows,
invalidData7.entries,
),
initialiseCells({
cols: invalidData7.dimensions.cols,
rows: invalidData7.dimensions.rows,
entries: invalidData7.entries,
}),
).toThrow('Crossword data error: group clue id not found');
});

test('initialiseCells with valid data', () => {
const cells = initialiseCells(
validData.dimensions.cols,
validData.dimensions.rows,
validData.entries,
);
const cells = initialiseCells({
cols: validData.dimensions.cols,
rows: validData.dimensions.rows,
entries: validData.entries,
});

expect(cells.length).toBe(23);
expect(cells).toEqual([
Expand Down Expand Up @@ -366,12 +366,12 @@ test('initialiseCells with valid data and guess grid', () => {
validData.dimensions.rows,
gridChar,
);
const cells = initialiseCells(
validData.dimensions.cols,
validData.dimensions.rows,
validData.entries,
const cells = initialiseCells({
cols: validData.dimensions.cols,
rows: validData.dimensions.rows,
entries: validData.entries,
guessGrid,
);
});

expect(cells.length).toBe(23);
expect(cells).toEqual([
Expand Down
28 changes: 21 additions & 7 deletions src/utils/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ export function blankNeighbours(
* @param entries
* @returns
*/
export function initialiseCells(
cols: number,
rows: number,
entries: GuardianClue[],
guessGrid?: GuessGrid,
) {
export function initialiseCells({
cols,
rows,
entries,
guessGrid,
allowMissingSolutions = false,
}: {
cols: number;
rows: number;
entries: GuardianClue[];
guessGrid?: GuessGrid;
allowMissingSolutions?: boolean;
}) {
const cells: Cell[] = [];
const entryIds = entries.map((entry) => entry.id);

Expand All @@ -65,6 +72,7 @@ export function initialiseCells(
if (col < 0 || col >= cols || row < 0 || row >= rows) {
throw new Error('Crossword data error: out of bounds');
} else if (
!allowMissingSolutions &&
entry.solution !== undefined &&
entry.length !== entry.solution.length
) {
Expand Down Expand Up @@ -96,7 +104,7 @@ export function initialiseCells(
cells.push(newCell);
} else {
// merge cell
if (currentCell.val !== entry.solution?.[i]) {
if (!allowMissingSolutions && currentCell.val !== entry.solution?.[i]) {
throw new Error('Crossword data error: solution character clash');
} else if (
across &&
Expand All @@ -111,6 +119,12 @@ export function initialiseCells(
} else {
currentCell.num = i === 0 ? entry.number : currentCell.num;
currentCell.clueIds = [...currentCell.clueIds, entry.id];

// overwrite with new value
const newChar = entry.solution?.[i];
if (newChar !== undefined) {
currentCell.val = newChar as Char;
}
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/utils/clue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ describe('getCrossingClueIds', () => {

describe('initialiseClues', () => {
test('clues get initialised', () => {
const cells = initialiseCells(
testData.dimensions.cols,
testData.dimensions.rows,
testData.entries,
);
const cells = initialiseCells({
cols: testData.dimensions.cols,
rows: testData.dimensions.rows,
entries: testData.entries,
});

const clues = initialiseClues(testData.entries, cells);
expect(clues).toEqual([
Expand Down Expand Up @@ -234,11 +234,11 @@ describe('initialiseClues', () => {
});

test('clues get initialised with selected clue', () => {
const cells = initialiseCells(
testData.dimensions.cols,
testData.dimensions.rows,
testData.entries,
);
const cells = initialiseCells({
cols: testData.dimensions.cols,
rows: testData.dimensions.rows,
entries: testData.entries,
});

const clues = initialiseClues(testData.entries, cells, '1-across');
expect(clues).toEqual(
Expand All @@ -262,11 +262,11 @@ describe('initialiseClues', () => {
});

test('clues get initialised with answered clues', () => {
const cells = initialiseCells(
testData.dimensions.cols,
testData.dimensions.rows,
testData.entries,
);
const cells = initialiseCells({
cols: testData.dimensions.cols,
rows: testData.dimensions.rows,
entries: testData.entries,
});

// reveal all cells
store.dispatch(cellsUpdateGrid(cells));
Expand Down
10 changes: 5 additions & 5 deletions src/utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export function initialiseStore(
data: GuardianCrossword,
guessGrid?: GuessGrid,
) {
const cells = initialiseCells(
data.dimensions.cols,
data.dimensions.rows,
data.entries,
const cells = initialiseCells({
cols: data.dimensions.cols,
rows: data.dimensions.rows,
entries: data.entries,
guessGrid,
);
});

const clues = initialiseClues(data.entries, cells);

Expand Down

0 comments on commit 85d3385

Please sign in to comment.