Skip to content

Commit c52b85f

Browse files
committed
tenzies game added react js project
1 parent a977ade commit c52b85f

File tree

23 files changed

+1876
-0
lines changed

23 files changed

+1876
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.jsx"></script>
12+
</body>
13+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "tenzies-game",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"nanoid": "^4.0.0",
13+
"react": "^18.2.0",
14+
"react-confetti": "^6.1.0",
15+
"react-dom": "^18.2.0"
16+
},
17+
"devDependencies": {
18+
"@types/react": "^18.0.26",
19+
"@types/react-dom": "^18.0.9",
20+
"@vitejs/plugin-react": "^3.0.0",
21+
"vite": "^4.0.0"
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.container {
2+
max-width: 400px;
3+
height: auto;
4+
background-color: rgb(48, 48, 48);
5+
margin: 21px auto;
6+
border-radius: 12px;
7+
padding: 12px;
8+
box-sizing: border-box;
9+
font-family: monospace;
10+
box-shadow: 0px 0px 9px 3px gray;
11+
display: flex;
12+
flex-wrap: wrap;
13+
align-items: center;
14+
justify-content: center;
15+
user-select: none;
16+
}
17+
.dice {
18+
width: 50px;
19+
height: 50px;
20+
border-radius: 6px;
21+
background-color: white;
22+
color: rgb(42, 42, 42);
23+
display: flex;
24+
align-items: center;
25+
justify-content: center;
26+
font-size: 18px;
27+
margin: 9px;
28+
}
29+
.btn{
30+
max-width: 400px;
31+
padding: 12px 80px;
32+
}
33+
.btnroll{
34+
padding: 12px 50px;
35+
font-family: monospace;
36+
font-size: 18px;
37+
border-radius: 9px;
38+
color: white;
39+
border: none;
40+
background-color: rgb(255, 0, 132);
41+
box-shadow: 3px 3px white;
42+
transition: .2s;
43+
}
44+
.btnroll:active{
45+
transform: translate(3px, 3px);
46+
box-shadow: 0px 0px;
47+
}
48+
.head{
49+
font-size: 27px;
50+
color: white;
51+
font-weight: bold;
52+
text-align: center;
53+
margin-bottom: 20px;
54+
margin-top: 10px;
55+
}
56+
.sub{
57+
color: rgba(255, 255, 255, 0.652);
58+
text-align: center;
59+
60+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React from "react";
2+
import "./App.css";
3+
import Dice from "./Dice";
4+
import { nanoid } from "nanoid";
5+
import Confetti from "react-confetti";
6+
7+
export default function App() {
8+
const refreshPage = () => {
9+
window.location.reload();
10+
};
11+
const [dice, setDice] = React.useState(generateDice());
12+
const [tanzies, setTanzies] = React.useState(false);
13+
14+
React.useEffect(() => {
15+
const taptruth = dice.every((dice) => dice.tapon);
16+
if (taptruth) {
17+
setTanzies(true);
18+
console.log("You have won the game");
19+
}
20+
}, [dice]);
21+
22+
function useDieObjectData() {
23+
return {
24+
diceNum: Math.floor(Math.random() * 10),
25+
tapon: false,
26+
id: nanoid(),
27+
};
28+
}
29+
function generateDice() {
30+
const newDice = [];
31+
for (let i = 0; i < 10; i++) {
32+
newDice.push(useDieObjectData());
33+
}
34+
// console.log(newDice);
35+
return newDice;
36+
}
37+
38+
function holdDiceValue(id) {
39+
setDice((prevDice) =>
40+
prevDice.map((dice) => {
41+
return dice.id == id ? { ...dice, tapon: !dice.tapon } : dice;
42+
})
43+
);
44+
}
45+
function generateNewDice() {
46+
setDice((oldDice) =>
47+
oldDice.map((olDice) => {
48+
return olDice.tapon ? olDice : useDieObjectData();
49+
})
50+
);
51+
}
52+
function replaygame() {
53+
setTanzies(false);
54+
// event.preventDefault()
55+
dice.tapon === false;
56+
}
57+
58+
const showDice = dice.map((diceData) => {
59+
return (
60+
<Dice
61+
value={diceData.diceNum}
62+
key={diceData.id}
63+
tapon={diceData.tapon}
64+
holdDiceValue={() => holdDiceValue(diceData.id)}
65+
/>
66+
);
67+
});
68+
69+
return (
70+
<div className="container">
71+
{tanzies && <Confetti />}
72+
<div className="textcontainer">
73+
<p className="head">Tenzies</p>
74+
<p className="sub">
75+
Roll until all dice are same. Click each dice to freeze it at its
76+
current value between rolls.
77+
</p>
78+
</div>
79+
{showDice}
80+
<div className="btn">
81+
82+
{tanzies ? (
83+
<button className="btnroll" onClick={refreshPage}>
84+
Refresh
85+
</button>
86+
) : (
87+
<button className="btnroll" onClick={generateNewDice}>
88+
Roll
89+
</button>
90+
)}
91+
</div>
92+
</div>
93+
);
94+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
3+
export default function Dice(props) {
4+
const bgcolor = {
5+
backgroundColor: props.tapon ? "rgb(0, 221, 255)" : "white"
6+
}
7+
return(
8+
<div className="dice" style={bgcolor} onClick={props.holdDiceValue}>
9+
<h2>{props.value}</h2>
10+
</div>
11+
)
12+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom/client'
3+
import App from './App'
4+
5+
ReactDOM.createRoot(document.getElementById('root')).render(
6+
<React.StrictMode>
7+
<App />
8+
</React.StrictMode>,
9+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [react()],
7+
})

0 commit comments

Comments
 (0)