update readme
This commit is contained in:
parent
6309359770
commit
97b8ac6be4
6 changed files with 231 additions and 15 deletions
31
README.md
31
README.md
|
@ -1,10 +1,23 @@
|
|||
word that updates as you type within the active row
|
||||
once entered, you want to store the whole word in an array full of all the guesses so far.
|
||||
["treat", "races", "", "", "", "" ]
|
||||
currentRow = 0 and increments
|
||||
once you guess a word, the status of the letters is stored. That updates the appearance of the keyboard, and also the color of the cell in the word row.
|
||||
after you guess a word, the next row is "unlocked" or the active row that you are now working in. You can input letters starting at the furthest left, as the row is filled, the "active" cell moves one to the right.
|
||||
If you delete a letter, the cell to the left is emptied and becomes the current cell.
|
||||
If you hit enter before all cells are filled, it errors out
|
||||
If you guess a word that isn't in the words list, it errors out.
|
||||
# Wordle Clone
|
||||
|
||||
_Inspiration_
|
||||
This game is an open source clone of the immensely popular online word guessing game Wordle. Like many others all over the world, I saw the signature pattern of green, yellow, and white squares popping up all over social media and the web and had to check it out. After a few days of play, I decided it would be great for my learning to try to rebuild Wordle in React!
|
||||
|
||||
_Design Decisions_
|
||||
I used a combination of React, Typescript, and Tailwind to build this Wordle Clone. When examining the original Wordle, I assumed the list might come from an external API or database, but after investigating in chrome dev tools I found that the list of words is simply stored in an array on the front end. I'm using the same list as the OG Wordle uses, but watch out for spoilers if you go find the file in this repo! The word match functionality is simple: the word array index increments each day from a fixed game epoch timestamp (only one puzzle per day!) roughly like so:
|
||||
|
||||
```
|
||||
WORDS[Math.floor((NOW_IN_MS - GAME_EPOCH_IN_MS) / ONE_DAY_IN_MS)]
|
||||
```
|
||||
|
||||
React enabled me to componentize the littlest parts of the game - keys and letter cells - and use them as the building blocks for the keyboard, word grid, and winning solution graphic. As for handling state, I used the built in useState and useEffect hooks to track guesses, whether the game is won, and to conditionally render popups.
|
||||
|
||||
In addition to other things, Typescript helped ensure type safety for the statuses of each guessed letter, which were used in many areas of the app and needed to be accurate for the game to work correctly.
|
||||
|
||||
I implemented Tailwind mostly because I wanted to learn how to use Tailwind CSS, but I also took advantage of [Tailwind UI](https://tailwindui.com/) with their [headless package](https://headlessui.dev/) to build the modals and notifications. This was such an easy way to build simple popups for how to play, winning the game, and invalid words.
|
||||
|
||||
_To Run Locally_
|
||||
Clone the repository and perform the following command line actions:
|
||||
|
||||
- npm init
|
||||
- npm run start
|
||||
|
|
30
src/App.tsx
30
src/App.tsx
|
@ -1,8 +1,11 @@
|
|||
import { InformationCircleIcon } from "@heroicons/react/outline";
|
||||
import { useState, useEffect } from "react";
|
||||
import { Alert } from "./components/alerts/Alert";
|
||||
import { Grid } from "./components/grid/Grid";
|
||||
import { Keyboard } from "./components/keyboard/Keyboard";
|
||||
import { WinModal } from "./components/win-modal/WinModal";
|
||||
import { AboutModal } from "./components/modals/AboutModal";
|
||||
import { InfoModal } from "./components/modals/InfoModal";
|
||||
import { WinModal } from "./components/modals/WinModal";
|
||||
import { isWordInWordList, isWinningWord, solution } from "./lib/words";
|
||||
|
||||
function App() {
|
||||
|
@ -10,6 +13,8 @@ function App() {
|
|||
const [currentGuess, setCurrentGuess] = useState("");
|
||||
const [isGameWon, setIsGameWon] = useState(false);
|
||||
const [isWinModalOpen, setIsWinModalOpen] = useState(false);
|
||||
const [isInfoModalOpen, setIsInfoModalOpen] = useState(false);
|
||||
const [isAboutModalOpen, setIsAboutModalOpen] = useState(false);
|
||||
const [isWordNotFoundAlertOpen, setIsWordNotFoundAlertOpen] = useState(false);
|
||||
const [isGameLost, setIsGameLost] = useState(false);
|
||||
|
||||
|
@ -63,6 +68,13 @@ function App() {
|
|||
message={`You lost, the word was ${solution}`}
|
||||
isOpen={isGameLost}
|
||||
/>
|
||||
<div className="flex w-80 mx-auto items-center mb-8">
|
||||
<h1 className="text-xl grow font-bold">Not Wordle</h1>
|
||||
<InformationCircleIcon
|
||||
className="h-6 w-6 cursor-pointer"
|
||||
onClick={() => setIsInfoModalOpen(true)}
|
||||
/>
|
||||
</div>
|
||||
<Grid guesses={guesses} currentGuess={currentGuess} />
|
||||
<Keyboard
|
||||
onChar={onChar}
|
||||
|
@ -75,6 +87,22 @@ function App() {
|
|||
handleClose={() => setIsWinModalOpen(false)}
|
||||
guesses={guesses}
|
||||
/>
|
||||
<InfoModal
|
||||
isOpen={isInfoModalOpen}
|
||||
handleClose={() => setIsInfoModalOpen(false)}
|
||||
/>
|
||||
<AboutModal
|
||||
isOpen={isAboutModalOpen}
|
||||
handleClose={() => setIsAboutModalOpen(false)}
|
||||
/>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="mx-auto mt-8 flex items-center px-2.5 py-1.5 border border-transparent text-xs font-medium rounded text-indigo-700 bg-indigo-100 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||
onClick={() => setIsAboutModalOpen(true)}
|
||||
>
|
||||
About this game
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -8,12 +8,12 @@ type Props = {
|
|||
|
||||
export const Cell = ({ value, status }: Props) => {
|
||||
const classes = classnames(
|
||||
"w-14 h-14 border-solid border-2 border-slate-200 flex items-center justify-center mx-0.5 text-lg font-bold rounded",
|
||||
"w-14 h-14 border-solid border-2 flex items-center justify-center mx-0.5 text-lg font-bold rounded",
|
||||
{
|
||||
"bg-white": !status,
|
||||
"bg-slate-400 text-white": status === "absent",
|
||||
"bg-green-500 text-white": status === "correct",
|
||||
"bg-yellow-500 text-white": status === "present",
|
||||
"bg-white border-slate-200": !status,
|
||||
"bg-slate-400 text-white border-slate-400": status === "absent",
|
||||
"bg-green-500 text-white border-green-500": status === "correct",
|
||||
"bg-yellow-500 text-white border-yellow-500": status === "present",
|
||||
}
|
||||
);
|
||||
|
||||
|
|
70
src/components/modals/AboutModal.tsx
Normal file
70
src/components/modals/AboutModal.tsx
Normal file
|
@ -0,0 +1,70 @@
|
|||
import { Fragment } from "react";
|
||||
import { Dialog, Transition } from "@headlessui/react";
|
||||
|
||||
type Props = {
|
||||
isOpen: boolean;
|
||||
handleClose: () => void;
|
||||
};
|
||||
|
||||
export const AboutModal = ({ isOpen, handleClose }: Props) => {
|
||||
return (
|
||||
<Transition.Root show={isOpen} as={Fragment}>
|
||||
<Dialog
|
||||
as="div"
|
||||
className="fixed z-10 inset-0 overflow-y-auto"
|
||||
onClose={handleClose}
|
||||
>
|
||||
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0"
|
||||
enterTo="opacity-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Dialog.Overlay className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
|
||||
</Transition.Child>
|
||||
|
||||
{/* This element is to trick the browser into centering the modal contents. */}
|
||||
<span
|
||||
className="hidden sm:inline-block sm:align-middle sm:h-screen"
|
||||
aria-hidden="true"
|
||||
>
|
||||
​
|
||||
</span>
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
||||
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
>
|
||||
<div className="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-sm sm:w-full sm:p-6">
|
||||
<div>
|
||||
<div className="text-center">
|
||||
<Dialog.Title
|
||||
as="h3"
|
||||
className="text-lg leading-6 font-medium text-gray-900"
|
||||
>
|
||||
About
|
||||
</Dialog.Title>
|
||||
<div className="mt-2">
|
||||
<p className="text-sm text-gray-500">
|
||||
- loved playing Wordle - decided to try writing it with
|
||||
React, Typescript, and Tailwind - code is open source and
|
||||
available here [link]
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Transition.Child>
|
||||
</div>
|
||||
</Dialog>
|
||||
</Transition.Root>
|
||||
);
|
||||
};
|
105
src/components/modals/InfoModal.tsx
Normal file
105
src/components/modals/InfoModal.tsx
Normal file
|
@ -0,0 +1,105 @@
|
|||
import { Fragment } from "react";
|
||||
import { Dialog, Transition } from "@headlessui/react";
|
||||
import { CheckIcon } from "@heroicons/react/outline";
|
||||
import { Cell } from "../grid/Cell";
|
||||
|
||||
type Props = {
|
||||
isOpen: boolean;
|
||||
handleClose: () => void;
|
||||
};
|
||||
|
||||
export const InfoModal = ({ isOpen, handleClose }: Props) => {
|
||||
return (
|
||||
<Transition.Root show={isOpen} as={Fragment}>
|
||||
<Dialog
|
||||
as="div"
|
||||
className="fixed z-10 inset-0 overflow-y-auto"
|
||||
onClose={handleClose}
|
||||
>
|
||||
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0"
|
||||
enterTo="opacity-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
>
|
||||
<Dialog.Overlay className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
|
||||
</Transition.Child>
|
||||
|
||||
{/* This element is to trick the browser into centering the modal contents. */}
|
||||
<span
|
||||
className="hidden sm:inline-block sm:align-middle sm:h-screen"
|
||||
aria-hidden="true"
|
||||
>
|
||||
​
|
||||
</span>
|
||||
<Transition.Child
|
||||
as={Fragment}
|
||||
enter="ease-out duration-300"
|
||||
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
enterTo="opacity-100 translate-y-0 sm:scale-100"
|
||||
leave="ease-in duration-200"
|
||||
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
||||
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||
>
|
||||
<div className="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-sm sm:w-full sm:p-6">
|
||||
<div>
|
||||
<div className="text-center">
|
||||
<Dialog.Title
|
||||
as="h3"
|
||||
className="text-lg leading-6 font-medium text-gray-900"
|
||||
>
|
||||
How to play
|
||||
</Dialog.Title>
|
||||
<div className="mt-2">
|
||||
<p className="text-sm text-gray-500">
|
||||
Guess the WORDLE in 6 tries. After each guess, the color
|
||||
of the tiles will change to show how close your guess was
|
||||
to the word.
|
||||
</p>
|
||||
|
||||
<div className="flex justify-center mb-1 mt-4">
|
||||
<Cell value="W" status="correct" />
|
||||
<Cell value="E" />
|
||||
<Cell value="A" />
|
||||
<Cell value="R" />
|
||||
<Cell value="Y" />
|
||||
</div>
|
||||
<p className="text-sm text-gray-500">
|
||||
The letter W is in the word and in the correct spot.
|
||||
</p>
|
||||
|
||||
<div className="flex justify-center mb-1 mt-4">
|
||||
<Cell value="P" />
|
||||
<Cell value="I" status="present" />
|
||||
<Cell value="L" />
|
||||
<Cell value="O" />
|
||||
<Cell value="T" />
|
||||
</div>
|
||||
<p className="text-sm text-gray-500">
|
||||
The letter L is in the word but in the wrong spot.
|
||||
</p>
|
||||
|
||||
<div className="flex justify-center mb-1 mt-4">
|
||||
<Cell value="V" />
|
||||
<Cell value="A" />
|
||||
<Cell value="G" />
|
||||
<Cell value="U" status="absent" />
|
||||
<Cell value="E" />
|
||||
</div>
|
||||
<p className="text-sm text-gray-500">
|
||||
The letter U is not in the word in any spot.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Transition.Child>
|
||||
</div>
|
||||
</Dialog>
|
||||
</Transition.Root>
|
||||
);
|
||||
};
|
Loading…
Add table
Reference in a new issue