-
-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
683 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import PlayHeader from 'common/playlists/PlayHeader'; | ||
import Main from './components/Main'; | ||
import './styles.css'; | ||
|
||
function HangmanGame(props: any) { | ||
// Your Code Start below. | ||
return ( | ||
<> | ||
<div className="play-details"> | ||
<PlayHeader play={props} /> | ||
<div className="play-details-body"> | ||
{/* Your Code Starts Here */} | ||
<Main /> | ||
{/* Your Code Ends Here */} | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default HangmanGame; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Hangman Game | ||
|
||
Hangman is an old school favorite, a word game where the goal is simply to find the missing word or words. You will be presented with a number of blank spaces representing the missing letters you need to find. Use the keyboard to guess a letter. | ||
|
||
## Play Demographic | ||
|
||
- Language: ts | ||
- Level: Intermediate | ||
|
||
## Creator Information | ||
|
||
- User: ANKITy102 | ||
- Gihub Link: https://github.com/ANKITy102 | ||
- Blog: | ||
- Video: | ||
|
||
## Implementation Details | ||
|
||
The Hangman game is structured around four major components, each designed for enhanced aesthetics and responsiveness using the Styled Components library: | ||
|
||
1. Drawing.tsx | ||
The Drawing.tsx component plays a crucial role in rendering the visual representation of the Hangman figure as the game progresses. It provides a visual cue to the player regarding their current progress and incorrect guesses. | ||
|
||
2. Word.tsx | ||
The Word.tsx component is responsible for managing the user's input and displaying the correct answer. It ensures a seamless interaction between the player's guesses and the hidden word, providing real-time feedback on the correctness of their choices. | ||
|
||
3. Keyboard.tsx | ||
The Keyboard.tsx component is designed to facilitate user input. It presents an interactive keyboard to players, allowing them to select letters as guesses. This component enhances the user experience by making it intuitive and straightforward to make guesses. | ||
|
||
4. Main.tsx | ||
The Main.tsx component serves as the central hub where all other components are integrated, resulting in the complete Hangman game experience. It orchestrates the flow of the game, including initializing the game state, tracking guessed letters, and determining whether the player has won or lost. | ||
|
||
In addition to these core components, the game relies on a wordList.json file, which contains a collection of hints and words used throughout the gameplay. These hints provide context to players and make the game more engaging and challenging. | ||
|
||
This modular and organized structure ensures that the Hangman game is not only enjoyable but also maintainable and extensible, making it an excellent showcase of best practices in React development. | ||
|
||
|
||
## Consideration | ||
|
||
Update all considerations(if any) | ||
|
||
## Resources | ||
|
||
Update external resources(if any) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { | ||
Head, | ||
Body, | ||
RightArm, | ||
LeftArm, | ||
RightLeg, | ||
LeftLeg, | ||
Element4, | ||
Element3, | ||
Element2, | ||
Element1 | ||
} from '../styled-components'; | ||
|
||
const BodyParts = [Head, Body, RightArm, LeftArm, RightLeg, LeftLeg]; | ||
const Gallows = [Element4, Element3, Element2, Element1]; | ||
|
||
interface DrawingProps { | ||
numberOfGuesses: number; | ||
} | ||
|
||
export default function Drawing({ numberOfGuesses }: DrawingProps) { | ||
return ( | ||
<div style={{ position: 'relative' }}> | ||
{BodyParts.slice(0, numberOfGuesses).map((Component, id) => ( | ||
<Component key={id} /> | ||
))} | ||
{Gallows.map((Component, id) => ( | ||
<Component key={id} /> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useEffect } from 'react'; | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { KEYS } from '../constants/constants'; | ||
import { Key, KeyboardGrid, KeyContainer } from '../styled-components'; | ||
|
||
interface KeyboardProps { | ||
correctLetters: string[]; | ||
incorrectLetters: string[]; | ||
addGuessedLetter: (letter: string) => void; | ||
disabled?: boolean; | ||
} | ||
|
||
export default function Keyboard({ | ||
correctLetters, | ||
incorrectLetters, | ||
addGuessedLetter, | ||
disabled = false | ||
}: KeyboardProps) { | ||
useEffect(() => { | ||
const handleKeyPress = (event: KeyboardEvent) => { | ||
const key = event.key.toLowerCase(); | ||
if ( | ||
!disabled && | ||
KEYS.includes(key) && | ||
!correctLetters.includes(key) && | ||
!incorrectLetters.includes(key) | ||
) { | ||
addGuessedLetter(key); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handleKeyPress); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleKeyPress); | ||
}; | ||
}, [correctLetters, incorrectLetters, addGuessedLetter, disabled]); | ||
|
||
return ( | ||
<KeyContainer> | ||
<KeyboardGrid> | ||
{KEYS.map((key) => { | ||
const active = correctLetters.includes(key); | ||
const inActive = incorrectLetters.includes(key); | ||
|
||
return ( | ||
<Key | ||
active={active} | ||
disabled={active || inActive || disabled} | ||
inActive={inActive} | ||
key={key} | ||
onClick={() => addGuessedLetter(key)} | ||
> | ||
{key} | ||
</Key> | ||
); | ||
})} | ||
</KeyboardGrid> | ||
</KeyContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import Drawing from './Drawing'; | ||
import words from '../constants/wordList.json'; | ||
import Keyboard from './Keyboard'; | ||
import Word from './Word'; | ||
import { | ||
BigContainer, | ||
Container, | ||
EndGame, | ||
P, | ||
Span, | ||
Title, | ||
TryAgainButton | ||
} from '../styled-components'; | ||
import { VscDebugRestart } from 'react-icons/vsc'; | ||
|
||
function getRandomWord(arr: { [key: string]: string }[]) { | ||
const obj = arr[Math.floor(Math.random() * arr.length)]; | ||
const word = Object.values(obj)[0] as string; | ||
|
||
return { question: Object.keys(obj)[0], word }; | ||
} | ||
|
||
export default function App() { | ||
const [wordToGuess, setWordToGuess] = useState<string>(''); | ||
const [hintToGuess, setHintToGuess] = useState<string>(''); | ||
const [guessedLetters, setGuessedLetters] = useState<string[]>([]); | ||
|
||
const incorrectLetters = guessedLetters.filter((letter) => !wordToGuess.includes(letter)); | ||
|
||
const isLoser = incorrectLetters.length >= 6; | ||
const isWinner = wordToGuess.split('').every((letter) => guessedLetters.includes(letter)); | ||
const isGameCompleted = isWinner || isLoser; | ||
|
||
const addGuessedLetter = (letter: string) => { | ||
if (!guessedLetters.includes(letter)) { | ||
setGuessedLetters((currentLetters) => [...currentLetters, letter]); | ||
} | ||
}; | ||
|
||
const restartGame = () => { | ||
const { word, question } = getRandomWord(words); | ||
setHintToGuess(question); | ||
setWordToGuess(word); | ||
setGuessedLetters([]); | ||
}; | ||
|
||
useEffect(() => { | ||
restartGame(); | ||
// eslint-disable-next-line | ||
}, []); | ||
|
||
return ( | ||
<BigContainer> | ||
<Container> | ||
<Title>Hangman</Title> | ||
|
||
{!isGameCompleted ? ( | ||
<Drawing numberOfGuesses={incorrectLetters.length} /> | ||
) : ( | ||
<EndGame isWinner={isWinner}> | ||
{isWinner && 'You are a winner!'} | ||
{isLoser && 'Nice try...'} | ||
</EndGame> | ||
)} | ||
{!isGameCompleted && ( | ||
<P> | ||
<Span>Question: {hintToGuess} </Span> | ||
</P> | ||
)} | ||
<Word guessedLetters={guessedLetters} reveal={isLoser} wordToGuess={wordToGuess} /> | ||
|
||
{isGameCompleted && ( | ||
<TryAgainButton onClick={restartGame}> | ||
<VscDebugRestart /> | ||
</TryAgainButton> | ||
)} | ||
|
||
<Keyboard | ||
addGuessedLetter={addGuessedLetter} | ||
correctLetters={guessedLetters.filter((letter) => wordToGuess.includes(letter))} | ||
disabled={isWinner || isLoser} | ||
incorrectLetters={incorrectLetters} | ||
/> | ||
</Container> | ||
</BigContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { Border, LetterComponent, WordContainer } from '../styled-components'; | ||
|
||
interface WordProps { | ||
guessedLetters: string[]; | ||
wordToGuess: string; | ||
reveal?: boolean; | ||
} | ||
|
||
export default function Word({ guessedLetters, wordToGuess, reveal = false }: WordProps) { | ||
return ( | ||
<WordContainer> | ||
{wordToGuess.split('').map((letter, id) => ( | ||
<Border key={id}> | ||
<LetterComponent guessedLetters={guessedLetters} letter={letter} reveal={reveal}> | ||
{letter} | ||
</LetterComponent> | ||
</Border> | ||
))} | ||
</WordContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
|
||
export const KEYS = [ | ||
'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' | ||
]; |
Oops, something went wrong.