Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 17 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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: () => {
Expand All @@ -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
Expand All @@ -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 `
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -223,7 +224,7 @@ function App() {
return
}

if (currentRow === 6) return
if (currentRow === rowCount) return

updateCellStatuses(row, currentRowNum)
updateCharStatuses(row)
Expand Down Expand Up @@ -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,
])

Expand All @@ -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())
Expand Down
12 changes: 9 additions & 3 deletions src/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down