为什么随机打乱的数组会重复

Why does a random shuffled array repeat

我洗牌了。

然后我为数组中的每个索引分配一个值。

然后我对每个索引执行一个操作。在这种情况下,将数组中单元格的颜色变为红色。尽管为混洗数组中的每个索引分配了不同的值,但我仍然会重复,有时同一个单元格连续保持红色超过一秒。为什么会这样,就好像数组在 If 语句之间不断地洗牌一样?代码如下。

我已编辑代码以显示整个 viewController。

   import UIKit

class ViewController: UIViewController {


    @IBOutlet var theview: UIView!

    @IBOutlet weak var timerx: UILabel!

    @IBOutlet weak var square1: UIImageView!

    @IBOutlet weak var square2: UIImageView!

    @IBOutlet weak var square3: UIImageView!

    @IBOutlet weak var square4: UIImageView!

    @IBOutlet weak var square5: UIImageView!

    @IBOutlet weak var square6: UIImageView!

    @IBOutlet weak var square7: UIImageView!

    @IBOutlet weak var square8: UIImageView!

    @IBOutlet weak var square9: UIImageView!

    var viewArray = [UIImageView]()

    var timer:Timer?
    var seconds = 11







    override func viewDidLoad() {
        super.viewDidLoad()


        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)


        }

         @objc func timeElapsed() {
       seconds -= 1
       timerx.text = "\(seconds)"
       if seconds <= 0 {
           timer?.invalidate()
                     }
      viewArray = [square1,square2,square3,square4,square5,square6,square7,square8,square9]

            func allcellsYellow(){
                           square1.backgroundColor = UIColor.systemYellow
                           square2.backgroundColor = UIColor.systemYellow
                           square3.backgroundColor = UIColor.systemYellow
                           square4.backgroundColor = UIColor.systemYellow
                           square5.backgroundColor = UIColor.systemYellow
                           square6.backgroundColor = UIColor.systemYellow
                           square7.backgroundColor = UIColor.systemYellow
                           square8.backgroundColor = UIColor.systemYellow
                           square9.backgroundColor = UIColor.systemYellow
                                     }
            func allcellsRed(){
                                      square1.backgroundColor = UIColor.red
                                      square2.backgroundColor = UIColor.red
                                      square3.backgroundColor = UIColor.red
                                      square4.backgroundColor = UIColor.red
                                      square5.backgroundColor = UIColor.red
                                      square6.backgroundColor = UIColor.red
                                      square7.backgroundColor = UIColor.red
                                      square8.backgroundColor = UIColor.red
                                      square9.backgroundColor = UIColor.red
                                                }

            viewArray.shuffle()


            let randomcell = (viewArray[0])
            let randomcell2 = (viewArray[1])
            let randomcell3 = (viewArray[2])
            let randomcell4 = (viewArray[3])
            let randomcell5 = (viewArray[4])
            let randomcell6 = (viewArray[5])
            let randomcell7 = (viewArray[6])
            let randomcell8 = (viewArray[7])
            let randomcell9 = (viewArray[8])


            if seconds == 10 {
                randomcell.backgroundColor = UIColor.red
            }
            if seconds < 10 {
                allcellsYellow()
            }
            if seconds == 9 {
                randomcell2.backgroundColor = UIColor.red
            }
            if seconds < 9 {
                allcellsYellow()
            }
            if seconds == 8 {
                randomcell3.backgroundColor = UIColor.red
            }
            if seconds < 8 {
                allcellsYellow()
            }
            if seconds == 7 {
                randomcell4.backgroundColor = UIColor.red
            }
            if seconds < 7 {
                allcellsYellow()
            }
            if seconds == 6 {
                randomcell5.backgroundColor = UIColor.red
            }
            if seconds < 6 {
                allcellsYellow()
            }
            if seconds == 5 {
                randomcell6.backgroundColor = UIColor.red
            }
            if seconds < 5 {
                allcellsYellow()
            }
            if seconds == 4 {
                randomcell7.backgroundColor = UIColor.red
            }
            if seconds < 4 {
                allcellsYellow()
            }
            if seconds == 3 {
                randomcell8.backgroundColor = UIColor.red
            }
            if seconds < 3 {
                allcellsYellow()
            }
            if seconds == 2 {
                randomcell9.backgroundColor = UIColor.red
            }
            if seconds < 2 {
                allcellsYellow()
            }
            if seconds == 1 {
                randomcell.backgroundColor = UIColor.red
            }
            if seconds < 1 {
                allcellsYellow()
            }
            if seconds == 0 {
                allcellsRed()
            }

我尽量使此代码与您的代码相似,以便您了解需要进行哪些 结构 更改。正如人们所说,您可以做很多事情来使其更加简洁和易于管理。

请仅将此作为重新安排的建议,而不是一个好的解决方案!

import UIKit

class ViewController: UIViewController {

@IBOutlet var theview: UIView!
@IBOutlet weak var timerx: UILabel!
@IBOutlet weak var square1: UIImageView!
@IBOutlet weak var square2: UIImageView!
@IBOutlet weak var square3: UIImageView!
@IBOutlet weak var square4: UIImageView!
@IBOutlet weak var square5: UIImageView!
@IBOutlet weak var square6: UIImageView!
@IBOutlet weak var square7: UIImageView!
@IBOutlet weak var square8: UIImageView!
@IBOutlet weak var square9: UIImageView!

var viewArray = [UIImageView]()

var timer:Timer?
var seconds = 11

    override func viewDidLoad() {
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)
        viewArray = [square1,square2,square3,square4,square5,square6,square7,square8,square9]
        viewArray.shuffle()
    }

    func allcellsYellow(){
        square1.backgroundColor = UIColor.systemYellow
        square2.backgroundColor = UIColor.systemYellow
        square3.backgroundColor = UIColor.systemYellow
        square4.backgroundColor = UIColor.systemYellow
        square5.backgroundColor = UIColor.systemYellow
        square6.backgroundColor = UIColor.systemYellow
        square7.backgroundColor = UIColor.systemYellow
        square8.backgroundColor = UIColor.systemYellow
        square9.backgroundColor = UIColor.systemYellow
    }
    func allcellsRed(){
        square1.backgroundColor = UIColor.red
        square2.backgroundColor = UIColor.red
        square3.backgroundColor = UIColor.red
        square4.backgroundColor = UIColor.red
        square5.backgroundColor = UIColor.red
        square6.backgroundColor = UIColor.red
        square7.backgroundColor = UIColor.red
        square8.backgroundColor = UIColor.red
        square9.backgroundColor = UIColor.red
    }

    fileprivate func updateColours() {
        let randomcell = (viewArray[0])
        let randomcell2 = (viewArray[1])
        let randomcell3 = (viewArray[2])
        let randomcell4 = (viewArray[3])
        let randomcell5 = (viewArray[4])
        let randomcell6 = (viewArray[5])
        let randomcell7 = (viewArray[6])
        let randomcell8 = (viewArray[7])
        let randomcell9 = (viewArray[8])


        if seconds == 10 {
            randomcell.backgroundColor = UIColor.red
        }
        if seconds < 10 {
            allcellsYellow()
        }
        if seconds == 9 {
            randomcell2.backgroundColor = UIColor.red
        }
        if seconds < 9 {
            allcellsYellow()
        }
        if seconds == 8 {
            randomcell3.backgroundColor = UIColor.red
        }
        if seconds < 8 {
            allcellsYellow()
        }
        if seconds == 7 {
            randomcell4.backgroundColor = UIColor.red
        }
        if seconds < 7 {
            allcellsYellow()
        }
        if seconds == 6 {
            randomcell5.backgroundColor = UIColor.red
        }
        if seconds < 6 {
            allcellsYellow()
        }
        if seconds == 5 {
            randomcell6.backgroundColor = UIColor.red
        }
        if seconds < 5 {
            allcellsYellow()
        }
        if seconds == 4 {
            randomcell7.backgroundColor = UIColor.red
        }
        if seconds < 4 {
            allcellsYellow()
        }
        if seconds == 3 {
            randomcell8.backgroundColor = UIColor.red
        }
        if seconds < 3 {
            allcellsYellow()
        }
        if seconds == 2 {
            randomcell9.backgroundColor = UIColor.red
        }
        if seconds < 2 {
            allcellsYellow()
        }
        if seconds == 1 {
            randomcell.backgroundColor = UIColor.red
        }
        if seconds < 1 {
            allcellsYellow()
        }
        if seconds == 0 {
            allcellsRed()
        }
    }

    @objc func timeElapsed() {
       seconds -= 1
       timerx.text = "\(seconds)"
        print("\(seconds)")
       if seconds <= 0 {
           timer?.invalidate()
        }
        DispatchQueue.main.async {
            self.updateColours()
        }
    }

}

每个图像视图不需要一个插座。您可以将它们全部连接到单个 IBOutlet 集合。然后您可以简单地创建其索引的集合并将其洗牌。您还可以完成大部分代码的清理,创建一种将所有背景或单个背景转换为特定颜色的方法:

class ViewController: UIViewController {
    @IBOutlet var squares: [UIImageView]!
    @IBOutlet weak var timeLabel: UILabel!
    var timer = Timer()
    var indices: [Int] = []
    var seconds = 11
    override func viewDidLoad() {
        super.viewDidLoad()
        timer = .scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeElapsed), userInfo: nil, repeats: true)
        indices = squares.indices.shuffled()
        setAllSquares(to: .systemYellow)
    }
    func setAllSquares(to color: UIColor) {
        squares.forEach { [=10=].backgroundColor = color }
    }
    func setSquare(at index: Int, to color: UIColor) {
        squares[index].backgroundColor = color
    }
    @objc func timeElapsed(_ timer: Timer) {
        timeLabel.text = "seconds: \(seconds)"
        if seconds <= 0 { timer.invalidate() }
        switch seconds {
        case 2 ... 10:
            setAllSquares(to: .systemYellow)
            setSquare(at: indices[seconds-2], to: .systemRed)
        case 1:
            setAllSquares(to: .systemYellow)
        case 0:
            setAllSquares(to: .systemRed)
        default:
            break
        }
        seconds -= 1
    }
}

Sample project