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 CurrentScore from "./components/CurrentScore";
|
||||||
import Hangman from "./components/Hangman/Hangman";
|
import Hangman from "./components/Hangman/Hangman";
|
||||||
import Header from "./components/Header";
|
import Header from "./components/Header";
|
||||||
import PuzzleWord from "./components/PuzzleWord";
|
import PuzzleWord from "./components/Hangman/PuzzleWord/PuzzleWord";
|
||||||
|
|
||||||
function App() {
|
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 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 PuzzleWord = ({hangmanFrame, setHangmanFrame}) => {
|
||||||
|
|
||||||
const [actualWord, setActualWord] = useState('murcielago')
|
const [actualWord, setActualWord] = useState('murcielago')
|
||||||
|
const [isVictory, setIsVictory] = useState(false)
|
||||||
|
const [isDefeat, setIsDefeat] = useState(false)
|
||||||
|
|
||||||
const generatePuzzleWord = () => {
|
const generatePuzzleWord = () => {
|
||||||
const puzzleWord = document.getElementById('puzzleWord')
|
const puzzleWord = document.getElementById('puzzleWord')
|
||||||
@@ -35,17 +39,17 @@ const PuzzleWord = ({hangmanFrame, setHangmanFrame}) => {
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
|
||||||
|
Register_Input(actualWord, hangmanFrame, setHangmanFrame, setIsVictory, setIsDefeat)
|
||||||
Register_Input(actualWord, hangmanFrame, setHangmanFrame)
|
|
||||||
|
|
||||||
}, [hangmanFrame])
|
}, [hangmanFrame])
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="puzzleWord" id="puzzleWord">
|
<>
|
||||||
|
<div className="puzzleWord" id="puzzleWord"></div>
|
||||||
</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,12 +1,16 @@
|
|||||||
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"];
|
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"];
|
||||||
|
|
||||||
const keyRegister = (event) => {
|
const keyRegister = (event) => {
|
||||||
|
|
||||||
const currentKey = event.key.toLowerCase()
|
const currentKey = event.key.toLowerCase()
|
||||||
|
|
||||||
|
console.log(hangmanFrame)
|
||||||
|
|
||||||
if (hangmanFrame < 6 && alphabet.includes(currentKey) ) {
|
if (hangmanFrame <= 5 && alphabet.includes(currentKey) ) {
|
||||||
|
|
||||||
actualWord = actualWord.toLowerCase()
|
actualWord = actualWord.toLowerCase()
|
||||||
|
|
||||||
@@ -41,16 +45,25 @@ const Register_Input = (actualWord, hangmanFrame, setHangmanFrame) => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
CheckVictory(setIsVictory)
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const quantity = hangmanFrame + 1
|
const quantity = hangmanFrame + 1
|
||||||
setHangmanFrame(quantity)
|
setHangmanFrame(quantity)
|
||||||
|
|
||||||
|
if (hangmanFrame === 5) {
|
||||||
|
setIsDefeat(true)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
removeRegisterInput()
|
removeRegisterInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeRegisterInput = () => {
|
const removeRegisterInput = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user