Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am learning Swift, and I am creating a simple dice roll app, I can't seem to find what's wrong with the code

The gist of the app is simple, you press the little dice image, it runs a function that returns a random number from 1-6, the boxes on the screen reflect what it would look like on a dice, and the colored box labels are updated with the number as you roll the dice. When I run the app, everything looks good at the beginning. As soon as I hit the dice button, the randomDiceRoll() function runs, but the colored box labels and UIView boxes (the ones that reflect what it would look like on a dice) don't match up. Again, I am just learning Swift so please cut me some slack.

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, no actions have been performed yet.

After one button press:

After one button press

After two button presses:

After two button presses

And so on..(app just repeats itself) There would be 6 colored label boxes in total.

like image 923
andrewlundy Avatar asked Jan 30 '26 12:01

andrewlundy


1 Answers

I think that your issue is you are calling the randomDiceRoll() function multiple times rather than once and using the result. Each time you call randomDiceRoll() it will return a NEW random number, You should call this once and keep the result in memory for comparison

refactor your code so that you assign the result of the function to a variable and use the variable to compare, like so

@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
    }
}
like image 63
Scriptable Avatar answered Feb 02 '26 01:02

Scriptable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!