Skip to content

Commit 06a198d

Browse files
committed
Head directly into a new round after each correct guess
1 parent 57bbc58 commit 06a198d

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/App.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { useEffect } from "react";
33
import styles from "./App.module.css";
44
import useAppState from "./useAppState";
55
import normalizeString from "./util/normalizeString";
6+
import pluralize from "./util/pluralize";
67

78
function App() {
89
const [state, dispatch] = useAppState();
@@ -43,21 +44,25 @@ function App() {
4344
Guess:
4445
<input
4546
type="text"
47+
autoFocus
4648
value={state.guess}
4749
onChange={(ev) =>
4850
dispatch({ type: "update-guess", newGuess: ev.target.value })
4951
}
5052
/>
5153
</label>
5254
</div>
55+
<button onClick={() => dispatch({ type: "end-game" })}>
56+
End game
57+
</button>
5358
</div>
5459
);
5560
}
5661

5762
case "post-game": {
5863
return (
5964
<div className={styles.container}>
60-
<div>Nice game! You guessed {state.goal}</div>
65+
<div>You guessed {pluralize(state.wordsGuessed, "word")}.</div>
6166
<button onClick={() => dispatch({ type: "start-game" })}>
6267
Begin new game
6368
</button>

src/useAppState.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ export type State = Readonly<
1212
phase: "in-game";
1313
goal: string;
1414
guess: string;
15+
wordsGuessed: number;
1516
wordPack: readonly string[];
1617
}
1718
| {
1819
phase: "post-game";
19-
goal: string;
20+
wordsGuessed: number;
2021
wordPack: readonly string[];
2122
}
2223
>;
@@ -28,10 +29,24 @@ export function getInitialState(): State {
2829
export type Action =
2930
| { type: "load-data"; wordPack: readonly string[] }
3031
| { type: "start-game" }
31-
| { type: "update-guess"; newGuess: string };
32+
| { type: "update-guess"; newGuess: string }
33+
| { type: "end-game" };
3234

3335
export function reducer(state: State, action: Action): State {
3436
switch (action.type) {
37+
case "end-game": {
38+
// No-op if not in a game.
39+
if (state.phase !== "in-game") {
40+
return state;
41+
}
42+
43+
return {
44+
phase: "post-game",
45+
wordsGuessed: state.wordsGuessed,
46+
wordPack: state.wordPack,
47+
};
48+
}
49+
3550
case "load-data": {
3651
// No-op if not in pre-game phase.
3752
if (state.phase !== "pre-game") {
@@ -57,6 +72,7 @@ export function reducer(state: State, action: Action): State {
5772
phase: "in-game",
5873
goal: getRandomElement(wordPack),
5974
guess: "",
75+
wordsGuessed: 0,
6076
wordPack,
6177
};
6278
}
@@ -69,9 +85,10 @@ export function reducer(state: State, action: Action): State {
6985

7086
if (normalizeString(action.newGuess) === state.goal) {
7187
return {
72-
phase: "post-game",
73-
goal: state.goal,
74-
wordPack: state.wordPack,
88+
...state,
89+
wordsGuessed: state.wordsGuessed + 1,
90+
goal: getRandomElement(state.wordPack),
91+
guess: "",
7592
};
7693
}
7794

src/util/pluralize.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function pluralize(
2+
count: number,
3+
singularNoun: string,
4+
pluralNoun?: string,
5+
): string {
6+
const noun = count === 1 ? singularNoun : (pluralNoun ?? singularNoun + "s");
7+
return `${count} ${noun}`;
8+
}

0 commit comments

Comments
 (0)