From c3f76e8bf67633bb7eae695cf1677eeb4483e2a1 Mon Sep 17 00:00:00 2001 From: skedwards88 Date: Sun, 7 Apr 2024 09:51:54 -0700 Subject: [PATCH] add hints --- src/components/Game.js | 2 ++ src/components/Shape.js | 30 ++++++++++++++++++++++++------ src/logic/gameReducer.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/components/Game.js b/src/components/Game.js index bf4b860..2ab74e8 100644 --- a/src/components/Game.js +++ b/src/components/Game.js @@ -68,6 +68,8 @@ function Game({dispatchGameState, gameState, isDaily}) { gridSize={Math.sqrt(gameState.letters.length)} letters={gameState.letters} key={shape.join("-")} + dispatchGameState={dispatchGameState} + shapeIndex={index} > ))} diff --git a/src/components/Shape.js b/src/components/Shape.js index 906198d..451b8f9 100644 --- a/src/components/Shape.js +++ b/src/components/Shape.js @@ -1,7 +1,7 @@ import React from "react"; import {indexesToWord} from "../logic/indexesToWord"; -function ShapeBox({filled, solved}) { +function ShapeBox({filled, solved, dispatchGameState, shapeIndex}) { let className = "shapeBox"; if (filled) { className += " filled"; @@ -10,10 +10,28 @@ function ShapeBox({filled, solved}) { } } - return
; + return ( +
{ + dispatchGameState({action: "hint", shapeIndex}); + } + : undefined + } + >
+ ); } -export function Shape({shape, foundSolution, gridSize, letters}) { +export function Shape({ + shape, + foundSolution, + gridSize, + letters, + dispatchGameState, + shapeIndex, +}) { const shapeIsSolved = foundSolution.every((i) => i != undefined); const emptyGrid = Array(gridSize * gridSize).fill(); @@ -22,6 +40,8 @@ export function Shape({shape, foundSolution, gridSize, letters}) { filled={shape.includes(index)} solved={shapeIsSolved} key={index} + dispatchGameState={dispatchGameState} + shapeIndex={shapeIndex} > )); @@ -29,9 +49,7 @@ export function Shape({shape, foundSolution, gridSize, letters}) { return (
-
- {boxes} -
+
{boxes}
{word}
); diff --git a/src/logic/gameReducer.js b/src/logic/gameReducer.js index fc0df12..efc02ca 100644 --- a/src/logic/gameReducer.js +++ b/src/logic/gameReducer.js @@ -134,6 +134,34 @@ export function gameReducer(currentGameState, payload) { foundSolutions: newFoundSolutions, result: "", }; + } else if (payload.action === "hint") { + console.log("hi") + // A hint reveals one letter at a time (in order) of the official solution + const actualSolution = + currentGameState.officialSolutions[payload.shapeIndex]; + let newFoundSolutions = cloneDeep(currentGameState.foundSolutions); + let hintedSolution = newFoundSolutions[payload.shapeIndex]; + + // Since hints may have been given previously, get the next unrevealed index for the hint + const nextHintIndex = hintedSolution.findIndex( + (i) => i === null || i === undefined + ); + + // If all hints have been given for the shape, return + if (nextHintIndex < 0) { + return { + ...currentGameState, + }; + } + + // Otherwise, reveal the next letter + const hintValue = actualSolution[nextHintIndex]; + newFoundSolutions[payload.shapeIndex][nextHintIndex] = hintValue; + + return { + ...currentGameState, + foundSolutions: newFoundSolutions, + }; } else if (payload.action === "todo handle other cases") { return { ...currentGameState,