-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
197 lines (180 loc) · 5.2 KB
/
App.js
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
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View, Alert } from 'react-native';
import { useEffect, useState } from 'react';
export default function App() {
const [score, setScore] = useState({
'X': 0,
'O': 0,
});
const winConditions = [
['top1', 'top2', 'top3'],
['mid1', 'mid2', 'mid3'],
['bot1', 'bot2', 'bot3'],
['top1', 'mid1', 'bot1'],
['top2', 'mid2', 'bot2'],
['top3', 'mid3', 'bot3'],
['top1', 'mid2', 'bot3'],
['top3', 'mid2', 'bot1'],
];
const [board, setBoard] = useState({
top1: '',
top2: '',
top3: '',
mid1: '',
mid2: '',
mid3: '',
bot1: '',
bot2: '',
bot3: '',
});
const [turn, setTurn] = useState('X');
const [occupiedTiles, setOccupiedTiles] = useState([]);
useEffect(() => {
setTimeout(() => {
checkWin();
}, 300);
}, [board]);
function checkWin() {
for (let i = 0; i < winConditions.length; i++) {
const [a, b, c] = winConditions[i];
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
const winner = board[a];
setScore({ ...score, [winner]: score[winner] + 1 });
Alert.alert(`${board[a]} wins!`, "Ready for revenge? \nYou have no choice anyway 😀", [
{
text: 'New Game',
onPress: () => {
setBoard({
top1: '',
top2: '',
top3: '',
mid1: '',
mid2: '',
mid3: '',
bot1: '',
bot2: '',
bot3: '',
});
setOccupiedTiles([]);
setTurn('X');
},
}]);
return;
} else if (occupiedTiles.length === 9) {
Alert.alert(`It's a tie!`, "Ready for revenge? \nYou have no choice anyway 😀", [
{
text: 'New Game',
onPress: () => {
setBoard({
top1: '',
top2: '',
top3: '',
mid1: '',
mid2: '',
mid3: '',
bot1: '',
bot2: '',
bot3: '',
});
setOccupiedTiles([]);
setTurn('X');
},
}]);
return;
}
}
}
function onPressTile(tile) {
if (occupiedTiles.includes(tile)) { return }
setBoard({ ...board, [tile]: turn });
setOccupiedTiles([...occupiedTiles, tile]);
setTurn(turn === 'X' ? 'O' : 'X');
}
return (
<View style={styles.container}>
<Text style={styles.title}>Tic Tac Toe</Text>
<View style={styles.row}>
<View onTouchEnd={() => onPressTile('top1')} style={styles.tile}>
<Text style={styles.tileText}>{board.top1}</Text>
</View>
<View onTouchEnd={() => onPressTile('top2')} style={styles.tile}>
<Text style={styles.tileText}>{board.top2}</Text>
</View>
<View onTouchEnd={() => onPressTile('top3')} style={styles.tile}>
<Text style={styles.tileText}>{board.top3}</Text>
</View>
</View>
<View style={styles.row}>
<View onTouchEnd={() => onPressTile('mid1')} style={styles.tile}>
<Text style={styles.tileText}>{board.mid1}</Text>
</View>
<View onTouchEnd={() => onPressTile('mid2')} style={styles.tile}>
<Text style={styles.tileText}>{board.mid2}</Text>
</View>
<View onTouchEnd={() => onPressTile('mid3')} style={styles.tile}>
<Text style={styles.tileText}>{board.mid3}</Text>
</View>
</View>
<View style={styles.row}>
<View onTouchEnd={() => onPressTile('bot1')} style={styles.tile}>
<Text style={styles.tileText}>{board.bot1}</Text>
</View>
<View onTouchEnd={() => onPressTile('bot2')} style={styles.tile}>
<Text style={styles.tileText}>{board.bot2}</Text>
</View>
<View onTouchEnd={() => onPressTile('bot3')} style={styles.tile}>
<Text style={styles.tileText}>{board.bot3}</Text>
</View>
</View>
<View style={styles.statsWrapper}>
<Text style={[styles.stats, { color: '#5e548e' }]}>Next: {turn}</Text>
<View style={{ borderBottomColor: '#c8e7ff', borderBottomWidth: 1, width: 300 }}></View>
<Text style={styles.stats}>Total Wins:</Text>
<Text style={styles.stats}> X : {score.X}</Text>
<Text style={styles.stats}> O : {score.O}</Text>
</View>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#90a8c3',
alignItems: 'center',
justifyContent: 'center',
paddingTop: 100,
},
tile: {
width: 100,
height: 100,
backgroundColor: '#c8e7ff',
margin: 6,
alignItems: 'center',
justifyContent: 'center',
},
row: {
flexDirection: 'row',
},
title: {
fontSize: 50,
position: 'absolute',
top: 50,
marginTop: 20,
color: '#c8e7ff',
},
tileText: {
fontSize: 70,
color: '#4f5d75',
},
stats: {
fontSize: 30,
color: 'white',
margin: 10,
},
statsWrapper: {
paddingTop: 20,
alignItems: 'center',
justifyContent: 'center',
},
});