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

Project/wordle #85

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9,217 changes: 74 additions & 9,143 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/components/AllGuesses/AllGuesses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { NUM_OF_GUESSES_ALLOWED } from '../../constants';
import { range } from '../../utils';
import Guess from '../Guess/Guess';

function AllGuesses({guessList, answer}) {
return <div className="guess-results">
{
range(NUM_OF_GUESSES_ALLOWED).map(num => {
return <Guess key={num} value = {guessList[num]} answer={answer}/>
})
}
</div>
}

export default AllGuesses;
2 changes: 2 additions & 0 deletions src/components/AllGuesses/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './AllGuesses';
export { default } from './AllGuesses';
32 changes: 31 additions & 1 deletion src/components/Game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@ import React from 'react';

import { sample } from '../../utils';
import { WORDS } from '../../data';
import GuessInput from '../GuessInput';
import AllGuesses from '../AllGuesses';
import { NUM_OF_GUESSES_ALLOWED } from '../../constants';
import WonBanner from '../WonBanner';
import LostBanner from '../LostBanner';

// 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 [guessList , setGuessList] = React.useState([]);
const [gameStatus, setGameStatus] = React.useState('running');

function handleGuessList(guess) {
let newGuess = {guess, key: `${guess} - ${Math.random()}`};
let newGuessList = [...guessList, newGuess];
setGuessList(newGuessList);
if(guess.toUpperCase() === answer){
setGameStatus('won');
} else if(newGuessList.length >= NUM_OF_GUESSES_ALLOWED){
setGameStatus('lost')
}
}

return (
<>
<AllGuesses guessList={guessList} answer={answer}/>
<GuessInput handleGuessList={handleGuessList} gameStatus={gameStatus}/>
{
gameStatus === 'won' && <WonBanner guessCount = {guessList.length} />
}
{
gameStatus === 'lost' && <LostBanner answer = {answer} />
}
</>
)
}

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

function Guess({value, answer}) {
return (
<p className="guess">
{
range(5).map(i => {
const result = value ? checkGuess(value.guess, answer): '';
let { status, letter} = result? result[i]: {};
return(<span className={`cell ${status}`} key={i}>
{letter}
</span>)
})
}
</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';
29 changes: 29 additions & 0 deletions src/components/GuessInput/GuessInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';


function GuessInput({handleGuessList, gameStatus}) {
const [guess , setGuess ] = React.useState('');

function handleGuessText(event) {
event.preventDefault();
handleGuessList(guess);
setGuess('');
}

return <div>
<form className="guess-input-wrapper" onSubmit={handleGuessText}>
<label htmlFor="guess-input">Enter guess:</label>
<input id="guess-input"
type="text"
value={guess}
onChange={(event) => {setGuess(event.target.value.toUpperCase())} }
required
minLength={5}
maxLength={5}
disabled={gameStatus != 'running'}
/>
</form>
</div>;
}

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';
9 changes: 9 additions & 0 deletions src/components/LostBanner/LostBanner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

function LostBanner({answer}) {
return <div className="sad banner">
<p>Sorry, the correct answer is <strong>{answer}</strong>.</p>
</div>;
}

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

function WonBanner({guessCount}) {
return <div className="happy banner">
<p>
<strong>Congratulations!</strong> Got it in
<strong>{guessCount} guesses</strong>.
</p>
</div>;
}

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