Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

done #92

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
400 changes: 150 additions & 250 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions src/components/Game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ import React from 'react';

import { sample } from '../../utils';
import { WORDS } from '../../data';

import GuessInput from '../GuessInput/GuessInput';
import GuessResults from '../GuessResults/GuessResults';
// Pick a random word on every pageload.
const answer = sample(WORDS);
// To make debugging easier, we'll log the solution in the console.
console.info({ answer });

function Game() {
return <>Put a game here!</>;
const [guesses,setGuesses] = React.useState([])

function handleSubmitGuess(tentativeGuess){
setGuesses([...guesses, tentativeGuess])
}

return <>
<GuessResults guesses={guesses} answer={answer}/>
<GuessInput handleSubmitGuess={handleSubmitGuess}/>
</>

;
}

export default Game;
29 changes: 29 additions & 0 deletions src/components/Guess/Guess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { range } from '../../utils';
import { checkGuess } from '../../game-helpers'

function Cell({letter, status}) {
return(
<span className={`cell ${status}`}>
{letter}
</span>
)

}

function Guess({value, answer}) {
const result = checkGuess(value,answer)
return (
<p className="guess">
{range(5).map((num) => (
<Cell
key={num}
letter = {result ? result[num].letter : undefined}
status = {result ? result[num].status : undefined}
/>
))}
</p>
);
}

export default Guess;
2 changes: 2 additions & 0 deletions src/components/Guess/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Guess';
export { default } from './Guess';
34 changes: 34 additions & 0 deletions src/components/GuessInput/GuessInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';

function GuessInput({handleSubmitGuess}) {
const[tentativeGuess, setTentativeGuess] = React.useState()

function handleSubmit(event) {
event.preventDefault();
if (tentativeGuess.length !== 5) {
window.alert('Please enter 5 letters! :)')
return;
}
handleSubmitGuess(tentativeGuess)
setTentativeGuess('')
}

return(
<form onSubmit={handleSubmit} className="guess-input-wrapper">
<label htmlFor="guess-input">Enter guess:</label>
<input
required
minLength={5}
maxLength={5}
id="guess-input" type="text"
value={tentativeGuess}
onChange={(event)=> {
const nextGuess = event.target.value.toUpperCase()
setTentativeGuess(nextGuess)
}}
/>
</form>
);
}

export default GuessInput;
2 changes: 2 additions & 0 deletions src/components/GuessInput/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './GuessInput';
export { default } from './GuessInput';
15 changes: 15 additions & 0 deletions src/components/GuessResults/GuessResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { NUM_OF_GUESSES_ALLOWED} from '../../constants'
import { range } from '../../utils';
import Guess from '../Guess/Guess';
function GuessResults({ guesses, answer }) {
return (
<div className="guess-results">
{range(NUM_OF_GUESSES_ALLOWED).map((num) => (
<Guess key={num} value={guesses[num]} answer={answer}/>
))}
</div>
);
}

export default GuessResults;
2 changes: 2 additions & 0 deletions src/components/GuessResults/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './GuessResults';
export { default } from './GuessResults';