mirror of
https://github.com/FranP-code/Hangman-game-with-React.git
synced 2025-10-13 00:42:32 +00:00
Victory and defeat logics done
This commit is contained in:
@@ -2,7 +2,7 @@ import React, {useState} from "react";
|
||||
import CurrentScore from "./components/CurrentScore";
|
||||
import Hangman from "./components/Hangman/Hangman";
|
||||
import Header from "./components/Header";
|
||||
import PuzzleWord from "./components/PuzzleWord";
|
||||
import PuzzleWord from "./components/Hangman/PuzzleWord/PuzzleWord";
|
||||
|
||||
function App() {
|
||||
|
||||
|
||||
9
src/components/Hangman/PuzzleWord/Defeat.jsx
Normal file
9
src/components/Hangman/PuzzleWord/Defeat.jsx
Normal file
@@ -0,0 +1,9 @@
|
||||
const Defeat = () => {
|
||||
return (
|
||||
<div>
|
||||
<h1>DEFEAT</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Defeat
|
||||
@@ -1,9 +1,13 @@
|
||||
import React, {useState} from 'react'
|
||||
import Register_Input from './Scripts/Register input'
|
||||
import Register_Input from '../../Scripts/Register input'
|
||||
import Defeat from './Defeat'
|
||||
import Victory from './Victory'
|
||||
|
||||
const PuzzleWord = ({hangmanFrame, setHangmanFrame}) => {
|
||||
|
||||
const [actualWord, setActualWord] = useState('murcielago')
|
||||
const [isVictory, setIsVictory] = useState(false)
|
||||
const [isDefeat, setIsDefeat] = useState(false)
|
||||
|
||||
const generatePuzzleWord = () => {
|
||||
const puzzleWord = document.getElementById('puzzleWord')
|
||||
@@ -35,17 +39,17 @@ const PuzzleWord = ({hangmanFrame, setHangmanFrame}) => {
|
||||
|
||||
React.useEffect(() => {
|
||||
|
||||
|
||||
Register_Input(actualWord, hangmanFrame, setHangmanFrame)
|
||||
Register_Input(actualWord, hangmanFrame, setHangmanFrame, setIsVictory, setIsDefeat)
|
||||
|
||||
}, [hangmanFrame])
|
||||
|
||||
|
||||
return (
|
||||
<div className="puzzleWord" id="puzzleWord">
|
||||
|
||||
</div>
|
||||
<>
|
||||
<div className="puzzleWord" id="puzzleWord"></div>
|
||||
|
||||
{isVictory ? <Victory /> : null}
|
||||
{isDefeat ? <Defeat /> : null}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
11
src/components/Hangman/PuzzleWord/Victory.jsx
Normal file
11
src/components/Hangman/PuzzleWord/Victory.jsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import React from 'react'
|
||||
|
||||
const Victory = () => {
|
||||
return (
|
||||
<div>
|
||||
<h1>VICTORIA</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Victory
|
||||
25
src/components/Scripts/CheckVictory.jsx
Normal file
25
src/components/Scripts/CheckVictory.jsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import Victory from "../Hangman/PuzzleWord/Victory";
|
||||
|
||||
const CheckVictory = (setIsVictory) => {
|
||||
|
||||
let allChildrenHaveText = true
|
||||
|
||||
const puzzleWord = document.getElementById('puzzleWord')
|
||||
const childrens = [...puzzleWord.children]
|
||||
|
||||
childrens.forEach(children => {
|
||||
|
||||
if (children.textContent === '') {
|
||||
allChildrenHaveText = false
|
||||
}
|
||||
});
|
||||
|
||||
console.log(allChildrenHaveText)
|
||||
|
||||
if (allChildrenHaveText) {
|
||||
|
||||
setIsVictory(true)
|
||||
}
|
||||
}
|
||||
|
||||
export default CheckVictory
|
||||
@@ -1,4 +1,6 @@
|
||||
const Register_Input = (actualWord, hangmanFrame, setHangmanFrame) => {
|
||||
import CheckVictory from "./CheckVictory.jsx";
|
||||
|
||||
const Register_Input = (actualWord, hangmanFrame, setHangmanFrame, setIsVictory, setIsDefeat) => {
|
||||
|
||||
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"];
|
||||
|
||||
@@ -6,7 +8,9 @@ const Register_Input = (actualWord, hangmanFrame, setHangmanFrame) => {
|
||||
|
||||
const currentKey = event.key.toLowerCase()
|
||||
|
||||
if (hangmanFrame < 6 && alphabet.includes(currentKey) ) {
|
||||
console.log(hangmanFrame)
|
||||
|
||||
if (hangmanFrame <= 5 && alphabet.includes(currentKey) ) {
|
||||
|
||||
actualWord = actualWord.toLowerCase()
|
||||
|
||||
@@ -41,16 +45,25 @@ const Register_Input = (actualWord, hangmanFrame, setHangmanFrame) => {
|
||||
|
||||
});
|
||||
|
||||
CheckVictory(setIsVictory)
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
const quantity = hangmanFrame + 1
|
||||
setHangmanFrame(quantity)
|
||||
|
||||
if (hangmanFrame === 5) {
|
||||
setIsDefeat(true)
|
||||
|
||||
}
|
||||
|
||||
removeRegisterInput()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const removeRegisterInput = () => {
|
||||
|
||||
Reference in New Issue
Block a user