刽子手计划

时间:2016-10-27 05:21:22

标签: ios arrays swift swift2

我在iOS上正在开发一个Hangman单视图应用程序游戏,并且我一直停留在“letterButton”代码上。我打算做的是将viewDidLoad函数中生成的破折号替换为用户将通过字母按钮输入的内容(所有字母按钮分组在letterButton下)。

我尝试用字母数组替换dash数组,但它只是告诉我字母数组太大了。我尝试将字母数组转换为字符串,然后用它替换破折号数组,但这也不起作用。所以现在我卡住了,我不知道怎么做用字母输入替换破折号数组。

请帮忙(这是我的代码;请忽略仅仅是我拼命尝试的dashArray [n]部分):

class ViewController: UIViewController {
    @IBOutlet var letters: [UIButton]!
    var wordArray: [String] = ["cat", "dog", "hamster", "lizard"]
    var letterArray: [String] = ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]
    var randomValue: Int = 0
    var word: String = ""

    @IBOutlet weak var wordLabel: UILabel!

    override func viewDidLoad()
    {
        randomValue = Int(arc4random_uniform(UInt32(wordArray.count)))
        word = wordArray[randomValue]
        letterArray = Array(arrayLiteral: word)

        let amount = word.characters.count
        let dashArray = Array(count: amount, repeatedValue: "_")
        let dashString = dashArray.joinWithSeparator(" ")

        wordLabel.text = dashString

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func letterButton(sender: UIButton)
    {
        let amount = word.characters.count

        var dashArray = Array(count: amount, repeatedValue: "_")

        dashArray [0] =
        dashArray [1] =
        dashArray [2] =
        dashArray [3] =
        dashArray [4] =
        dashArray [5] =
        dashArray [6] =

        let dashString = dashArray.joinWithSeparator(" ")
        wordLabel.text = dashString
    }

    @IBAction func resetButton(sender: UIButton)
    {
        let amount = word.characters.count
        let dashArray = Array(count: amount, repeatedValue: "_")
        let dashString = dashArray.joinWithSeparator(" ")

        wordLabel.text = dashString
    }
}

1 个答案:

答案 0 :(得分:0)

如果我找到了你,你需要从一个按钮上取一个字母,用这个字母替换相应的短划线。

@IBAction func letterButton(sender: UIButton)
{
    guard let letterIndex = letters.indexOf(sender) else { return }

    let letter = letterArray[letterIndex]

    var dashString = wordLabel.text
    dashString[letterIndex] = letter
    wordLabel.text = dashString
}

编辑感谢您的澄清,在阅读了游戏规则后,您想要实现的目标越来越清晰。目前的问题是您在letterArray中重写了viewDidLoad。我建议修复,抱歉可能出现的错误:

class ViewController: UIViewController {
     @IBOutlet var letterButtons: [UIButton]!
     @IBOutlet weak var wordLabel: UILabel!
     var words: [String] = ["cat", "dog", "hamster", "lizard"]
     var letters: [String] = ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"]
     var word: String = ""

     override func viewDidLoad()
     {
         super.viewDidLoad()

         let randomValue = Int(arc4random_uniform(UInt32(words.count)))
         word = wordArray[randomValue]

         let amount = word.characters.count
         let dashArray = Array(count: amount, repeatedValue: "_")
         wordLabel.text = dashArray.joinWithSeparator(" ")
     }

     @IBAction func letterButton(sender: UIButton)
     {
         guard let letterIndex = letters.indexOf(sender) else { return }

         let letter = letterArray[letterIndex]
         guard let indexInWord = word.characters.indexOf(letter) else {
             print("no such letter in this word")
             return
         }

         // since we have spaces between dashes, we need to calc index this way
         let indexInDashedString = indexInWord * 2

         var dashString = wordLabel.text
         dashString[indexInDashedString] = letter
         wordLabel.text = dashString
     }

     @IBAction func resetButton(sender: UIButton)
     {
         let amount = word.characters.count
         let dashArray = Array(count: amount, repeatedValue: "_")
         wordLabel.text = dashArray.joinWithSeparator(" ")
     }
}