-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
337 lines (318 loc) · 7.69 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { StatusBar } from "expo-status-bar";
import { useEffect, useRef, useState } from "react";
import {
Dimensions,
Pressable,
StyleSheet,
Text,
View,
useColorScheme,
Animated,
} from "react-native";
const { width, height } = Dimensions.get("screen");
// Theme Colors
const LIGHT = "#f3f2e5";
const DARK = "#242c40";
// PIECES
const X = "❌";
const O = "⭕";
const bottomBorderIndecies = [0, 1, 2, 3, 4, 5];
const rightBorderIndecies = [0, 1, 3, 4, 6, 7];
// Individual Square Component
function Square({ value, onSquareClick, index }) {
const colorScheme = useColorScheme();
const themeTextStyle =
colorScheme === "light" ? styles.lightThemeText : styles.darkThemeText;
const themeBorderStyle =
colorScheme === "light" ? styles.lightBorder : styles.darkBorder;
let squareBorderStyle = [styles.square];
if (bottomBorderIndecies.includes(index)) {
squareBorderStyle.push(styles.bottomBorder);
}
if (rightBorderIndecies.includes(index)) {
squareBorderStyle.push(styles.rightBorder);
}
return (
<Pressable
onPress={onSquareClick}
style={[...squareBorderStyle, themeBorderStyle]}
android_disableSound
>
<Text style={[styles.xo, themeTextStyle]}>{value}</Text>
</Pressable>
);
}
// Board Component
function Board({ xIsNext, squares, onPlay, playerX, winner, reset }) {
const colorScheme = useColorScheme();
const themeTextStyle =
colorScheme === "light" ? styles.lightThemeText : styles.darkThemeText;
const fadeAnim = useRef(new Animated.Value(1)).current; // Start opacity at full
useEffect(() => {
if ((playerX && xIsNext) || (!playerX && !xIsNext) || winner) {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}).start();
} else {
Animated.timing(fadeAnim, {
toValue: 0.5,
duration: 500,
useNativeDriver: true,
}).start();
}
}, [fadeAnim, playerX, xIsNext, winner]);
function handleClick(i) {
if ((xIsNext && playerX) || (!xIsNext && !playerX)) {
// Disable if there's a winner or if the square is already filled
if (winner || squares[i]) {
return;
}
// Update state
const nextSquares = squares.slice();
if (xIsNext) {
nextSquares[i] = X;
} else {
nextSquares[i] = O;
}
onPlay(nextSquares);
}
}
let status;
if (winner) {
if (winner === "tie") {
status = "Tie!";
} else if ((playerX && winner == X) || (!playerX && winner == O)) {
status = "You Won!";
} else {
status = "You Lost!";
}
} else {
if ((playerX && xIsNext) || (!playerX && !xIsNext)) {
status = "Your Turn";
} else {
status = " ";
}
}
return (
<Animated.View
style={[
styles.board,
playerX && styles.flip,
{
opacity: fadeAnim,
},
]}
>
<View style={{ height: 50 }}>
{winner && (
<Pressable onPress={reset} style={styles.playAgain}>
<Text adjustsFontSizeToFit style={styles.playAgainText}>
Play Again
</Text>
</Pressable>
)}
</View>
<Text style={[styles.statusText, themeTextStyle]}>{status}</Text>
<View>
<View style={styles.boardRow}>
<Square
value={squares[0]}
onSquareClick={() => handleClick(0)}
index={0}
/>
<Square
value={squares[1]}
onSquareClick={() => handleClick(1)}
index={1}
/>
<Square
value={squares[2]}
onSquareClick={() => handleClick(2)}
index={2}
/>
</View>
<View style={styles.boardRow}>
<Square
value={squares[3]}
onSquareClick={() => handleClick(3)}
index={3}
/>
<Square
value={squares[4]}
onSquareClick={() => handleClick(4)}
index={4}
/>
<Square
value={squares[5]}
onSquareClick={() => handleClick(5)}
index={5}
/>
</View>
<View style={styles.boardRow}>
<Square
value={squares[6]}
onSquareClick={() => handleClick(6)}
index={6}
/>
<Square
value={squares[7]}
onSquareClick={() => handleClick(7)}
index={7}
/>
<Square
value={squares[8]}
onSquareClick={() => handleClick(8)}
index={8}
/>
</View>
</View>
</Animated.View>
);
}
export default function App() {
const colorScheme = useColorScheme();
const themeContainerStyle =
colorScheme === "light" ? styles.lightContainer : styles.darkContainer;
const [history, setHistory] = useState([Array(9).fill(null)]);
const [currentMove, setCurrentMove] = useState(0);
const xIsNext = currentMove % 2 === 0;
const currentSquares = history[currentMove];
// Manage game state
function handlePlay(nextSquares) {
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
}
const winner = calculateWinner(currentSquares);
return (
<View style={[styles.container, themeContainerStyle]}>
<Board
xIsNext={xIsNext}
squares={currentSquares}
onPlay={handlePlay}
playerX={true}
winner={winner}
reset={() => setCurrentMove(0)}
/>
<Board
xIsNext={xIsNext}
squares={currentSquares}
onPlay={handlePlay}
playerX={false}
winner={winner}
reset={() => setCurrentMove(0)}
/>
<StatusBar style="auto" />
</View>
);
}
function calculateWinner(squares) {
// All possible win cases
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
// Calculate winner
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
// See if any case contains the same piece (X or O)
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
// Calculate tie game
let full = true;
for (let i = 0; i < squares.length; i++) {
if (squares[i] === null) {
full = false;
}
}
if (full) {
return "tie";
}
return null;
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
paddingVertical: "20%",
},
lightContainer: {
backgroundColor: LIGHT,
},
darkContainer: {
backgroundColor: DARK,
},
lightThemeText: {
color: DARK,
},
darkThemeText: {
color: LIGHT,
},
lightBorder: {
borderColor: DARK,
},
darkBorder: {
borderColor: LIGHT,
},
board: {
flex: 1,
width: width,
padding: 10,
gap: 10,
alignItems: "center",
borderTopWidth: 1,
borderTopColor: "#aaa",
},
flip: {
transform: [{ rotate: "180deg" }],
},
boardRow: {
flexDirection: "row",
},
square: {
height: width * 0.2,
width: width * 0.2,
padding: 0,
alignItems: "center",
justifyContent: "center",
},
bottomBorder: {
borderBottomWidth: 2,
},
rightBorder: {
borderRightWidth: 2,
},
xo: {
fontSize: 40,
fontWeight: "bold",
},
statusText: {
fontSize: 24,
fontWeight: "bold",
},
playAgain: {
flex: 1,
alignItems: "center",
justifyContent: "center",
paddingHorizontal: 40,
paddingVertical: 10,
backgroundColor: "#67f",
borderRadius: 5,
},
playAgainText: {
fontSize: 24,
fontWeight: "bold",
color: LIGHT,
},
});