build keyboard and guess state
This commit is contained in:
parent
441a78cf2d
commit
8e26ecf70b
11 changed files with 2580 additions and 76 deletions
54
README.md
54
README.md
|
@ -1,46 +1,10 @@
|
||||||
# Getting Started with Create React App
|
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.
|
||||||
|
|
||||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
|
||||||
|
|
||||||
## Available Scripts
|
|
||||||
|
|
||||||
In the project directory, you can run:
|
|
||||||
|
|
||||||
### `npm start`
|
|
||||||
|
|
||||||
Runs the app in the development mode.\
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
|
||||||
|
|
||||||
The page will reload if you make edits.\
|
|
||||||
You will also see any lint errors in the console.
|
|
||||||
|
|
||||||
### `npm test`
|
|
||||||
|
|
||||||
Launches the test runner in the interactive watch mode.\
|
|
||||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
|
||||||
|
|
||||||
### `npm run build`
|
|
||||||
|
|
||||||
Builds the app for production to the `build` folder.\
|
|
||||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
|
||||||
|
|
||||||
The build is minified and the filenames include the hashes.\
|
|
||||||
Your app is ready to be deployed!
|
|
||||||
|
|
||||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
|
||||||
|
|
||||||
### `npm run eject`
|
|
||||||
|
|
||||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
|
||||||
|
|
||||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
|
||||||
|
|
||||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
|
||||||
|
|
||||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
|
||||||
|
|
||||||
## Learn More
|
|
||||||
|
|
||||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
|
||||||
|
|
||||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
|
||||||
|
|
16
package-lock.json
generated
16
package-lock.json
generated
|
@ -15,11 +15,17 @@
|
||||||
"@types/node": "^16.11.19",
|
"@types/node": "^16.11.19",
|
||||||
"@types/react": "^17.0.38",
|
"@types/react": "^17.0.38",
|
||||||
"@types/react-dom": "^17.0.11",
|
"@types/react-dom": "^17.0.11",
|
||||||
|
"classnames": "^2.3.1",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"react-scripts": "5.0.0",
|
"react-scripts": "5.0.0",
|
||||||
"typescript": "^4.5.4",
|
"typescript": "^4.5.4",
|
||||||
"web-vitals": "^2.1.3"
|
"web-vitals": "^2.1.3"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"autoprefixer": "^10.4.2",
|
||||||
|
"postcss": "^8.4.5",
|
||||||
|
"tailwindcss": "^3.0.12"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@babel/code-frame": {
|
"node_modules/@babel/code-frame": {
|
||||||
|
@ -4967,6 +4973,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz",
|
||||||
"integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA=="
|
"integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA=="
|
||||||
},
|
},
|
||||||
|
"node_modules/classnames": {
|
||||||
|
"version": "2.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.1.tgz",
|
||||||
|
"integrity": "sha512-OlQdbZ7gLfGarSqxesMesDa5uz7KFbID8Kpq/SxIoNGDqY8lSYs0D+hhtBXhcdB3rcbXArFr7vlHheLk1voeNA=="
|
||||||
|
},
|
||||||
"node_modules/clean-css": {
|
"node_modules/clean-css": {
|
||||||
"version": "5.2.2",
|
"version": "5.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-5.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-5.2.2.tgz",
|
||||||
|
@ -19403,6 +19414,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz",
|
||||||
"integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA=="
|
"integrity": "sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA=="
|
||||||
},
|
},
|
||||||
|
"classnames": {
|
||||||
|
"version": "2.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.1.tgz",
|
||||||
|
"integrity": "sha512-OlQdbZ7gLfGarSqxesMesDa5uz7KFbID8Kpq/SxIoNGDqY8lSYs0D+hhtBXhcdB3rcbXArFr7vlHheLk1voeNA=="
|
||||||
|
},
|
||||||
"clean-css": {
|
"clean-css": {
|
||||||
"version": "5.2.2",
|
"version": "5.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-5.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/clean-css/-/clean-css-5.2.2.tgz",
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"@types/node": "^16.11.19",
|
"@types/node": "^16.11.19",
|
||||||
"@types/react": "^17.0.38",
|
"@types/react": "^17.0.38",
|
||||||
"@types/react-dom": "^17.0.11",
|
"@types/react-dom": "^17.0.11",
|
||||||
|
"classnames": "^2.3.1",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"react-scripts": "5.0.0",
|
"react-scripts": "5.0.0",
|
||||||
|
@ -39,5 +40,10 @@
|
||||||
"last 1 firefox version",
|
"last 1 firefox version",
|
||||||
"last 1 safari version"
|
"last 1 safari version"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"autoprefixer": "^10.4.2",
|
||||||
|
"postcss": "^8.4.5",
|
||||||
|
"tailwindcss": "^3.0.12"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
postcss.config.js
Normal file
6
postcss.config.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module.exports = {
|
||||||
|
plugins: {
|
||||||
|
tailwindcss: {},
|
||||||
|
autoprefixer: {},
|
||||||
|
},
|
||||||
|
}
|
47
src/App.tsx
47
src/App.tsx
|
@ -1,24 +1,35 @@
|
||||||
import React from 'react';
|
import { useState } from "react";
|
||||||
import logo from './logo.svg';
|
import { createModuleResolutionCache } from "typescript";
|
||||||
import './App.css';
|
import { Keyboard } from "./components/keyboard/Keyboard";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [guesses, setGuesses] = useState<string[]>([]);
|
||||||
|
const [currentGuess, setCurrentGuess] = useState("");
|
||||||
|
|
||||||
|
const onChar = (value: string) => {
|
||||||
|
if (currentGuess.length < 5) {
|
||||||
|
setCurrentGuess(`${currentGuess}${value}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDelete = () => {
|
||||||
|
setCurrentGuess(currentGuess.slice(0, -1));
|
||||||
|
};
|
||||||
|
|
||||||
|
const onEnter = () => {
|
||||||
|
// TODO: check if the current guess is in the words list
|
||||||
|
if (currentGuess.length === 5 && guesses.length < 6) {
|
||||||
|
setGuesses([...guesses, currentGuess]);
|
||||||
|
setCurrentGuess("");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(currentGuess);
|
||||||
|
console.log(guesses);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div>
|
||||||
<header className="App-header">
|
<Keyboard onChar={onChar} onDelete={onDelete} onEnter={onEnter} />
|
||||||
<img src={logo} className="App-logo" alt="logo" />
|
|
||||||
<p>
|
|
||||||
Edit <code>src/App.tsx</code> and save to reload.
|
|
||||||
</p>
|
|
||||||
<a
|
|
||||||
className="App-link"
|
|
||||||
href="https://reactjs.org"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
Learn React
|
|
||||||
</a>
|
|
||||||
</header>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
41
src/components/keyboard/Key.tsx
Normal file
41
src/components/keyboard/Key.tsx
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import { ReactNode } from "react";
|
||||||
|
import classnames from "classnames";
|
||||||
|
import { CharStatus, CharValue } from "../../lib/keyboard";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
children: ReactNode;
|
||||||
|
value: CharValue;
|
||||||
|
width?: number;
|
||||||
|
status?: CharStatus;
|
||||||
|
onClick: (value: CharValue) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Key = ({
|
||||||
|
children,
|
||||||
|
status,
|
||||||
|
width = 40,
|
||||||
|
value,
|
||||||
|
onClick,
|
||||||
|
}: Props) => {
|
||||||
|
const classes = classnames(
|
||||||
|
"flex items-center justify-center rounded mx-0.5 text-xs font-bold cursor-pointer",
|
||||||
|
{
|
||||||
|
"bg-slate-200 hover:bg-slate-300 active:bg-slate-400": !status,
|
||||||
|
"bg-slate-400 text-white": status === "absent",
|
||||||
|
"bg-green-500 hover:bg-green-600 active:bg-green-700 text-white":
|
||||||
|
status === "correct",
|
||||||
|
"bg-yellow-500 hover:bg-yellow-600 active:bg-yellow-700 text-white":
|
||||||
|
status === "present",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{ width: `${width}px`, height: "58px" }}
|
||||||
|
className={classes}
|
||||||
|
onClick={() => onClick(value)}
|
||||||
|
>
|
||||||
|
<span>{children}</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
115
src/components/keyboard/Keyboard.tsx
Normal file
115
src/components/keyboard/Keyboard.tsx
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
import { CharValue } from "../../lib/keyboard";
|
||||||
|
import { Key } from "./Key";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
onChar: (value: string) => void;
|
||||||
|
onDelete: () => void;
|
||||||
|
onEnter: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Keyboard = (props: Props) => {
|
||||||
|
const onClick = (value: CharValue) => {
|
||||||
|
if (value === "ENTER") {
|
||||||
|
return props.onEnter();
|
||||||
|
}
|
||||||
|
if (value === "DELETE") {
|
||||||
|
return props.onDelete();
|
||||||
|
}
|
||||||
|
return props.onChar(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="flex justify-center mb-1">
|
||||||
|
<Key value="Q" onClick={onClick}>
|
||||||
|
Q
|
||||||
|
</Key>
|
||||||
|
<Key value="W" onClick={onClick}>
|
||||||
|
W
|
||||||
|
</Key>
|
||||||
|
<Key value="E" onClick={onClick}>
|
||||||
|
E
|
||||||
|
</Key>
|
||||||
|
<Key status="correct" value="R" onClick={onClick}>
|
||||||
|
R
|
||||||
|
</Key>
|
||||||
|
<Key value="T" onClick={onClick}>
|
||||||
|
T
|
||||||
|
</Key>
|
||||||
|
<Key value="Y" onClick={onClick}>
|
||||||
|
Y
|
||||||
|
</Key>
|
||||||
|
<Key status="present" value="U" onClick={onClick}>
|
||||||
|
U
|
||||||
|
</Key>
|
||||||
|
<Key value="I" onClick={onClick}>
|
||||||
|
I
|
||||||
|
</Key>
|
||||||
|
<Key value="O" onClick={onClick}>
|
||||||
|
O
|
||||||
|
</Key>
|
||||||
|
<Key value="P" onClick={onClick}>
|
||||||
|
P
|
||||||
|
</Key>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-center mb-1">
|
||||||
|
<Key value="A" onClick={onClick}>
|
||||||
|
A
|
||||||
|
</Key>
|
||||||
|
<Key value="S" onClick={onClick}>
|
||||||
|
S
|
||||||
|
</Key>
|
||||||
|
<Key value="D" onClick={onClick}>
|
||||||
|
D
|
||||||
|
</Key>
|
||||||
|
<Key status="present" value="F" onClick={onClick}>
|
||||||
|
F
|
||||||
|
</Key>
|
||||||
|
<Key value="G" onClick={onClick}>
|
||||||
|
G
|
||||||
|
</Key>
|
||||||
|
<Key value="H" onClick={onClick}>
|
||||||
|
H
|
||||||
|
</Key>
|
||||||
|
<Key value="J" onClick={onClick}>
|
||||||
|
J
|
||||||
|
</Key>
|
||||||
|
<Key status="absent" value="K" onClick={onClick}>
|
||||||
|
K
|
||||||
|
</Key>
|
||||||
|
<Key value="L" onClick={onClick}>
|
||||||
|
L
|
||||||
|
</Key>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-center">
|
||||||
|
<Key width={65.4} value="ENTER" onClick={onClick}>
|
||||||
|
ENTER
|
||||||
|
</Key>
|
||||||
|
<Key value="Z" onClick={onClick}>
|
||||||
|
Z
|
||||||
|
</Key>
|
||||||
|
<Key value="X" onClick={onClick}>
|
||||||
|
X
|
||||||
|
</Key>
|
||||||
|
<Key value="C" onClick={onClick}>
|
||||||
|
C
|
||||||
|
</Key>
|
||||||
|
<Key value="V" onClick={onClick}>
|
||||||
|
V
|
||||||
|
</Key>
|
||||||
|
<Key value="B" onClick={onClick}>
|
||||||
|
B
|
||||||
|
</Key>
|
||||||
|
<Key value="N" onClick={onClick}>
|
||||||
|
N
|
||||||
|
</Key>
|
||||||
|
<Key value="M" onClick={onClick}>
|
||||||
|
M
|
||||||
|
</Key>
|
||||||
|
<Key width={65.4} value="DELETE" onClick={onClick}>
|
||||||
|
DELETE
|
||||||
|
</Key>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
|
@ -1,13 +1,3 @@
|
||||||
body {
|
@tailwind base;
|
||||||
margin: 0;
|
@tailwind components;
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
@tailwind utilities;
|
||||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
|
||||||
sans-serif;
|
|
||||||
-webkit-font-smoothing: antialiased;
|
|
||||||
-moz-osx-font-smoothing: grayscale;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
|
||||||
monospace;
|
|
||||||
}
|
|
||||||
|
|
31
src/lib/keyboard.ts
Normal file
31
src/lib/keyboard.ts
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
export type CharStatus = "absent" | "present" | "correct";
|
||||||
|
|
||||||
|
export type CharValue =
|
||||||
|
| "Q"
|
||||||
|
| "W"
|
||||||
|
| "E"
|
||||||
|
| "R"
|
||||||
|
| "T"
|
||||||
|
| "Y"
|
||||||
|
| "U"
|
||||||
|
| "I"
|
||||||
|
| "O"
|
||||||
|
| "P"
|
||||||
|
| "A"
|
||||||
|
| "S"
|
||||||
|
| "D"
|
||||||
|
| "F"
|
||||||
|
| "G"
|
||||||
|
| "H"
|
||||||
|
| "J"
|
||||||
|
| "K"
|
||||||
|
| "L"
|
||||||
|
| "ENTER"
|
||||||
|
| "Z"
|
||||||
|
| "X"
|
||||||
|
| "C"
|
||||||
|
| "V"
|
||||||
|
| "B"
|
||||||
|
| "N"
|
||||||
|
| "M"
|
||||||
|
| "DELETE";
|
2317
src/lib/words.ts
Normal file
2317
src/lib/words.ts
Normal file
File diff suppressed because it is too large
Load diff
7
tailwind.config.js
Normal file
7
tailwind.config.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module.exports = {
|
||||||
|
content: ["./src/**/*.{js,jsx,ts,tsx}"],
|
||||||
|
theme: {
|
||||||
|
extend: {},
|
||||||
|
},
|
||||||
|
plugins: [],
|
||||||
|
};
|
Loading…
Add table
Reference in a new issue