编辑代码

//函数6:生成不同项的随机位置和随机内容
func differenceCreate(mount:Int) {
    for _ in 0..<mount {
        let col = Int(arc4random()) % cols
        let row = Int(arc4random()) % rows

        errorCords.append((row,col))
        //从images提供是字符中选一个
        let index = Int(arc4random()) % images.count
        //选择上面图形,还是下面图形?
        let which = Int(arc4random()) % 2
        //影响iv1和iv2这两个图形
        if which == 0 {
            iv1[cols*row + col] = images[index]
        }
        else {
            iv2[cols*row + col] = images[index]
        }
    }
}

//函数7:不同点的Button被单机后调用,支持音效
@objc func buttonCheckMusic(_sender : UIButton) {
    let tipsLabel = self.view.viewWithTag(1001) as! UiILabel
    var tag = sender.tag
    if tag >= 5000 {
        tag -= 5000
    }
    else if tag >= 4000 {
        tag -= 4000
    }
    let row = Int (tag / cols)
    let col = tag - cols * row

    let result = errorCords.filter {
        $0 == (row,col)
    }
    if result.count >= 1 {
        let path = Bundle.main.path(forResource:"birdsound",ofType:"m4a")
        let url = URL(fileURLWithPath: path!)
        soundPlayer=try? AVAudioPlayer(contentsOf: url)
        soundPlayer.play()
        tipsLabel.text = "找错啦: (\(row),\(col))"
    }
}