Skip to content

Commit 836b594

Browse files
ZeynabZeynab
Zeynab
authored and
Zeynab
committed
memorygame
1 parent 34c08b0 commit 836b594

File tree

3 files changed

+126
-11
lines changed

3 files changed

+126
-11
lines changed

src/Memorygame.tsx

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { useEffect, useState } from 'react';
2+
import './App.css';
3+
4+
const TILE_COLORS = ['red', 'green', 'blue', 'yellow', 'Red', 'Green', 'Blue', 'Yellow'];
5+
6+
export default function MemoryGame() {
7+
const [myColors, setMyColors] = useState<any>([]);
8+
const [tiles, setTiles] = useState<any>([]);
9+
const [counter, setCounter] = useState<number>(1);
10+
const [selected, setSelected] = useState<any>([]);
11+
12+
useEffect(() => {
13+
setMyColors(TILE_COLORS);
14+
shuffle(TILE_COLORS);
15+
}, []);
16+
// Write your code here.
17+
function handleColor(item: any) {
18+
setTiles([...tiles, item]);
19+
setCounter((counter) => counter + 1);
20+
if (counter === 1) {
21+
if (tiles[0]?.toLowerCase() === item?.toLowerCase()) {
22+
setSelected([...selected, tiles[0], item]);
23+
}
24+
setTimeout(() => {
25+
setTiles([]);
26+
setCounter(0);
27+
}, 500);
28+
}
29+
}
30+
return (
31+
<>
32+
<h1>Memory</h1>
33+
<div className="board">
34+
{myColors &&
35+
myColors.map((item: string, index: number) => (
36+
<div
37+
key={index}
38+
style={{
39+
background: tiles.includes(item) ? item : selected.includes(item) ? item : 'white',
40+
}}
41+
className={`tile ${tiles.includes(item) ? item : selected.includes(item) ? item : ''}`}
42+
onClick={() => handleColor(item)}
43+
></div>
44+
))}
45+
</div>
46+
{selected.length === TILE_COLORS.length && (
47+
<>
48+
<h1>You are the Winner</h1>
49+
<button
50+
onClick={() => {
51+
setSelected([]);
52+
shuffle(myColors);
53+
setMyColors(TILE_COLORS);
54+
}}
55+
>
56+
Restart
57+
</button>
58+
</>
59+
)}
60+
</>
61+
);
62+
}
63+
64+
/**
65+
* Returns the array shuffled into a random order.
66+
* Do not edit this function.
67+
*/
68+
function shuffle(array: string[]) {
69+
for (let i = array.length - 1; i > 0; i--) {
70+
const randomIndex = Math.floor(Math.random() * (i + 1));
71+
72+
[array[i], array[randomIndex]] = [array[randomIndex], array[i]];
73+
}
74+
return array;
75+
}

src/index.css

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
1-
body {
2-
margin: 0;
3-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4-
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5-
sans-serif;
6-
-webkit-font-smoothing: antialiased;
7-
-moz-osx-font-smoothing: grayscale;
1+
#root {
2+
text-align: center;
83
}
94

10-
code {
11-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12-
monospace;
5+
h1 {
6+
margin-bottom: 0;
137
}
8+
9+
button,
10+
input[type='button'] {
11+
display: block;
12+
margin: 0 10px 10px auto;
13+
cursor: pointer;
14+
padding: 12px;
15+
font-size: 16px;
16+
border-radius: 8px;
17+
background-color: #02203c;
18+
color: white;
19+
}
20+
21+
.board {
22+
display: flex;
23+
flex-wrap: wrap;
24+
justify-content: center;
25+
gap: 10px;
26+
padding: 10px;
27+
}
28+
29+
.tile {
30+
flex-basis: 40%;
31+
border: 1px solid black;
32+
width: 20px;
33+
height: 20px;
34+
background-color: white;
35+
cursor: pointer;
36+
}
37+
38+
.blue {
39+
background-color: blue;
40+
}
41+
42+
.yellow {
43+
background-color: yellow;
44+
}
45+
46+
.red {
47+
background-color: red;
48+
}
49+
50+
.green {
51+
background-color: green;
52+
}

src/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import ReactDOM from 'react-dom/client';
33
import './index.css';
44
import App from './App';
55
import reportWebVitals from './reportWebVitals';
6+
import MemoryGame from './Memorygame';
67

78
const root = ReactDOM.createRoot(
89
document.getElementById('root') as HTMLElement
910
);
1011
root.render(
1112
<React.StrictMode>
12-
<App />
13+
<MemoryGame/>
1314
</React.StrictMode>
1415
);
1516

0 commit comments

Comments
 (0)