-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
217 lines (189 loc) · 5.57 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
import React, { Component } from 'react';
import { StyleSheet, Text, TextInput, View, Image, Button, Alert } from 'react-native';
export default class App extends React.Component {
constructor(props) {
super(props);
//This is all replicated in restartGame() because javascript isn't it?
targetNumberValue = Math.floor(Math.random() * 5) + 1;
this.state = {guess: '',
targetNumber: targetNumberValue,
clue: '',
gameOver: false,
level: 1,
rangeEnd: 5,
guesses: 0
};
}
nextLevel() {
newLevel = Number.parseInt(this.state.level)
newLevel++
this.setState({level: newLevel})
}
newRange() {
newRangeEnd = Number.parseInt(this.state.rangeEnd)
newRangeEnd = newRangeEnd * 2
this.setState({rangeEnd: newRangeEnd})
}
showDialog() {
Alert.alert(
'YAY',
"You got my number! Now you're on level " + this.state.level,
[
{text: 'OK', onPress: () => console.log('OK Pressed')},
],
{ cancelable: false }
)
}
winLevel() {
this.nextLevel()
this.newRange()
this.restartGame()
this.showDialog()
}
handleClick() {
console.log(this.state.guess)
console.log(this.state.targetNumber)
this.setState({guesses: this.state.guesses + 1})
if (Number.parseInt(this.state.guess) > Number.parseInt(this.state.targetNumber)) {
this.setState({clue: "..."})
setTimeout(() => { this.setState({clue: "TOO HIGH"})}, 200);
} else if (Number.parseInt(this.state.guess) < Number.parseInt(this.state.targetNumber)) {
this.setState({clue: "..."})
setTimeout(() => { this.setState({clue: "TOO LOW"})}, 200);
} else {
this.setState({clue: "..."})
setTimeout(() => {
this.setState({clue: "YAY"})
this.winLevel()
},
200);
}
}
restartGame() {
targetNumberValue = Math.floor(Math.random() * this.state.rangeEnd) + 1;
this.setState({guess: '',
targetNumber: targetNumberValue,
clue: '',
gameOver: false})
}
showPlayAgain() {
if (this.state.gameOver) {
return<Button onPress={() => { this.restartGame() }} title="Play Again" />
}
}
render() {
console.log('render called');
return (
<View style={styles.container}>
<View style={{
flexDirection: 'column',
justifyContent: 'space-around',
backgroundColor: 'steelblue',
height: '70%'
}}>
<View style={{
flexDirection: 'column',
justifyContent: 'space-around',
}}>
<View style={{width: '100%', height: 50, backgroundColor: 'steelblue',
justifyContent: 'center', alignItems: 'center'}}>
<Text style={[styles.title, styles.thefont]}>NUMBER GUESSER</Text>
</View>
</View>
<View style={{
flexDirection: 'row',
justifyContent: 'space-around',
height: '40%',
alignItems: 'center'
}}>
<View style={{width: '33%', height: 50,
justifyContent: 'center', alignItems: 'center'}}>
<Text style={[styles.rangeValue, styles.thefont]}>1</Text>
</View>
<TextInput
style={styles.guessBox}
keyboardType="numeric"
returnKeyType="done"
onChangeText={(guess) => this.setState({guess}) }
onSubmitEditing={ () => this.handleClick() }
/>
<View style={{width: '33%', height: 50,
justifyContent: 'center', alignItems: 'center'}}>
<Text style={[styles.rangeValue, styles.thefont]}>{this.state.rangeEnd}</Text>
</View>
</View>
</View>
<View style={{
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'
}}>
<Text style={[styles.clue, styles.thefont]}>{this.state.clue}</Text>
</View>
<View style={{
flexDirection: 'row',
justifyContent: 'space-around',
}}>
<View style={{width: '40%', height: 100}}>
<Text style={[styles.dataValue, styles.thefont]}>{this.state.level}</Text>
<Text style={[styles.dataLabel, styles.thefont]}>LEVEL</Text>
</View>
<View style={{width: '40%', height: 120}}>
<Text style={[styles.dataValue, styles.thefont]}>{this.state.guesses}</Text>
<Text style={[styles.dataLabel, styles.thefont]}>GUESSES</Text>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'space-between',
backgroundColor: '#fafafd'
},
thefont: {
fontFamily: 'Roboto'
},
title: {
color: 'white',
fontSize: 30,
},
dataLabel: {
color: '#aaa',
fontSize: 16,
width: 100,
height: 20,
fontWeight: 'bold'
},
rangeValue: {
fontSize: 40,
fontWeight: 'bold',
color: 'white'
},
guessBox: {
width: 100,
height: 60,
fontSize: 40,
borderTopWidth: 1,
borderLeftWidth: 1,
borderRightWidth: 1,
borderBottomWidth: 1,
color: 'white'
},
dataValue: {
color: '#14143D',
fontSize: 30,
width: 100,
height: 35
},
clue: {
color: 'steelblue',
fontSize: 30,
fontWeight: 'bold',
width: 150,
height: 100
}
});