-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory game #82
Memory game #82
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react' | ||
import { makeStyles } from '@material-ui/styles' | ||
import PropTypes from 'prop-types' | ||
|
||
import Card from './Card' | ||
|
||
import styles from './styles' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tetogomez this should be just one block
|
||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Board = ({ deck, pairSelected, selectCard }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<div className={classes.board}> | ||
{(deck || []).map((card, index) => { | ||
const isCardCompared = pairSelected.indexOf(card) > -1 | ||
|
||
return ( | ||
<Card | ||
key={index} | ||
image={card.image} | ||
isCardCompared={isCardCompared} | ||
selectCard={() => selectCard(card)} | ||
guessedRight={card.guessedRight} | ||
/> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
Board.propTypes = { | ||
deck: PropTypes.array, | ||
pairSelected: PropTypes.array, | ||
selectCard: PropTypes.func | ||
} | ||
|
||
export default Board |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { makeStyles } from '@material-ui/styles' | ||
import PropTypes from 'prop-types' | ||
import ReactCardFlip from 'react-card-flip' | ||
|
||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Card = ({ image, isCardCompared, selectCard, guessedRight }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<div className={classes.card} onClick={selectCard}> | ||
<ReactCardFlip | ||
isFlipped={isCardCompared || guessedRight} | ||
flipDirection="vertical" | ||
> | ||
<div className={classes.cover}></div> | ||
<div className={classes.content}> | ||
<img src={image} alt="memory-img" /> | ||
</div> | ||
</ReactCardFlip> | ||
</div> | ||
) | ||
} | ||
|
||
Card.propTypes = { | ||
image: PropTypes.any, | ||
isCardCompared: PropTypes.bool, | ||
selectCard: PropTypes.func, | ||
guessedRight: PropTypes.bool | ||
} | ||
|
||
export default Card |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { makeStyles } from '@material-ui/styles' | ||
|
||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Header = ({ attempts, resetGame }) => { | ||
const classes = useStyles() | ||
|
||
return ( | ||
<header> | ||
<div className={classes.title}>React-Memory</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe this should say "gGoods Memory" and use translations |
||
<div> | ||
<button className={classes.resetBtn} onClick={resetGame}> | ||
reset | ||
</button> | ||
</div> | ||
<div className={classes.title}>Attempts: {attempts}</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing translate |
||
</header> | ||
) | ||
} | ||
|
||
Header.propTypes = { | ||
attempts: PropTypes.number, | ||
resetGame: PropTypes.func | ||
} | ||
|
||
export default Header |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { makeStyles } from '@material-ui/styles' | ||
|
||
import { buildDeck } from '../../utils' | ||
|
||
import Header from './Header' | ||
import Board from './Board' | ||
import styles from './styles' | ||
|
||
const useStyles = makeStyles(styles) | ||
|
||
const Memory = ({ customOptions = [] }) => { | ||
const classes = useStyles() | ||
const [deck, setDeck] = useState() | ||
const [pairSelected, setPairSelected] = useState() | ||
const [pairCompared, setPairCompared] = useState() | ||
const [attempts, setAttempts] = useState() | ||
|
||
const selectCard = card => { | ||
if (pairCompared || pairSelected.indexOf(card) > -1 || card.guessedRight) { | ||
return | ||
} | ||
|
||
const newPairSelected = [...pairSelected, card] | ||
|
||
setPairSelected(newPairSelected) | ||
|
||
if (newPairSelected.length === 2) { | ||
comparePair(newPairSelected) | ||
} | ||
} | ||
|
||
const comparePair = pair => { | ||
setPairCompared(true) | ||
|
||
setTimeout(() => { | ||
const [firstCard, secondCard] = pair | ||
let newDeck = deck | ||
|
||
if (firstCard.image === secondCard.image) { | ||
newDeck = newDeck.map(card => { | ||
if (card.image !== firstCard.image) return card | ||
|
||
return { ...card, guessedRight: true } | ||
}) | ||
} | ||
|
||
verifyGame(deck) | ||
|
||
setDeck(newDeck) | ||
setPairSelected([]) | ||
setPairCompared(false) | ||
setAttempts(attempts + 1) | ||
}, 1000) | ||
} | ||
|
||
const verifyGame = deck => { | ||
if (deck.filter(card => !card.guessedRight).length === 0) { | ||
console.log(`Win with ${attempts} attempts!`) | ||
} | ||
} | ||
|
||
const resetGame = () => { | ||
setDeck(buildDeck(customOptions)) | ||
setPairSelected([]) | ||
setPairCompared(false) | ||
setAttempts(0) | ||
} | ||
|
||
useEffect(() => { | ||
resetGame() | ||
}, []) | ||
|
||
// console.log({ deck }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tetogomez remove this line |
||
|
||
return ( | ||
<div className={classes.app}> | ||
<Header attempts={attempts} resetGame={resetGame} /> | ||
<Board deck={deck} pairSelected={pairSelected} selectCard={selectCard} /> | ||
</div> | ||
) | ||
} | ||
|
||
Memory.propTypes = { | ||
customOptions: PropTypes.array | ||
} | ||
|
||
export default Memory |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export default () => ({ | ||
card: { | ||
width: 125, | ||
height: 125 | ||
}, | ||
cover: { | ||
width: 125, | ||
height: 125, | ||
backgroundColor: '#a5a1a4' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tetogomez please try to avoid static colors |
||
}, | ||
content: { | ||
width: 125, | ||
height: 125, | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
backgroundColor: 'rgb(3, 220, 244)', | ||
'& img': { | ||
width: 125, | ||
height: 125 | ||
} | ||
}, | ||
header: { | ||
height: 50, | ||
border: '1px solid black', | ||
marginBottom: 10, | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
alignContent: 'center' | ||
}, | ||
title: { | ||
fontSize: 25, | ||
padding: 10, | ||
color: '#00' | ||
}, | ||
resetBtn: { | ||
left: 12, | ||
top: 58 | ||
}, | ||
board: { | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
width: 700, | ||
height: 600, | ||
margin: '0 auto', | ||
justifyContent: 'space-around' | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing translate