-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
130 lines (114 loc) · 3.63 KB
/
App.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React from "react"
import Die from "./Die"
import {nanoid} from "nanoid"
import Confetti from "react-confetti"
export default function App() {
const [dice, setDice] = React.useState(allNewDice())
const [tenzies, setTenzies] = React.useState(false)
const [count, setCount] = React.useState(0)
const [timer, setTimer] = React.useState(0)
React.useEffect(() => {
const allHeld = dice.every(die => die.isHeld)
const firstValue = dice[0].value
const allSameValue = dice.every(die => die.value === firstValue)
if (allHeld && allSameValue) {
setTenzies(true)
}
}, [dice])
function startTimer() {
const intervalId = setInterval(() => {
setTimer(prevTimer => prevTimer + 1);
}, 1000); // Update the timer every 1000 milliseconds (1 second)
// Save the interval ID to stop the timer when needed
return intervalId;
}
function stopTimer(intervalId) {
clearInterval(intervalId);
}
React.useEffect(() => {
if (!tenzies) {
// start the timer when the game starts
const intervalId = startTimer();
// cleanup function to stop the timer when the game ends
return () => stopTimer(intervalId);
}
}, [tenzies]);
function generateNewDie() {
return {
value: Math.ceil(Math.random() * 6),
isHeld: false,
id: nanoid()
}
}
function allNewDice() {
const newDice = []
for (let i = 0; i < 10; i++) {
newDice.push(generateNewDie())
}
return newDice
}
function rollDice() {
if(!tenzies) {
setDice(oldDice => oldDice.map(die => {
return die.isHeld ?
die :
generateNewDie()
}))
setCount(prevCount => prevCount + 1)
console.log(count)
} else {
setTenzies(false)
setDice(allNewDice())
setCount(0)
}
}
function holdDice(id) {
setDice(oldDice => oldDice.map(die => {
return die.id === id ?
{
value: die.value,
id: die.id,
isHeld: !die.isHeld
} :
die
}))
}
const diceElements = dice.map(die => (
<Die
key={die.id}
value={die.value}
isHeld={die.isHeld}
holdDice={() => holdDice(die.id)}
/>
))
return (
<main>
{tenzies && <Confetti
width={window.innerWidth}
height={window.innerHeight}
/>}
<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">
{diceElements}
</div>
<div className= "footer-container">
<div className="count-container">
<h4>Roll Count</h4>
<p>{count}</p>
</div>
<button
className="roll-dice"
onClick={rollDice}
>
{tenzies ? "New Game" : "Roll"}
</button>
<div className="time-container">
<h4>Timer</h4>
<p>{timer}</p>
</div>
</div>
</main>
)
}