Change state if win or lose


#1

Hello guys! I’m making a small game with expo and i have a problem. Creating the state of the var, because i want that the user earn the money only if he has win. And for win i mean that after pressing one button i set the State only if i win.

EDIT : if u want to try the alpha version (italian version here is the link expo.io

(note that i’m using renderIf from ajwhite )

[ when the user press the Button we throw like a dice that goes from 0 - 50.000 / if the number generated is lower or equal than winningnumber we give to the player the moneytowin ]

[ on sliding the moneytowin must increase in the opposite direction of the slider 100% = 11 , 99% = 11 , 98 = 12 ]

static defaultProps = {
    value : 1, // value of the slider
    money: 100, // the money of the user
    moneytowin: 11, // the money that the user earn if win
    winningnumber: 0, // this is a number that randomly change.
    number: 0, // this is a number that is generate when the user play the game
    bet: 10, // this is the bet of the player
  };

  state = { 
    value: this.props.value,
    money: this.props.money,
    moneytowin: this.props.moneytowin,
    winningnumber: this.props.winningnumber,
    number: this.props.number, 
    bet: this.props.bet,
  };

  render() {
    const win = renderIf(this.state.winningnumber <=  this.state.number); 
    const lose = renderIf(this.state.winningnumber >  this.state.number);
    const play = renderIf(this.state.money > 0);
    const rechargemoney = renderIf(this.state.money <= 0);
    return (

{win(
        <Text style={[styles.text, {color: "green"}]}>you win !!</Text>
        
      )}
      {lose(
        <Text style={[styles.text, {color: "red"}]}>you lose :(</Text>
      )}
// we create a Slider that increase the bet and the moneytowin , and the winningnumber .
<Slider
minimumValue={1} maximumValue={100}
  value={this.state.value}
  onSlidingComplete={
    (value) => 
    this.setState({
      value: value, 
      bet: value / 3 + 5,
      moneytowin: moneytowin + Math.floor((Math.random() * 50000) + 1) / 100,
      winningnumber: Math.floor((Math.random() * 50000)) 
      })
      } />

// we create the buttons that made the game possible
   {play(
        <Button
            onPress={(value) => 
            this.setState({
              number: Math.floor((Math.random() * 50000)), // we create the number 
              money: this.state.money.toFixed(3) - this.state.bet.toFixed(3), // we pay the money for the match
           })}
            title="play"
                />
      )}
   {rechargemoney(
            <Button
            onPress={(value) => this.refs.modal3.open() }
            title="recharge money"
             />
          )}
)
}


#2

hi-

can you paste your code here?
it will be a lot easier to help if you do that.
there are a lot of different ways you can change state!

charlie