Skip to content

Commit 2c0c57c

Browse files
committed
rolling and holding the dice
1 parent 98ef290 commit 2c0c57c

File tree

7 files changed

+115
-38
lines changed

7 files changed

+115
-38
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
5+
<link rel="icon" type="image/svg+xml" href="./src/images/dice.png" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<link href="https://fonts.googleapis.com/css2?family=Inter&family=Karla:wght@700&display=swap" rel="stylesheet">
88
<title>Tenzies Game</title>

package-lock.json

Lines changed: 26 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13+
"nanoid": "^5.0.7",
14+
"prop-types": "^15.8.1",
1315
"react": "^18.2.0",
1416
"react-dom": "^18.2.0"
1517
},

src/App.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ p {
4141
.box,
4242
h2 {
4343
border-radius: 3.81px;
44-
background-color: #FFFFFF;
4544
color: #2B283A;
4645
width: 35.84px;
4746
height: 35.84px;
@@ -79,4 +78,11 @@ button {
7978
button:hover {
8079
background-color: #4630d3;
8180
cursor: pointer;
81+
}
82+
button:focus{
83+
outline: none;
84+
}
85+
86+
button:active {
87+
box-shadow: inset 5px 5px 10px -3px rgba(0, 0, 0, 0.7);
8288
}

src/App.jsx

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
1-
import React from 'react'
2-
import Dice from './components/dice'
3-
import './App.css'
1+
import React from "react";
2+
import Dice from "./components/dice";
3+
import "./App.css";
4+
import { nanoid } from "nanoid";
45

5-
function App() {
6+
export default function App() {
7+
const [dices, setDices] = React.useState(allNewDice());
8+
9+
function generateNewDice() {
10+
return {
11+
value: Math.floor(Math.random() * 6 + 1),
12+
isHeld: false,
13+
id: nanoid(),
14+
};
15+
}
16+
function rollDice() {
17+
setDices((prevDices) =>
18+
prevDices.map((dice) => {
19+
return !dice.isHeld ? generateNewDice() : dice;
20+
})
21+
);
22+
}
23+
24+
function holdDice(id) {
25+
setDices((prevDices) => {
26+
return prevDices.map((dice) => {
27+
return dice.id === id ? { ...dice, isHeld: !dice.isHeld } : dice;
28+
});
29+
});
30+
}
31+
32+
function allNewDice() {
33+
let newDice = [];
34+
for (let i = 0; i < 10; i++) {
35+
newDice.push(generateNewDice());
36+
}
37+
return newDice;
38+
}
39+
const diceElements = dices.map((dice) => (
40+
<Dice
41+
value={dice.value}
42+
key={dice.id}
43+
isHeld={dice.isHeld}
44+
holdDice={() => holdDice(dice.id)}
45+
/>
46+
));
647

748
return (
849
<main>
950
<h1>Tenzies</h1>
1051
<div className="description">
11-
<p>Roll until all dice are the same. Click each die to freeze it at its current value between rolls.</p>
52+
<p>
53+
Roll until all dice are the same. Click each die to freeze it at its
54+
current value between rolls.
55+
</p>
1256
</div>
1357
<div className="boxes">
14-
<Dice value="1"/>
15-
<Dice value="2"/>
16-
<Dice value="3"/>
17-
<Dice value="4"/>
18-
<Dice value="5"/>
19-
<Dice value="6"/>
20-
<Dice value="1"/>
21-
<Dice value="1"/>
22-
<Dice value="1"/>
23-
<Dice value="1"/>
58+
{diceElements}
59+
{/* <Dice value={1} />
60+
<Dice value={2} />
61+
<Dice value={3} />
62+
<Dice value={4} />
63+
<Dice value={5} />
64+
<Dice value={6} />
65+
<Dice value={1} />
66+
<Dice value={1} />
67+
<Dice value={1} />
68+
<Dice value={1} /> */}
2469
</div>
25-
<button>Roll</button>
70+
<button onClick={rollDice}>Roll</button>
2671
</main>
27-
)
72+
);
2873
}
29-
30-
export default App

src/components/dice.jsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
export default function Dice(props){
2-
return(
3-
<div className="box">
4-
<h2>{props.value}</h2>
5-
</div>
6-
)
7-
}
1+
import PropTypes from "prop-types";
2+
export default function Dice(props) {
3+
const styles = {
4+
backgroundColor: props.isHeld ? "#59E391" : "#FFFFFF",
5+
};
6+
7+
return (
8+
<div style={styles} className="box" onClick={props.holdDice}>
9+
<h2>{props.value}</h2>
10+
</div>
11+
);
12+
}
13+
Dice.propTypes = {
14+
value: PropTypes.number,
15+
isHeld: PropTypes.any,
16+
holdDice: PropTypes.func,
17+
};

src/images/dice.png

133 KB
Loading

0 commit comments

Comments
 (0)