@@ -4,17 +4,26 @@ import { KeyboardEvent, useCallback, useEffect, useState } from "react"
4
4
import archive from "./assets/simpleWords.json"
5
5
import GuessingWord from "./components/GuessingWord/GuessingWord"
6
6
import HangmanSvg from "./components/HangmanSvg/HangmanSvg"
7
-
7
+ import ReloadButton from "./components/ReloadButton/ReloadButton"
8
8
9
9
function App ( ) {
10
-
11
10
const [ wordFromArchive , setWordFromArchive ] = useState ( ( ) => {
12
- return archive [ Math . floor ( Math . random ( ) * archive . length ) ] . toLowerCase ( )
11
+ return newWord ( )
13
12
} )
14
13
15
14
16
15
const [ usedLetters , setUsedLetters ] = useState < string [ ] > ( [ ] )
17
16
17
+ function newWord ( ) {
18
+ return archive [ Math . floor ( Math . random ( ) * archive . length ) ] . toLowerCase ( )
19
+ }
20
+
21
+ function newGameFn ( ) {
22
+ setWordFromArchive ( newWord ( ) )
23
+ setUsedLetters ( [ ] )
24
+ }
25
+
26
+
18
27
const incorrectLetters = usedLetters . filter ( ( letter ) => ! wordFromArchive . includes ( letter ) ) ;
19
28
20
29
const correctLetters = usedLetters . filter ( letter => wordFromArchive . includes ( letter ) ) ;
@@ -25,7 +34,7 @@ function App() {
25
34
const addLetter = useCallback ( ( letter :string ) => {
26
35
// letter = letter.toLowerCase();
27
36
if ( usedLetters . includes ( letter ) || gameIsLost || gameIsWon ) return
28
-
37
+ < ReloadButton />
29
38
setUsedLetters ( oldLetters => [ ...oldLetters , letter ] )
30
39
} , [ usedLetters ] )
31
40
@@ -59,12 +68,16 @@ function App() {
59
68
< HangmanSvg numberWrongLetters = { incorrectLetters . length } />
60
69
< GuessingWord usedLetters = { usedLetters } showWord = { gameIsLost }
61
70
wordFromArchive = { wordFromArchive } />
62
- < Letters
71
+
72
+ { gameIsLost || gameIsWon
73
+ ? < ReloadButton newGame = { newGameFn } />
74
+ : < Letters
63
75
correctLetters = { correctLetters }
64
76
incorrectLetters = { incorrectLetters }
65
77
addLetter = { addLetter }
66
78
disabled = { disabled }
67
79
/>
80
+ }
68
81
</ div >
69
82
)
70
83
}
0 commit comments