GameScreen.js
3.53 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
import { useState, useEffect } from "react";
import { View, StyleSheet, Alert, Text, FlatList } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import NumberContainer from "../components/game/NumberContainer";
import Card from "../components/ui/Card";
import InstructionText from "../components/ui/InstructionText";
import PrimaryButton from "../components/ui/PrimaryButton";
import Title from "../components/ui/Title";
import GuessLogItem from "../components/game/GuessLogItem";
function generateRandomBetween(min, max, exclude) {
const rndNum = Math.floor(Math.random() * (max - min)) + min;
if (rndNum === exclude) {
return generateRandomBetween(min, max, exclude);
} else {
return rndNum;
}
}
let minBoundary = 1;
let maxBoundary = 100;
function GameScreen({ userNumber, onGameOver }) {
const initialGuess = generateRandomBetween(1, 100, userNumber);
const [currentGuess, setCurrentGuess] = useState(initialGuess);
const [guessRounds, setGuessRounds] = useState([initialGuess]);
useEffect(() => {
if (currentGuess === userNumber) {
onGameOver(guessRounds.length);
}
}, [currentGuess, userNumber, onGameOver]);
useEffect(() => {
minBoundary = 1;
maxBoundary = 100;
}, []);
function nextGuessHandler(direction) {
// direction => 'lower', 'greater'
if (
(direction === "lower" && currentGuess < userNumber) ||
(direction === "greater" && currentGuess > userNumber)
) {
Alert.alert("Don't lie!", "You know that this is wrong...", [
{ text: "Sorry!", style: "cancel" },
]);
return;
}
if (direction === "lower") {
maxBoundary = currentGuess;
} else {
minBoundary = currentGuess + 1;
}
const newRndNumber = generateRandomBetween(
minBoundary,
maxBoundary,
currentGuess
);
setCurrentGuess(newRndNumber);
setGuessRounds((prevGuessRounds) => [newRndNumber, ...prevGuessRounds]);
}
const guessRoundsListLength = guessRounds.length;
return (
<View style={styles.screen}>
<Title>Opponent's Guess</Title>
<NumberContainer>{currentGuess}</NumberContainer>
<Card>
<InstructionText style={styles.instructionText}>
Higher or lower?
</InstructionText>
<View style={styles.buttonsContainer}>
<View style={styles.buttonContainer}>
<PrimaryButton onPress={nextGuessHandler.bind(this, "lower")}>
<Ionicons name="md-remove" size={24} color="white" />
</PrimaryButton>
</View>
<View style={styles.buttonContainer}>
<PrimaryButton onPress={nextGuessHandler.bind(this, "greater")}>
<Ionicons name="md-add" size={24} color="white" />
</PrimaryButton>
</View>
</View>
</Card>
<View style={styles.listContainer}>
{/* {guessRounds.map(guessRound => <Text key={guessRound}>{guessRound}</Text>)} */}
<FlatList
data={guessRounds}
renderItem={(itemData) => (
<GuessLogItem
roundNumber={guessRoundsListLength - itemData.index}
guess={itemData.item}
/>
)}
keyExtractor={(item) => item}
/>
</View>
</View>
);
}
export default GameScreen;
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 24,
},
instructionText: {
marginBottom: 12,
},
buttonsContainer: {
flexDirection: "row",
},
buttonContainer: {
flex: 1,
},
listContainer: {
flex: 1,
padding: 16,
},
});