-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
204 lines (192 loc) · 5.01 KB
/
Copy pathApp.js
File metadata and controls
204 lines (192 loc) · 5.01 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, {useState} from 'react';
import {StyleSheet, View, TouchableOpacity} from 'react-native';
import {
Text,
Header,
Body,
Container,
H1,
H3,
Button,
Title,
Content,
Card,
} from 'native-base';
import Icons from './components/Icons';
import Snackbar from 'react-native-snackbar';
const itemArray = new Array(9).fill('empty');
const App = () => {
const [isCross, setIsCross] = useState(false);
const [winMessage, setWinMessage] = useState('');
const changeItem = itemNumber => {
if (winMessage) {
return Snackbar.show({
text: 'Current Match Is Over',
backgroundColor: '#000000',
textColor: '#ffffff',
});
}
if (itemArray[itemNumber] === 'empty') {
itemArray[itemNumber] = isCross ? 'cross' : 'circle';
setIsCross(!isCross);
} else {
return Snackbar.show({
text: 'Card is already filled',
backgroundColor: '#000000',
textColor: '#ffffff',
});
}
let noOfEmptyCard = 0;
for (let index = 0; index < itemArray.length; index++) {
const item = itemArray[index];
if (item === 'empty') {
noOfEmptyCard += 1;
}
}
if (noOfEmptyCard === 0) {
setWinMessage('Match is Draw!');
return Snackbar.show({
text: 'Match is Draw!!!',
backgroundColor: '#000000',
textColor: '#ffffff',
});
}
checkWinner();
};
const resetGame = () => {
setIsCross(false);
setWinMessage('');
for (let index = 0; index < itemArray.length; index++) {
itemArray[index] = 'empty';
}
};
const checkWinner = () => {
if (
itemArray[0] !== 'empty' &&
itemArray[0] === itemArray[1] &&
itemArray[1] === itemArray[2]
) {
setWinMessage(`${itemArray[0]} Won the match`);
} else if (
itemArray[3] !== 'empty' &&
itemArray[3] === itemArray[4] &&
itemArray[4] === itemArray[5]
) {
setWinMessage(`${itemArray[3]} Won the match`);
} else if (
itemArray[6] !== 'empty' &&
itemArray[6] === itemArray[7] &&
itemArray[7] === itemArray[8]
) {
setWinMessage(`${itemArray[6]} Won the match`);
} else if (
itemArray[0] !== 'empty' &&
itemArray[0] === itemArray[3] &&
itemArray[3] === itemArray[6]
) {
setWinMessage(`${itemArray[0]} Won the match`);
} else if (
itemArray[1] !== 'empty' &&
itemArray[1] === itemArray[4] &&
itemArray[4] === itemArray[7]
) {
setWinMessage(`${itemArray[1]} Won the match`);
} else if (
itemArray[2] !== 'empty' &&
itemArray[2] === itemArray[5] &&
itemArray[5] === itemArray[8]
) {
setWinMessage(`${itemArray[2]} Won the match`);
} else if (
itemArray[0] !== 'empty' &&
itemArray[0] === itemArray[4] &&
itemArray[4] === itemArray[8]
) {
setWinMessage(`${itemArray[0]} Won the match`);
} else if (
itemArray[2] !== 'empty' &&
itemArray[2] === itemArray[4] &&
itemArray[4] === itemArray[6]
) {
setWinMessage(`${itemArray[2]} Won the match`);
}
};
return (
<>
<Container>
<Header style={{backgroundColor: '#000'}}>
<Body style={{ alignItems: "center" }}>
<Title style={{ fontSize: 30 }}>Tic Tac Toe</Title>
</Body>
</Header>
<Content>
<View style={styles.grid}>
{itemArray.map((item, index) => {
return (
<TouchableOpacity
onPress={() => {
console.log('Card Pressed');
changeItem(index);
}}
key={index}
style={styles.box}>
<Card style={styles.myIcon}>
<Icons name={item} />
</Card>
</TouchableOpacity>
);
})}
</View>
{winMessage ? (
<View>
<H1 style={styles.message}>{winMessage}</H1>
<Button
primary
rounded
last
onPress={() => {
resetGame();
}}
style={{
width: '90%',
justifyContent: 'center',
marginHorizontal: '5%',
}}>
<Text>Reload Game</Text>
</Button>
</View>
) : (
<H3 style={styles.message}>
{`${isCross ? 'Cross' : 'Circle'}`} Turn
</H3>
)}
</Content>
</Container>
</>
);
};
export default App;
const styles = StyleSheet.create({
grid: {
flexGrow: 1,
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: 15,
},
box: {
width: '33%',
marginBottom: 6,
},
myIcon: {
height: 120,
alignItems: 'center',
justifyContent: 'center',
},
message: {
backgroundColor: 'lightblue',
paddingVertical: 10,
marginBottom: 6,
textAlign: 'center',
textTransform: 'uppercase',
},
});