//GameStyle.js
import React from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
import { PanGestureHandler, State } from 'react-native-gesture-handler';
const BOARD_SIZE = 4;
const GameStyle = ({ board, handleSwipe, initializeGame }) => {
const getTileColor = (value) => {
switch (value) {
case 2:
return '#EEE4DA';
case 4:
return '#EDE0C8';
case 8:
return '#F2B179';
case 16:
return '#F59563';
case 32:
return '#F67C5F';
case 64:
return '#F65E3B';
case 128:
return '#EDCF72';
case 256:
return '#EDCC61';
case 512:
return '#EDC850';
case 1024:
return '#EDC53F';
case 2048:
return '#EDC22E';
default:
return '#BBF99A';
}
};
const onSwipeEvent = (event) => {
if (event.nativeEvent.state === State.END) {
const { translationX, translationY } = event.nativeEvent;
const dx = Math.abs(translationX);
const dy = Math.abs(translationY);
if (dx > dy) {
if (translationX > 0) {
handleSwipe('RIGHT');
} else {
handleSwipe('LEFT');
}
} else {
if (translationY > 0) {
handleSwipe('DOWN');
} else {
handleSwipe('UP');
}
}
}
};
return (
<View style={styles.container}>
<View style={styles.Heading}>
<Text style={{ fontSize: 50, fontWeight: 'bold', color: 'green' }}>
GeekforGeeks
</Text>
<Text style={{
; paddingLeft: 70,
fontSize: 30,
fontWeight: 'bold',
color: 'black'
}}>
2048 Game
</Text>
</View >
<PanGestureHandler onGestureEvent={onSwipeEvent}>
<View style={styles.board}>
{board.map((row, rowIndex) => (
<View key={rowIndex} style={styles.row}>
{row.map((tile, colIndex) => (
<View key={colIndex}
style={[styles.tile, {
backgroundColor: getTileColor(tile)
}]}>
<Text style={styles.tileText}>
{tile !== 0 ? tile : ''}
</Text>
</View>
))}
</View>
))}
</View>
</PanGestureHandler>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
Heading: {
marginBottom: 30,
marginTop: -30,
},
board: {
flexDirection: 'column',
},
row: {
flexDirection: 'row',
},
tile: {
width: Dimensions.get('window').width / BOARD_SIZE - 10,
height: Dimensions.get('window').width / BOARD_SIZE - 10,
margin: 5,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 5,
},
tileText: {
fontSize: 20,
fontWeight: 'bold',
color: '#776E65',
},
});
export default GameStyle;