Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StyleSheet, Text, View, Button, Alert, TextInput, Image, AsyncStorage } from 'react-native';
  3.  
  4. export default class App extends React.Component {
  5.  
  6.   constructor(props)
  7.   {
  8.     super(props);
  9.     this.state = {number: 0, guess: '', text: '', counter: 0, highScore: 0}
  10.   }
  11.  
  12.   componentDidMount()
  13.   {
  14.     // Call function to set initial high score here on load
  15.     this.rerun()
  16.   }
  17.  
  18.   // When game starts/is complete re-initialize the number to guess
  19.   rerun = () =>
  20.   {
  21.     const current = Math.floor(Math.random() * 100) + 1;
  22.     this.setState({
  23.       number: current,
  24.       counter: 0
  25.     })
  26.   }
  27.  
  28.   guess = () =>
  29.   {
  30.     const currentGuess = this.state.guess;
  31.     const theNumber = this.state.number;
  32.     if (theNumber > currentGuess)
  33.     {
  34.       this.setState({
  35.         text: "Guess too low!",
  36.         counter: (this.state.counter + 1)
  37.       })
  38.     }
  39.     else if (theNumber < currentGuess)
  40.     {
  41.       this.setState({
  42.         text: "Guess too high!",
  43.         counter: (this.state.counter + 1)
  44.       })
  45.     }
  46.     else
  47.     {
  48.       const totalTries = (this.state.counter + 1);
  49.       let highScoreString = '';
  50.       Alert.alert("Guess correct! It took you " + totalTries + " tries!");
  51.       if(this.state.highScore > totalTries || this.state.highScore === 0)
  52.       {
  53.         this.setState({
  54.           text: ""
  55.         })
  56.         highScoreString = String(totalTries)
  57.         // Call Async function to save highScore as string key : value pair
  58.         AsyncStorage.setItem('highScore', highScoreString)
  59.       }
  60.       else
  61.       {
  62.         this.setState({
  63.           text: ""
  64.         })
  65.       }
  66.       // Get data from AsyncStorage and set it to the state
  67.       this.returnData()
  68.       // Rerun game
  69.       this.rerun()
  70.     }
  71.   }
  72.  
  73.   // Call data from async storage
  74.   returnData = async () =>
  75.   {
  76.     try
  77.     {
  78.       let highScore = await AsyncStorage.getItem('highScore')
  79.       console.log(highScore)
  80.       this.setState({
  81.         highScore: parseInt(highScore)
  82.       })
  83.     }
  84.     catch(error)
  85.     {
  86.       Alert.alert("Whoops! something broke!")
  87.     }
  88.   }
  89.  
  90.   render() {
  91.     return (
  92.       <View style={styles.container}>
  93.         <Text>Guess a number between 1-100!</Text>
  94.         <Text id="text">{this.state.text}</Text>
  95.         <TextInput style={{width: 200, borderColor: 'grey', borderWidth: 1}} onChangeText={(guess) => this.setState({guess})} keyboardType='numeric' value={this.state.guess}/>
  96.         <Button onPress={this.guess} title="Guess   "/>
  97.         <Text>High Score: {this.state.highScore} </Text>
  98.       </View>
  99.     );
  100.   }
  101. }
  102.  
  103. const styles = StyleSheet.create({
  104.   container: {
  105.     flex: 1,
  106.     backgroundColor: 'lightblue',
  107.     alignItems: 'center',
  108.     justifyContent: 'center',
  109.   },
  110. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement