Skip to content

Commit

Permalink
PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
j-m committed Nov 14, 2020
1 parent aefa57c commit 08a31a3
Show file tree
Hide file tree
Showing 21 changed files with 19,013 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
18,507 changes: 18,507 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "word-guess",
"version": "0.1.0",
"private": true,
"main": "src/index.tsx",
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.1.2",
"@testing-library/user-event": "^12.2.2",
"@types/jest": "^26.0.15",
"@types/node": "^12.19.4",
"@types/react": "^16.9.56",
"@types/react-dom": "^16.9.9",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"typescript": "^4.0.5",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"react-scripts": "^4.0.0"
}
}
Binary file added public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions src/components/Game.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import Playing from './Playing/Playing'
import StartForm from './Start/StartForm'

interface GameProps {}
interface GameState {
word: string
playing: boolean
guesses: string[]
}
export default class Game extends React.PureComponent<GameProps, GameState> {
constructor (props: GameProps) {
super(props)
this.state = {
word: "",
playing: false,
guesses: [],
}
}

start = (word:string) => this.setState({playing: true, word: word.toUpperCase()})
reset = () => this.setState({word: "", playing: false, guesses: []})

render() {
return (
<>
{
this.state.playing
? <Playing
objective={this.state.word}
reset={this.reset}
/>
: <StartForm start={this.start}/>
}
</>
)
}
}
25 changes: 25 additions & 0 deletions src/components/Playing/Alphabet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import Letter from './Letter';

const ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V', 'W','X','Y','Z']

interface AlphabetProps {
guesses: string[]
min: string
max: string
}
const Alphabet: React.FunctionComponent<AlphabetProps> = (props: AlphabetProps) => {
return (
<div className='alphabet'>
{Object.values(ALPHABET).map(letter =>
<Letter
key={letter}
char={letter}
guessed={props.guesses.includes(letter)}
ruledOut={letter < props.min || letter > props.max}
/>
)}
</div>
)
}
export default Alphabet
18 changes: 18 additions & 0 deletions src/components/Playing/Letter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'

interface LetterProps {
char: string
guessed: boolean
ruledOut: boolean
}
const Letter: React.FunctionComponent<LetterProps> = (props: LetterProps) => {
let className = 'letter'
if (props.guessed) className += ' guessed'
if (props.ruledOut) className += ' ruledOut'
return (
<span className={className}>
{props.char}
</span>
)
}
export default Letter
73 changes: 73 additions & 0 deletions src/components/Playing/Playing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react'
import Alphabet from './Alphabet'
import Word from './Word'

interface PlayingProps {
objective: string
reset: () => void
}
interface PlayingState {
word: string
guesses: string[]
count: number
}
export default class Playing extends React.PureComponent<PlayingProps, PlayingState> {
constructor (props: PlayingProps) {
super(props)
this.state = {
word: "",
guesses: [],
count:0,
}
}

guess = (letter:string) => {
letter = letter.toUpperCase()
if (this.props.objective[this.state.word.length] === letter) {
this.setState(state => ({...state, word: state.word + letter, guesses: []}))
} else {
this.setState(state => ({...state, guesses: [...state.guesses, letter], count: state.count + 1}))
}
}

render() {
let min = 'A'
let max = 'Z'
const nextLetter = this.props.objective[this.state.word.length]
Object.values(this.state.guesses).forEach(letter => {
if (letter < nextLetter && letter > min) {
min = letter
}
if (letter > nextLetter && letter < max) {
max = letter
}
})

return (
<>
<Alphabet
guesses={this.state.guesses}
min={min}
max={max}
/>
<Word
word={this.state.word}
guess={this.guess}
length={this.props.objective.length}
/>
{this.state.word === this.props.objective
? <button
onClick={this.props.reset}
autoFocus
>
Play Again
</button>
: undefined
}
<>
Guesses: {this.state.count}
</>
</>
)
}
}
41 changes: 41 additions & 0 deletions src/components/Playing/Word.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'

interface WordProps {
word: string
guess: (letter: string)=> void
length: number
}
const Word: React.FunctionComponent<WordProps> = (props: WordProps)=> {
return (
<div id="word">
{Object.values(props.word.split("")).map((letter, index) =>
<span
key={`letter_${index}`}
className='character'
>
{letter}
</span>
)}
{props.word.length < props.length
? <>
<input
type="text"
className='character'
value=""
maxLength={1}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => props.guess(event.target.value) }
autoFocus
/>
{Array.from({length: props.length - props.word.length - 1 }, (_, k) =>
<span
key={`blank_${k}`}
className='character'
/>
)}
</>
: undefined}
</div>
)
}

export default Word
32 changes: 32 additions & 0 deletions src/components/Start/StartForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react'
import WordInput from './WordInput'
import StartGameButton from './StartGameButton'

interface StartFormProps {
start: (word: string) => void
}
interface StartFormState {
word: string
}
export default class StartForm extends React.PureComponent<StartFormProps, StartFormState> {
constructor (props: StartFormProps) {
super(props)
this.state = {
word: "",
}
}

render() {
return (
<form id="start">
<WordInput
word={this.state.word}
onChange={(word: string)=>this.setState({word})}
/>
<StartGameButton
onClick={()=>this.props.start(this.state.word)}
/>
</form>
)
}
}
17 changes: 17 additions & 0 deletions src/components/Start/StartGameButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'

interface StartGameButtonProps {
onClick: ()=> void
}
const StartGameButton: React.FunctionComponent<StartGameButtonProps> = (props: StartGameButtonProps)=> {
return (
<input
type="submit"
style={{fontSize:'3rem', fontWeight:400}}
value='→'
onClick={props.onClick}
/>
)
}

export default StartGameButton
Loading

0 comments on commit 08a31a3

Please sign in to comment.