Skip to content

Commit

Permalink
add option to play custom game
Browse files Browse the repository at this point in the history
  • Loading branch information
skedwards88 committed Aug 24, 2024
1 parent a1264f4 commit 327ff4b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
98 changes: 65 additions & 33 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ export default function App() {
// todo consolidate lastVisited and setLastOpened?
const [, setLastOpened] = React.useState(Date.now());

function handleCustomGeneration() {
// If there is nothing to share, display a message with errors
if (!customState.pieces.some((piece) => piece.boardTop >= 0)) {
throw new Error("Add some letters to the board first!");
}

// Validate the grid
// - The UI restricts the grid size, so don't need to validate that
// - Make sure all letters are connected
// - Make sure all horizontal and vertical words are known
const grid = getGridFromPieces({
pieces: customState.pieces,
gridSize: customState.gridSize,
solution: false,
});

const {gameIsSolved, reason} = crosswordValidQ({
grid: grid,
trie: trie,
});

// If the board is not valid, display a message with errors
if (!gameIsSolved) {
throw new Error(reason);
}

// Center and resize/pad the grid
// Convert the grid to a representative string
const resizedGrid = resizeGrid(grid);
const cipherShift = pickRandomIntBetween(5, 9);
const representativeString = convertGridToRepresentativeString(
resizedGrid,
cipherShift,
);

return representativeString;
}

function handleVisibilityChange() {
// If the visibility of the app changes to become visible,
// update the state to force the app to re-render.
Expand Down Expand Up @@ -209,56 +247,47 @@ export default function App() {
return (
<div className="App" id="crossjig">
<div id="controls">
<button id="exitCustomButton" onClick={() => setDisplay("game")}>
Cancel
</button>
<button
id="shareCustomButton"
id="playCustomButton"
onClick={() => {
// If there is nothing to share, display a message with errors
if (!customState.pieces.some((piece) => piece.boardTop >= 0)) {
let representativeString;
try {
representativeString = handleCustomGeneration();
} catch (error) {
const invalidReason = error.message;
dispatchCustomState({
action: "updateInvalidReason",
invalidReason: "Add some letters to the board first!",
invalidReason: invalidReason,
});
setDisplay("customError");
return;
}

// Validate the grid
// - The UI restricts the grid size, so don't need to validate that
// - Make sure all letters are connected
// - Make sure all horizontal and vertical words are known
const grid = getGridFromPieces({
pieces: customState.pieces,
gridSize: customState.gridSize,
solution: false,
dispatchGameState({
action: "playCustom",
representativeString,
});

const {gameIsSolved, reason} = crosswordValidQ({
grid: grid,
trie: trie,
});

// If the board is not valid, display a message with errors
if (!gameIsSolved) {
setDisplay("game");
}}
>
Play
</button>
<button
id="shareCustomButton"
onClick={() => {
let representativeString;
try {
representativeString = handleCustomGeneration();
} catch (error) {
const invalidReason = error.message;
dispatchCustomState({
action: "updateInvalidReason",
invalidReason: reason,
invalidReason: invalidReason,
});
setDisplay("customError");
return;
}

// Center and resize/pad the grid
// Convert the grid to a representative string
const resizedGrid = resizeGrid(grid);
const cipherShift = pickRandomIntBetween(5, 9);
const representativeString = convertGridToRepresentativeString(
resizedGrid,
cipherShift,
);

// Share (or show the link if sharing is not supported)
if (navigator.canShare) {
handleShare({
Expand All @@ -278,6 +307,9 @@ export default function App() {
>
Share
</button>
<button id="exitCustomButton" onClick={() => setDisplay("game")}>
Cancel
</button>
</div>
<CustomCreation
dispatchCustomState={dispatchCustomState}
Expand Down
7 changes: 6 additions & 1 deletion src/logic/gameReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ export function gameReducer(currentGameState, payload) {
useSaved: false,
isCustom: false,
});
} else if (payload.action === "playCustom") {
return gameInit({
seed: payload.representativeString,
isCustom: true,
});
} else if (payload.action === "changeValidityOpacity") {
return {
...currentGameState,
Expand Down Expand Up @@ -657,7 +662,7 @@ export function gameReducer(currentGameState, payload) {
stats: newStats,
};
}
} else if (payload.action === "updateInvalidReason") {
} else if (payload.action === "updateInvalidReason") {
return {
...currentGameState,
invalidReason: payload.invalidReason,
Expand Down

0 comments on commit 327ff4b

Please sign in to comment.