jeudi 4 août 2016

Issue with checking new integer against previous one

Im attempting to make something similar to the high low game. So far i can get a new integer between 1-25 to be generated. Ive tried to write a function that when you press the 'lower' button it checks the new integer against the previous one, and if it is less then text is displayed below saying 'You Were Correct' and updates the users score +1.. and if wrong 'You Were Wrong' displays instead and score is reset to 0.

Whenever i press the lower button it gives me a new int but score is not updated and the message doesn't display. This is my first real attempt at making this so bear with me :)

Lower Button

    @IBAction func lower(sender: AnyObject) {
    var newNumber = randomIntBetween(2, high: 26)
    var oldNumber: Int?

    func lowerNumber() -> Int {
        if newNumber <= oldNumber {
            correctWrong.text = "You Were Correct!"
            score.text = "Score: \(countWin++)"
        } else {
            correctWrong.text = "You Were Wrong!"
            score.text = "Score: \(countLose)"
        }
        return newNumber
    }
     randomNumbers.text = "\(newNumber)"
}

Random Number Function

    func randomIntBetween(low:Int, high:Int) -> Int {
    let range = high - (low - 1)
    return (Int(arc4random()) % range) + (low - 1)
}

Variables/Constants Used

var countWin = 0
let countLose = 0

Thanks




Aucun commentaire:

Enregistrer un commentaire