Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

feat(win/reset): Add win condition and reset button #6

Open
wants to merge 1 commit into
base: feat_highlighting_show_which_letters_are
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions wordle-tutorial/src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@
}
}
}

.reset-button {
margin-top: 24px;
color: #999999;
text-transform: uppercase;
font-weight: bold;
font-size: 12px;
cursor: pointer;

&:hover {
color: #66CC66;
}
}
}
27 changes: 21 additions & 6 deletions wordle-tutorial/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const LETTERS = "abcdefghijklmnopqrstuvwxyz";
function App() {
// The word the user is trying to guess
const [actualWord] = useState("magic");
// Whether the game is still going
const [isPlaying, setIsPlaying] = useState(true);
// The current word
const [buffer, setBuffer] = useState("");
// All previous guesses
Expand All @@ -16,6 +18,10 @@ function App() {
// Called every time the user presses a key on the page
const handler = (ev: KeyboardEvent) => {
if (ev.key === "Enter" && buffer.length === 5) {
if (buffer === actualWord) {
window.alert("You win!");
setIsPlaying(false);
}
setHistory((oldValue) => {
return [...oldValue, buffer];
});
Expand All @@ -28,11 +34,13 @@ function App() {
};

// Register the handler defined above (and unregister it if we update it and need to re-register a new version)
document.addEventListener("keydown", handler);
return () => {
document.removeEventListener("keydown", handler);
};
}, [buffer]);
if (isPlaying) {
document.addEventListener("keydown", handler);
return () => {
document.removeEventListener("keydown", handler);
};
}
}, [buffer, isPlaying]);

return (
<div className="guesses">
Expand All @@ -41,7 +49,14 @@ function App() {
<Word key={idx} guess={pastGuess} highlight actualWord={actualWord} />
);
})}
<Word guess={buffer} actualWord={actualWord} />
{isPlaying && <Word guess={buffer} actualWord={actualWord} />}
<div className="reset-button" onClick={() => {
setIsPlaying(true);
setBuffer("");
setHistory([]);
}}>
Reset
</div>
</div>
);
}
Expand Down