diff --git a/package.json b/package.json index cbdc1ebd..e3d088a1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hurdle", "version": "0.1.0", "private": true, - "homepage": "http://iancanderson.github.io/hurdle", + "homepage": "http://hurdle.iancanderson.com", "dependencies": { "@headlessui/react": "^1.4.2", "@testing-library/jest-dom": "^5.16.1", diff --git a/src/App.tsx b/src/App.tsx index d798df2b..0604496e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -33,8 +33,8 @@ import { ReactComponent as Settings } from './data/Settings.svg' type State = { answer: (d: Difficulty) => Answer gameState: PlayState - board: Row[] - cellStatuses: CellStatus[][] + board: (d: Difficulty) => Row[] + cellStatuses: (d: Difficulty) => CellStatus[][] currentRowNum: number currentCol: Column charStatuses: () => { [key: string]: CellStatus } @@ -68,8 +68,9 @@ function App() { const initialStates: State = { answer: (d: Difficulty) => getRandomAnswer(d), gameState: PlayState.Playing, - board: Array.from({ length: rowCount }, () => ({})), - cellStatuses: Array(rowCount).fill(Array(columns.length).fill(CellStatus.Unguessed)), + board: (d: Difficulty) => Array.from({ length: rowCount(d) }, () => ({})), + cellStatuses: (d: Difficulty) => + Array(rowCount(d)).fill(Array(columns.length).fill(CellStatus.Unguessed)), currentRowNum: 0, currentCol: 0, charStatuses: () => { @@ -86,11 +87,6 @@ function App() { } const [gameState, setGameState] = useLocalStorage('stateGameState', initialStates.gameState) - const [board, setBoard] = useLocalStorage('stateBoard', initialStates.board) - const [cellStatuses, setCellStatuses] = useLocalStorage( - 'stateCellStatuses', - initialStates.cellStatuses - ) const [currentRowNum, setCurrentRow] = useLocalStorage( 'stateCurrentRow', initialStates.currentRowNum @@ -111,11 +107,16 @@ function App() { const [infoModalIsOpen, setInfoModalIsOpen] = useState(firstTime) const [settingsModalIsOpen, setSettingsModalIsOpen] = useState(false) const [difficultyLevel, persistDifficultyLevel] = useLocalStorage('difficulty', Difficulty.Normal) + const [cellStatuses, setCellStatuses] = useLocalStorage( + 'stateCellStatuses', + initialStates.cellStatuses(difficultyLevel) + ) const [answer, setAnswer] = useLocalStorage('stateAnswer', initialStates.answer(difficultyLevel)) const setDifficultyLevel = (d: Difficulty) => { persistDifficultyLevel(d) newGame(d) } + const [board, setBoard] = useLocalStorage('stateBoard', initialStates.board(difficultyLevel)) const getDifficultyLevelInstructions = () => { if (difficultyLevel === Difficulty.Easy) { return ` @@ -168,7 +169,7 @@ function App() { } }, [answer, gameState]) - function getCellStatus(row: number, col: number) { + function getCellStatus(row: number, col: number): CellStatus { return cellStatuses[row][col] ?? CellStatus.Unguessed } @@ -223,7 +224,7 @@ function App() { return } - if (currentRow === 6) return + if (currentRow === rowCount) return updateCellStatuses(row, currentRowNum) updateCharStatuses(row) @@ -264,17 +265,18 @@ function App() { var streak = currentStreak + 1 setCurrentStreak(streak) setLongestStreak((prev: number) => (streak > prev ? streak : prev)) - } else if (gameState === PlayState.Playing && currentRowNum === 6) { + } else if (gameState === PlayState.Playing && currentRowNum === rowCount(difficultyLevel)) { setGameState(PlayState.Lost) setCurrentStreak(0) } }, [ cellStatuses, currentRowNum, - gameState, - setGameState, currentStreak, + difficultyLevel, + gameState, setCurrentStreak, + setGameState, setLongestStreak, ]) @@ -288,7 +290,7 @@ function App() { const newGame = (difficulty: Difficulty) => { setAnswer(initialStates.answer(difficulty)) setGameState(initialStates.gameState) - setBoard(initialStates.board) + setBoard(initialStates.board(difficulty)) setCellStatuses(initialStates.cellStatuses) setCurrentRow(initialStates.currentRowNum) setCharStatuses(initialStates.charStatuses()) diff --git a/src/core.test.ts b/src/core.test.ts index bd66ff72..dc7f41f5 100644 --- a/src/core.test.ts +++ b/src/core.test.ts @@ -93,7 +93,9 @@ test('newCellStatuses adds gray statuses when number is already correctly guesse operandB: 4, result: 36, } - const prev = Array(rowCount).fill(Array(columns.length).fill(CellStatus.Unguessed)) + const prev = Array(rowCount(Difficulty.Normal)).fill( + Array(columns.length).fill(CellStatus.Unguessed) + ) const row: Row = { operandA: 6, operator: '*' as Operator, @@ -120,7 +122,9 @@ test('newCellStatuses adds yellow statuses when number is not correctly guessed operandB: 4, result: 36, } - const prev = Array(rowCount).fill(Array(columns.length).fill(CellStatus.Unguessed)) + const prev = Array(rowCount(Difficulty.Normal)).fill( + Array(columns.length).fill(CellStatus.Unguessed) + ) const row: Row = { operandA: 6, operator: '*' as Operator, @@ -147,7 +151,9 @@ test('newCellStatuses adds yellow statuses when number is correctly guessed in t operandB: 7, result: 35, } - const prev = Array(rowCount).fill(Array(columns.length).fill(CellStatus.Unguessed)) + const prev = Array(rowCount(Difficulty.Normal)).fill( + Array(columns.length).fill(CellStatus.Unguessed) + ) const row: Row = { operandA: 7, operator: '*' as Operator, diff --git a/src/core.ts b/src/core.ts index 7124b587..cbe91d91 100644 --- a/src/core.ts +++ b/src/core.ts @@ -30,7 +30,13 @@ export const allOperators = easyOperators.concat(normalOperators, hardOperators) export type Column = 0 | 1 | 2 | 3 | 4 | 5 export const columns: Column[] = [0, 1, 2, 3, 4, 5] -export const rowCount: number = 6 +export const rowCount = (d: Difficulty): number => { + if (d === Difficulty.Hard) { + return 5 + } else { + return 6 + } +} export enum CellStatus { Green = 'green',