-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from Nishitbaria/main
Created Tenzies Game
- Loading branch information
Showing
10 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from "react"; | ||
import Die from "./Die"; | ||
import { nanoid } from "nanoid"; | ||
import Confetti from "react-confetti"; | ||
import "./style.css"; | ||
export default 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 (allHeld && allSameValue) { | ||
setTenzies(true); | ||
} | ||
}, [dice]); | ||
|
||
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(); | ||
}) | ||
); | ||
} else { | ||
setTenzies(false); | ||
setDice(allNewDice()); | ||
} | ||
} | ||
|
||
function holdDice(id) { | ||
setDice((oldDice) => | ||
oldDice.map((die) => { | ||
return die.id === id ? { ...die, 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 />} | ||
<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> | ||
<button className="roll-dice" onClick={rollDice}> | ||
{tenzies ? "New Game" : "Roll"} | ||
</button> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from "react" | ||
|
||
export default function Die(props) { | ||
const styles = { | ||
backgroundColor: props.isHeld ? "#59E391" : "white" | ||
} | ||
return ( | ||
<div | ||
className="die-face" | ||
style={styles} | ||
onClick={props.holdDice} | ||
> | ||
<h2 className="die-num">{props.value}</h2> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# co82b48eab99c792f6b884857 | ||
|
||
Quick start: | ||
|
||
``` | ||
$ yarn # npm install | ||
$ yarn build # npm run build | ||
```` | ||
|
||
## Development | ||
|
||
Run Webpack in watch-mode to continually compile the JavaScript as you work: | ||
|
||
``` | ||
$ yarn watch # npm run watch | ||
``` | ||
|
||
## About Scrimba | ||
|
||
At Scrimba our goal is to create the best possible coding school at the cost of a gym membership! 💜 | ||
If we succeed with this, it will give anyone who wants to become a software developer a realistic shot at succeeding, regardless of where they live and the size of their wallets 🎉 | ||
The Frontend Developer Career Path aims to teach you everything you need to become a Junior Developer, or you could take a deep-dive with one of our advanced courses 🚀 | ||
|
||
- [Our courses](https://scrimba.com/allcourses) | ||
- [The Frontend Career Path](https://scrimba.com/learn/frontend) | ||
- [Become a Scrimba Pro member](https://scrimba.com/pricing) | ||
|
||
Happy Coding! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css"> | ||
<link rel="preconnect" href="https://fonts.googleapis.com"> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
<link href="https://fonts.googleapis.com/css2?family=Inter&family=Karla:wght@700&display=swap" rel="stylesheet"> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="index.pack.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import App from "./App" | ||
|
||
ReactDOM.render(<App />, document.getElementById("root")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "project", | ||
"dependencies": { | ||
"react": "17.0.2", | ||
"react-dom": "17.0.2", | ||
"nanoid": "3.1.28", | ||
"react-confetti": "6.0.1" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^2.0", | ||
"babel-core": "^6.0", | ||
"babel-loader": "^7.0", | ||
"babel-preset-env": "*", | ||
"babel-preset-react": "*" | ||
}, | ||
"scripts": { | ||
"build": "webpack", | ||
"watch": "webpack -w" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
background-color: #0B2434; | ||
padding: 20px; | ||
font-family: 'Karla', sans-serif; | ||
} | ||
|
||
main { | ||
background-color: #F5F5F5; | ||
height: 400px; | ||
max-width: 800px; | ||
border-radius: 5px; | ||
padding: 20px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-around; | ||
align-items: center; | ||
} | ||
|
||
.title { | ||
font-size: 40px; | ||
margin: 0; | ||
} | ||
|
||
.instructions { | ||
font-family: 'Inter', sans-serif; | ||
font-weight: 400; | ||
margin-top: 0; | ||
text-align: center; | ||
} | ||
|
||
.dice-container { | ||
display: grid; | ||
grid-template: auto auto / repeat(5, 1fr); | ||
gap: 20px; | ||
margin-bottom: 40px; | ||
} | ||
|
||
.die-face { | ||
height: 50px; | ||
width: 50px; | ||
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.15); | ||
border-radius: 10px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
} | ||
|
||
.die-num { | ||
font-size: 2rem; | ||
} | ||
|
||
.roll-dice { | ||
height: 50px; | ||
width: 150px; | ||
border: none; | ||
border-radius: 6px; | ||
background-color: #5035FF; | ||
color: white; | ||
font-size: 1.2rem; | ||
font-family: 'Karla', sans-serif; | ||
cursor: pointer; | ||
} | ||
|
||
.roll-dice:focus { | ||
outline: none; | ||
} | ||
|
||
.roll-dice:active { | ||
box-shadow: inset 5px 5px 10px -3px rgba(0, 0, 0, 0.7); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
"output": { | ||
"filename": "[name].pack.js" | ||
}, | ||
"module": { | ||
"rules": [ | ||
{ | ||
"use": { | ||
"loader": "babel-loader", | ||
"options": { | ||
"presets": [ | ||
"babel-preset-env", | ||
"babel-preset-react" | ||
] | ||
} | ||
}, | ||
"exclude": /node_modules/, | ||
"test": /\.js$/ | ||
} | ||
] | ||
}, | ||
"entry": { | ||
"index": "./index" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters