-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
87 lines (78 loc) · 2.01 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React from "react";
import ReactDOM from "react-dom/client";
import Dice from "./Components/Dice";
import { nanoid } from "nanoid";
import Confetti from "react-confetti";
function App() {
const [dice, setDice] = React.useState(allNeWDice());
const [tenzies, setTenzies] = React.useState(false);
React.useEffect(() => {
const allHeld = dice.every((die) => die.isHeld);
const firstValue = dice[0].value;
const allSameValue = dice.every((die) => die.value === firstValue);
if (allSameValue && allHeld) {
setTenzies(true);
console.log("you won!");
}
}, [dice]);
function generateNewDice() {
return {
value: Math.floor(Math.random() * 6) + 1,
isHeld: false,
id: nanoid(),
};
}
function allNeWDice() {
let newDice = [];
for (let i = 0; i < 10; i++) {
newDice.push(generateNewDice());
}
return newDice;
}
function rollDice() {
if (!tenzies) {
setDice((oldDice) =>
oldDice.map((die) => {
return die.isHeld ? die : generateNewDice();
})
);
} else {
setDice(allNeWDice());
}
}
function holdDice(id) {
setDice((oldDice) =>
oldDice.map((die) => {
return die.id === id
? {
...die,
isHeld: !die.isHeld,
}
: die;
})
);
}
const diceElement = dice.map((die) => (
<Dice
key={die.id}
value={die.value}
isHeld={die.isHeld}
handleClick={() => holdDice(die.id)}
/>
));
return (
<main>
{tenzies && <Confetti />}
<h1 className="title">Tenzies</h1>
<p className="instructions">
Roll until all dice are the same. Click each die to freeze it at its
current value between rolls.
</p>
<div className="dice-container">{diceElement}</div>
<button className="btn" onClick={rollDice}>
{tenzies ? "New Game" : "Roll"}
</button>
</main>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);