forked from ibzefo/Wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.js
More file actions
106 lines (93 loc) · 2.87 KB
/
GameScreen.js
File metadata and controls
106 lines (93 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { useState } from 'react';
import { View, StyleSheet, ScrollView, TouchableOpacity, Text } from 'react-native';
const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange'];
const shuffleArray = (array) => {
const shuffledArray = [...array];
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
return shuffledArray;
};
const GameScreen = () => {
const [gridColors, setGridColors] = useState(shuffleArray([...colors, ...colors]));
const [flippedIndices, setFlippedIndices] = useState([]);
const [matchedPairs, setMatchedPairs] = useState([]);
const handleFlip = (index) => {
if (flippedIndices.length === 2) {
return; // If two tiles are already flipped, do nothing
}
if (flippedIndices.includes(index) || matchedPairs.includes(gridColors[index])) {
return; // If the tile is already flipped or part of a matched pair, do nothing
}
// Flip the selected tile
setFlippedIndices([...flippedIndices, index]);
if (flippedIndices.length === 1) {
// Check for a match when two tiles are flipped
const [firstIndex] = flippedIndices;
if (gridColors[firstIndex] === gridColors[index]) {
// Matched pair, keep them solid
setMatchedPairs([...matchedPairs, gridColors[firstIndex]]);
setFlippedIndices([]);
} else {
// Not a match, reset the tiles after a short delay
setTimeout(() => {
setFlippedIndices([]);
}, 1000); // 1 second delay
}
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>Memory Game</Text>
<ScrollView contentContainerStyle={styles.grid}>
{gridColors.map((color, index) => (
<Tile
key={index}
color={color}
isFlipped={flippedIndices.includes(index) || matchedPairs.includes(color)}
onFlip={() => handleFlip(index)}
/>
))}
</ScrollView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 36,
color: 'white',
marginBottom: 20,
fontWeight: 'bold',
},
grid: {
flexDirection: 'row',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
},
});
export default GameScreen;
const Tile = ({ color, isFlipped, onFlip }) => {
return (
<TouchableOpacity onPress={onFlip}>
<View style={[tileStyles.tile, isFlipped && tileStyles.tileFlipped, { backgroundColor: isFlipped ? color : 'gray' }]} />
</TouchableOpacity>
);
};
const tileStyles = StyleSheet.create({
tile: {
width: 80,
height: 80,
margin: 5,
backgroundColor: 'gray',
},
tileFlipped: {
backgroundColor: 'white',
},
});