用数组生成随机函数没有重复swift

时间:2015-09-22 22:44:10

标签: arrays swift random struct func

我正在尝试通过创建不同的短游戏或应用程序来学习快速。在尝试测试我对游戏的新知识时,我意识到我需要创建一个我不知道如何创建的函数。我环顾四周,我找到的答案都没有帮助我理解我必须做的事情。

我将总结一下我的游戏:

它是一个单一的视图应用程序。 我有五个功能。

我想随机选择要执行的功能。

执行该功能后,用户将再次按下一个运行随机选择功能的按钮随机选择一个功能,但这次只剩下剩下的四个其他功能,依此类推。同时,一旦执行了五个函数,我想重新启动随机选择一个随机函数。所以游戏一直持续着。

我不确定如何将func放在数组中以使其正常工作。

例如我在游戏中使用了以下脚本。

Func one生成一个显示随机字符串的Label。

我使用以下脚本为没有重复的标签创建一个随机字生成器,一旦它遍历数组中的所有单词,它就会随机重新启动。 (它完美地运作)

    struct randword {
     var wordstring : String! }


   var stentenceword = [randword]
   var stentecenumber = Int


  func wordsinsentence (){
     sentenceword = [randoword(wordstring:"House"), randoword(wordstring:"Truck"), randoword(wordstring:"Ladder")]
   }

  func pickword(){
      if sentenceword.count > 0 {
          sentencenumber = random () % sentenceword.count
          wordlabel.text = sentecneword[sentencenumber].wordstring

      sentenceword.removeAtIndex(sentencenumber)

        ); if sentenceword.count <= 0{
          wordsinsentence() }

Func 2使用与以前相同的脚本生成随机图像,只是稍微修改了图像。

Func three是一个带有另一个脚本的计时器游戏......依此类推......等等。

现在我希望随机选择并删除所有五个func,然后再重新启动,就像我上面放置的示例脚本一样。

我尝试使用相同的脚本并修改它来替换字符串!带有函数数组的数组,但没有正结果。

任何人都可以帮我解决方案或向我展示我应该如何做的其他选择吗?

3 个答案:

答案 0 :(得分:1)

我尝试将您的代码粘贴到游乐场并尝试修复它,但它有点难以理解。缩进是非常好的习惯,特别是如果其他人必须查看您的代码。无论如何,想象一下你是否可以说Int.radomOutOf(5)并且它只返回0到5之间的一些随机数。你可以通过向Int或CGFloat或Double添加扩展来实现。并具有跟踪您的游戏生活时间并调用这些随机功能的功能。这是怎么做的(我希望我没有忘记任何事情)

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textLabel: UILabel!

    var randomIndex = Int()
    let NumberOfFunctions = 4
    var chosenIndexes: [Int] = []{
        didSet{
            if chosenIndexes.count == NumberOfFunctions { chosenIndexes.removeAll() }
        }
    }

    @IBAction func buttonPressed(sender: UIButton) {
        functionSelector()
    }

    func functionSelector(){

        repeat{
            randomIndex = Int.randomOutOf(NumberOfFunctions)
            print("selected: \(randomIndex) \(chosenIndexes)")
        } while chosenIndexes.contains(randomIndex) 

        chosenIndexes.append(randomIndex)

        switch randomIndex {
        case 0: function1()
        case 1: function2()
        case 2: function3()
        case 3: function4()
        default: break
        }
    }


    func function1(){
        textLabel.text = "sentence 1"
    }
    func function2(){
        textLabel.text = "sentence 2"
    }
    func function3(){
        textLabel.text = "sentence 3"
    }
    func function4(){
        textLabel.text = "sentence 4"
    }


}
//your extension to pick random
private extension Int {
    static func randomOutOf(max:Int) ->Int{
        print("max: \(max)")
        return Int(arc4random() % UInt32(max)) // returns 0 - max
    }
}

答案 1 :(得分:1)

您的问题有点难以理解,但请关注您最明确的问题:

  • 我不确定如何将func放入数组中以使其正常工作。
  • 我想随机选择要执行的功能。
  • 没有重复

首先,要随机选择值而不重复,您需要一个列表并将其随机排序。没有必要跟踪以前选择的项目。 Nate Cook在How do I shuffle an array in Swift?

很好地解释了斯威夫特的改组

这就引出了第二个问题,我们如何创建一系列函数?这是最简单的部分。你只需要创建一系列函数。

func first() { print("first") }
func second() { print("second") }
func third() { print("third") }

let fs = [first, second, third]

这就是它的全部内容。现在让我们将它们放在一起并按随机顺序调用它们:

for f in fs.shuffle() {
    f()
}

答案 2 :(得分:0)

Not a separate answer

这只是为了表明第一个答案运行得很好