I am learning Swift and I am creating a simple dice role app, I cannot find what is wrong with the code

The gist of the app is simple: you click on the small dice image, it runs a function that returns a random number between 1 and 6, the fields on the screen reflect how it will look on the dice, and the colored box labels update with the number when the dice are rolled. When I run the application, everything looks good at the beginning. As soon as I click the dice button, the randomDiceRoll () function runs, but the color box labels and UIView boxes (the ones that reflect what will appear on the bone) don't match. Again, I'm just learning Swift, so please cut me up. Thanks to everyone who contributed! Here is the code:

import UIKit

class ViewController: UIViewController {

@IBOutlet var upperLeft: UIView!

@IBOutlet var midLeft: UIView!

@IBOutlet var lowerLeft: UIView!

@IBOutlet var middle: UIView!

@IBOutlet var upperRight: UIView!

@IBOutlet var midRight: UIView!

@IBOutlet var lowerRight: UIView!


@IBOutlet var redBox: UILabel!

@IBOutlet var orangeBox: UILabel!

@IBOutlet var yellowBox: UILabel!

@IBOutlet var greenBox: UILabel!

@IBOutlet var blueBox: UILabel!

@IBOutlet var purpleBox: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    redBox.isHidden = true
    orangeBox.isHidden = true
    yellowBox.isHidden = true
    greenBox.isHidden = true
    blueBox.isHidden = true
    purpleBox.isHidden = true



}

func randomDiceRoll() -> Int {
    return Int(arc4random_uniform(6) + 1)
}


func updateLabelWithRoll(roll: Int) {

    let diceScore = String(roll)

    if redBox.isHidden {

        redBox.text = diceScore
        redBox.isHidden = false

    } else if orangeBox.isHidden {

        orangeBox.text = diceScore
        orangeBox.isHidden = false

    } else if yellowBox.isHidden {

        yellowBox.text = diceScore
        yellowBox.isHidden = false

    } else if greenBox.isHidden {

        greenBox.text = diceScore
        greenBox.isHidden = false

    } else if blueBox.isHidden {

        blueBox.text = diceScore
        blueBox.isHidden = false

    } else if purpleBox.isHidden {

        purpleBox.text = diceScore
        purpleBox.isHidden = false

    } else {

        redBox.isHidden = true
        orangeBox.isHidden = true
        yellowBox.isHidden = true
        greenBox.isHidden = true
        blueBox.isHidden = true
        purpleBox.isHidden = true

    }

}



@IBAction func buttonPress(_ sender: Any) {

    randomDiceRoll()
    updateLabelWithRoll(roll: randomDiceRoll())


    if randomDiceRoll() == 1 {

        upperLeft.isHidden = true
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = true
        middle.isHidden = false

    }

    if randomDiceRoll() == 2 {

        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true

    }


    if randomDiceRoll() == 3 {

        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false

    }


    if randomDiceRoll() == 4 {

        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true

    }

    if randomDiceRoll() == 5 {

        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false

    }

    if randomDiceRoll() == 6 {

        upperLeft.isHidden = false
        midLeft.isHidden = false
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = false
        lowerRight.isHidden = false
        middle.isHidden = true

    }


}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

      

When the app loads:

ViewDidLoad screen until any action has been taken.

After pressing one button:

After one button press

After two button presses:

After pressing the button twice

And so on .. (the application is just repeated) There will be 6 colored labels in total.

+3


source to share


1 answer


I think your problem is that you are calling the function randomDiceRoll()

multiple times, not just once, and using the result. Every time you call randomDiceRoll()

it returns a NEW random number, you must call this once and store the result in memory for comparison

refactor your code to assign the result of the function to a variable and use the variable for comparison, like

@IBAction func buttonPress(_ sender: Any) {

    let score = randomDiceRoll()
    updateLabelWithRoll(roll: score)

    if score == 1 {
        upperLeft.isHidden = true
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = true
        middle.isHidden = false
    }

    if score == 2 {
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true
    }

    if score == 3 {
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false
    }

    if score == 4 {
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true
    }

    if score == 5 {
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false
    }

    if score == 6 {
        upperLeft.isHidden = false
        midLeft.isHidden = false
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = false
        lowerRight.isHidden = false
        middle.isHidden = true
    }    
}

      



I would also recommend using a switch statement instead of multiple if statements.

@IBAction func buttonPress(_ sender: Any) {

    let score = randomDiceRoll()
    updateLabelWithRoll(roll: score)

    switch score {
    case 1:
        upperLeft.isHidden = true
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = true
        middle.isHidden = false
    case 2:
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true
    case 3:
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = true
        upperRight.isHidden = true
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false
    case 4:
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = true
    case 5:
        upperLeft.isHidden = false
        midLeft.isHidden = true
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = true
        lowerRight.isHidden = false
        middle.isHidden = false
    case 6:
        upperLeft.isHidden = false
        midLeft.isHidden = false
        lowerLeft.isHidden = false
        upperRight.isHidden = false
        midRight.isHidden = false
        lowerRight.isHidden = false
        middle.isHidden = true
    default:
        break
    }
}

      

+5


source







All Articles