Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
128 lines (118 loc) · 4.04 KB
/
Copy pathApp.js
File metadata and controls
128 lines (118 loc) · 4.04 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 React, {Component, useState} from 'react';
import { StyleSheet, Text, View, Button, Keyboard, TouchableWithoutFeedback, Modal } from 'react-native';
import Header from './components/header';
import Input from './components/input';
import Colors from './constants/colors';
import Buttons from './components/buttons';
import NumberLine from './components/numberLine';
export default class App extends Component {
state = {
userNumber: 1,
visible: 'false',
computerGuess: makeComputerGuess(0, 9999, 0),
min: 0,
max: 9999,
guessedRight: false,
}
render () {
const checkIfCorrect = () => {
var guess = this.state.computerGuess;
var correct = this.state.userNumber;
if(guess == correct){
alert("Computer Won!");
this.state.guessedRight= true;
}
}
const inputHandler = inputText => {
if(isNaN(inputText)){
inputText.replace(/[^0-9]/g, '')
alert("USE ONLY INTEGERS")
}
else {
this.setState({userNumber: inputText})
}
}
const confirmNumber = () => {
if(this.state.userNumber != ''){
this.setState({visible: true});
this.setState({guessedRight: false})
}
else {
alert("ENTER NUMBER FIRST");
}
}
const restartGame = () => {
this.setState({visible: 'false'})
this.state.max=9999;
this.state.min=0;
this.setState({computerGuess: makeComputerGuess(this.state.min, this.state.max, this.state.computerGuess)});
}
const guessSmaller = () => {
if(this.state.computerGuess > this.state.userNumber){
this.state.max = this.state.computerGuess;
this.setState({computerGuess: makeComputerGuess(this.state.min, this.state.max, this.state.computerGuess)});
}
else{
alert("DON'T CHEAT!!!");
}
}
const guessBigger = () => {
if(this.state.computerGuess < this.state.userNumber){
this.state.min = this.state.computerGuess;
this.setState({computerGuess: makeComputerGuess(this.state.min, this.state.max, this.state.computerGuess)});
}
else{
alert("DON'T CHEAT!!!");
}
}
return (
<TouchableWithoutFeedback onPress={() => {Keyboard.dismiss()}}>
<View style={styles.root}>
<Header title="Main Page"/>
<Input maxLength={4} keyboardType="number-pad" placeholder="enter number" onChangeText={inputHandler}/>
<Buttons secondAction={confirmNumber} firstTitle="Cancel" secondTitle="Confirm"/>
<Modal visible={this.state.visible}>
<View style={styles.rootGame}>
<Header title="Let's play!" />
<NumberLine selectedNumber={this.state.userNumber}/>
<View style={{marginTop: 60, width: '100%', alignContent: 'center',}} >
<Text style={{color: Colors.accentText, fontSize:20, fontWeight: 'bold', textAlign: 'center',}}>
Computer Guess:
</Text>
<Text style={{fontWeight: 'bold', fontSize: 60, textAlign: 'center', color: Colors.main,}}>
{this.state.computerGuess}
</Text>
{checkIfCorrect()}
<Buttons firstTitle="Smaller" firstAction={guessSmaller} secondTitle="Bigger" secondAction={guessBigger}/>
{this.state.guessedRight && <Button onPress={restartGame} color="green" title="Restart Game" style={{width:'40%', marginLeft: '20%', textAlign: 'center',}}/>}
</View>
</View>
</Modal>
</View>
</TouchableWithoutFeedback>
);
}
}
const makeComputerGuess = (min, max, current) => {
min = Math.ceil(min);
max = Math.floor(max);
const guessMade = Math.floor((Math.random() * (max -min)) + min);
if(guessMade == current){
makeComputerGuess(min, max, current)
}
else{
return guessMade;
}
}
const styles = StyleSheet.create({
root: {
backgroundColor: Colors.accent,
height: '100%',
width: '100%',
},
rootGame: {
backgroundColor: 'black',
height: '100%',
width: '100%',
},
});