Skip to content

Commit

Permalink
build keyboard and guess state
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahcode committed Jan 8, 2022
1 parent 441a78c commit 8e26ecf
Show file tree
Hide file tree
Showing 11 changed files with 2,580 additions and 76 deletions.
54 changes: 9 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@types/node": "^16.11.19",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"classnames": "^2.3.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
Expand Down Expand Up @@ -39,5 +40,10 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.12"
}
}
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
47 changes: 29 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { useState } from "react";
import { createModuleResolutionCache } from "typescript";
import { Keyboard } from "./components/keyboard/Keyboard";

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 (
<div className="App">
<header className="App-header">
<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>
<Keyboard onChar={onChar} onDelete={onDelete} onEnter={onEnter} />
</div>
);
}
Expand Down
41 changes: 41 additions & 0 deletions src/components/keyboard/Key.tsx
Original file line number Diff line number Diff line change
@@ -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 changes: 115 additions & 0 deletions src/components/keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
@@ -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>
);
};
16 changes: 3 additions & 13 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'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;
}
@tailwind base;
@tailwind components;
@tailwind utilities;
31 changes: 31 additions & 0 deletions src/lib/keyboard.ts
Original file line number Diff line number Diff line change
@@ -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";
Loading

0 comments on commit 8e26ecf

Please sign in to comment.