hangman-react-game 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +3 -0
- package/LICENSE +674 -0
- package/README.md +2 -0
- package/dist/index.cjs +532 -0
- package/dist/index.cjs.js +532 -0
- package/dist/index.esm.js +529 -0
- package/dist/index.js +2 -0
- package/dist/index.js.LICENSE.txt +16 -0
- package/dist/index.msj +529 -0
- package/package.json +29 -0
- package/rollup.config.js +28 -0
- package/src/components/Game.jsx +38 -0
- package/src/components/Hangman.jsx +28 -0
- package/src/hooks/useGame.jsx +173 -0
- package/src/index.js +4 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {useEffect, useState} from "react";
|
|
2
|
+
|
|
3
|
+
const MAX_FAILED_ATTEMPTS = 10;
|
|
4
|
+
const wordDictionary = [
|
|
5
|
+
"apple",
|
|
6
|
+
"banana",
|
|
7
|
+
"cherry",
|
|
8
|
+
"date",
|
|
9
|
+
"elderberry",
|
|
10
|
+
"fig",
|
|
11
|
+
"grape",
|
|
12
|
+
"honeydew",
|
|
13
|
+
"kiwi",
|
|
14
|
+
"lemon",
|
|
15
|
+
"mango",
|
|
16
|
+
"nectarine",
|
|
17
|
+
"orange",
|
|
18
|
+
"papaya",
|
|
19
|
+
"quince",
|
|
20
|
+
"raspberry",
|
|
21
|
+
"strawberry",
|
|
22
|
+
"tangerine",
|
|
23
|
+
"ugli",
|
|
24
|
+
"vanilla",
|
|
25
|
+
"watermelon",
|
|
26
|
+
"xigua",
|
|
27
|
+
"yellow",
|
|
28
|
+
"zucchini",
|
|
29
|
+
"abstract",
|
|
30
|
+
"breeze",
|
|
31
|
+
"cactus",
|
|
32
|
+
"dolphin",
|
|
33
|
+
"eagle",
|
|
34
|
+
"forest",
|
|
35
|
+
"giraffe",
|
|
36
|
+
"horizon",
|
|
37
|
+
"island",
|
|
38
|
+
"jungle",
|
|
39
|
+
"kaleidoscope",
|
|
40
|
+
"lighthouse",
|
|
41
|
+
"mountain",
|
|
42
|
+
"nebula",
|
|
43
|
+
"ocean",
|
|
44
|
+
"prairie",
|
|
45
|
+
"quasar",
|
|
46
|
+
"rainbow",
|
|
47
|
+
"sunrise",
|
|
48
|
+
"tornado",
|
|
49
|
+
"universe",
|
|
50
|
+
"volcano",
|
|
51
|
+
"whale",
|
|
52
|
+
"xerox",
|
|
53
|
+
"yacht",
|
|
54
|
+
"zephyr",
|
|
55
|
+
"albatross",
|
|
56
|
+
"butterfly",
|
|
57
|
+
"chameleon",
|
|
58
|
+
"dinosaur",
|
|
59
|
+
"elephant",
|
|
60
|
+
"flamingo",
|
|
61
|
+
"gazelle",
|
|
62
|
+
"hippopotamus",
|
|
63
|
+
"iguana",
|
|
64
|
+
"jellyfish",
|
|
65
|
+
"kangaroo",
|
|
66
|
+
"lion",
|
|
67
|
+
"monkey",
|
|
68
|
+
"narwhal",
|
|
69
|
+
"octopus",
|
|
70
|
+
"penguin",
|
|
71
|
+
"quail",
|
|
72
|
+
"rhinoceros",
|
|
73
|
+
"squirrel",
|
|
74
|
+
"turtle",
|
|
75
|
+
"umbrella",
|
|
76
|
+
"vulture",
|
|
77
|
+
"walrus",
|
|
78
|
+
"xylophone",
|
|
79
|
+
"yak",
|
|
80
|
+
"zebra",
|
|
81
|
+
"astronomy",
|
|
82
|
+
"biology",
|
|
83
|
+
"chemistry",
|
|
84
|
+
"democracy",
|
|
85
|
+
"ecosystem",
|
|
86
|
+
"fossil",
|
|
87
|
+
"genetics",
|
|
88
|
+
"hypothesis",
|
|
89
|
+
"innovation",
|
|
90
|
+
"jurisdiction",
|
|
91
|
+
"kinetic",
|
|
92
|
+
"laboratory",
|
|
93
|
+
"meteorology",
|
|
94
|
+
"neuroscience",
|
|
95
|
+
"observation",
|
|
96
|
+
"physics",
|
|
97
|
+
"quantum",
|
|
98
|
+
"robotics",
|
|
99
|
+
"synthesis",
|
|
100
|
+
"theorem",
|
|
101
|
+
"unicorn",
|
|
102
|
+
"velocity",
|
|
103
|
+
"wavelength",
|
|
104
|
+
"xenon",
|
|
105
|
+
"youth",
|
|
106
|
+
"zenith",
|
|
107
|
+
"bamboozlement",
|
|
108
|
+
"flabbergasted",
|
|
109
|
+
"hypothetical",
|
|
110
|
+
"juxtaposition",
|
|
111
|
+
"labyrinthine",
|
|
112
|
+
"nebulousness",
|
|
113
|
+
"quintessential",
|
|
114
|
+
"sesquipedalian",
|
|
115
|
+
"transcendental",
|
|
116
|
+
"unpredictable",
|
|
117
|
+
"verisimilitude",
|
|
118
|
+
"whippersnapper",
|
|
119
|
+
"xenotransplant",
|
|
120
|
+
"yesteryear",
|
|
121
|
+
"zigzaggedness"
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
const useGame = () => {
|
|
125
|
+
// Should fetch random word from a dictionary (maybe vary by language)
|
|
126
|
+
const [word, setWord] = useState("");
|
|
127
|
+
const [guessedLetters, setGuessedLetters] = useState([]);
|
|
128
|
+
const [failedAttempts, setFailedAttempts] = useState(0);
|
|
129
|
+
const [hasEnded, setHasEnded] = useState(false);
|
|
130
|
+
const [hasWon, setHasWon] = useState(false);
|
|
131
|
+
|
|
132
|
+
const censorWord = (w) => w.split("").map(x => (guessedLetters.includes(x) || x === " " ? x : "_") + " ").join("");
|
|
133
|
+
const newWord = () => setWord( wordDictionary[Math.floor( Math.random() * wordDictionary.length )]);
|
|
134
|
+
|
|
135
|
+
const guessLetter = async (letter) => {
|
|
136
|
+
if(!hasEnded && letter.length === 1){
|
|
137
|
+
if(!word.includes(letter))
|
|
138
|
+
setFailedAttempts(failedAttempts+1);
|
|
139
|
+
setGuessedLetters([...guessedLetters, letter]);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
useEffect(()=> newWord(), [])
|
|
144
|
+
useEffect(()=> {
|
|
145
|
+
setGuessedLetters([]);
|
|
146
|
+
setFailedAttempts(0);
|
|
147
|
+
setHasEnded(false);
|
|
148
|
+
setHasWon(false);
|
|
149
|
+
}, [word]);
|
|
150
|
+
|
|
151
|
+
useEffect(()=> {
|
|
152
|
+
if(failedAttempts >= MAX_FAILED_ATTEMPTS)
|
|
153
|
+
setHasEnded(true);
|
|
154
|
+
}, [failedAttempts]);
|
|
155
|
+
|
|
156
|
+
useEffect(()=> {
|
|
157
|
+
if(word && !censorWord(word).includes("_")) {
|
|
158
|
+
setHasWon(true);
|
|
159
|
+
setHasEnded(true);
|
|
160
|
+
}
|
|
161
|
+
}, [guessedLetters]);
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
newWord,
|
|
165
|
+
hasEnded,
|
|
166
|
+
hasWon,
|
|
167
|
+
failedAttempts,
|
|
168
|
+
guessLetter,
|
|
169
|
+
guessedLetters,
|
|
170
|
+
word : hasEnded ? word : censorWord(word)
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
export default useGame;
|
package/src/index.js
ADDED