dimanche 28 juin 2020

How do you share a randomly generated integer across buttons?

Sorry, I'm quite new to Xcode, but here goes... I'm trying to make this really simple multiplication game, and I have two buttons, one to generate the next question, and one to submit your answer. The problem is that, I need both buttons to "know" the same two random integers that should be multiplied, but the two buttons generate two different sets of integers, and so the question might be something like "What is 10 * 8", while the answer the computer thinks is right might be something like 5 * 5. (I used sender tags for the two different buttons, the submit button having sender tag 2 and the next question button having sender tag 1.) Please help! Here's the full code:

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerBox: UITextField!
@IBOutlet weak var scoreLabel: UILabel!
@IBAction func button(_ sender: AnyObject) {
var thing = 1
var score = 0
if thing < 11
{
    let firstNumber = Int(arc4random_uniform(UInt32(11)))
    let secondNumber = Int(arc4random_uniform(UInt32(11)))
    let answer = Int(firstNumber * secondNumber)
    let answerString = "\(answer)"
    
    
    questionLabel.text = "What is " + "\(firstNumber)" + " x " + "\(secondNumber)"
            
    
    
    if sender.tag != 1
    {
        if answerBox.text == answerString
        {
            score += 1
            scoreLabel.text = "Score: " + "\(score)"
            questionLabel.text =  "Correct!"
            thing += 1
            self.answerBox.text = ""
        }
        else if answerBox.text !=  answerString
        {
            scoreLabel.text = "Score: " + "\(score)"
            questionLabel.text = "Incorrect..."
            thing += 1
            self.answerBox.text = ""
        }
    if sender.tag == 1
    {
        
        questionLabel.text = "What is " + "\(firstNumber)" + " x " + "\(secondNumber)"
            
    }
    if thing > 10
    {
        self.questionLabel.text = "Your score was " + "\(score)" + " out of 10!"
        thing -= 10
        score = 0
        self.scoreLabel.text = "Score: 0"
        }
    }
    
    
       
    }
    

}

}




Aucun commentaire:

Enregistrer un commentaire