在所有猜测都用完后,我怎样才能让游戏显示这个词?

How can I get the game to show the word after all guesses have been used up?

所以我是编码新手,我一直在练习游戏等来培养我的技能。我创建了这个猜词游戏,我试图让游戏在所有猜测都用完后显示这个词。但是,该程序不会读取我编写的设置标签以显示答案的代码。这是我到目前为止编写的代码:

class ViewController: UIViewController {
    var listOfWords = ["ladybug", "program", "computer", "language", "glorious", "incandescent"]
    let incorrectMovesAllowed = 7

    var totalWins = 0 {
        didSet {
            newRound()
        }
    }

    var totalLosses = 0 {
        didSet {
            newRound()
        }
    }

    @IBOutlet var letterButtons: [UIButton]!
    @IBAction func buttonPressed(_ sender: UIButton) {
    sender.isEnabled = false
        let letterString = sender.title(for: .normal)!
        let letter = Character(letterString.lowercased())
        currentGame.playerGuessed(letter: letter)
        updateUI()
        updateGameState()

    }

    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var correctWordLabel: UILabel!
    @IBOutlet weak var treeImageView: UIImageView!

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

    func enableLetterButtons (_enable: Bool){
        for button in letterButtons {
            button.isEnabled = _enable
        }
    }

    var currentGame : Game!

    func newRound() {
        if !listOfWords.isEmpty{
           let newWord = listOfWords.removeFirst()
            currentGame = Game (word: newWord, incorrectMovesRemaining: incorrectMovesAllowed, guessedLetters: [])
            enableLetterButtons(_enable: true)
            updateUI()
        } else {
            enableLetterButtons (_enable: false)
        }

    }

    func updateUI() {
        var letters = [String] ()
        for letter in currentGame.formattedWord.characters {
            letters.append(String(letter))
        }
        let wordWithSpacing = letters.joined(separator: " ")
        correctWordLabel.text = wordWithSpacing
        scoreLabel.text = "Wins: \(totalWins), Losses:\(totalLosses)"
        treeImageView.image = UIImage (named: "Tree \(currentGame.incorrectMovesRemaining)")
    }

    func updateGameState(){
       var letters = [String] ()
        for letter in currentGame.word.characters {
            letters.append(String(letter))
        }
        let theAnswer = letters.joined(separator: " ")
        if currentGame.incorrectMovesRemaining == 0 {
            correctWordLabel.text = theAnswer
            Thread.sleep(forTimeInterval: 3)
            totalLosses += 1
        } else if currentGame.word == currentGame.formattedWord {
            totalWins += 1
        } else {
            updateUI()
        }
    }
}

另外,我有一个结构是这样写的:

import Foundation
struct Game {
    var word : String
    var incorrectMovesRemaining : Int
    var guessedLetters: [Character]
    mutating func playerGuessed (letter: Character){
        guessedLetters.append(letter)
        if !word.characters.contains(letter){
            incorrectMovesRemaining -= 1
        }
    }
    var formattedWord: String {
        var guessedWord = ""
        for letter in word.characters {
            if guessedLetters.contains(letter) {
                guessedWord += "\(letter)"
            } else {
                guessedWord += "_"
            }
        }
        return guessedWord
    }
}

您需要重新绘制 UI,这是通过 self.setNeedsDisplay() 完成的。它通知系统视图的内容需要重绘。在您的 updateUI() 中,您可以添加此内容。 关于 setNeedsDisplay 你可以获得更多信息 here

class ViewController: UIViewController {
    var listOfWords = ["ladybug", "program", "computer", "language", "glorious", "incandescent"]
    let incorrectMovesAllowed = 7
    var totalWins = 0 {
        didSet {
            newRound()
        }
    }

    var totalLosses = 0 {
        didSet {
            newRound()
        }
    }

    @IBOutlet var letterButtons: [UIButton]!
    @IBAction func buttonPressed(_ sender: UIButton) {
    sender.isEnabled = false
        let letterString = sender.title(for: .normal)!
        let letter = Character(letterString.lowercased())
        currentGame.playerGuessed(letter: letter)
        updateUI()
        updateGameState()

    }

    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var correctWordLabel: UILabel!
    @IBOutlet weak var treeImageView: UIImageView!

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

    func enableLetterButtons (_enable: Bool){
        for button in letterButtons {
            button.isEnabled = _enable
        }
    }

    var currentGame : Game!

    func newRound() {
        if !listOfWords.isEmpty{
           let newWord = listOfWords.removeFirst()
            currentGame = Game (word: newWord, incorrectMovesRemaining: incorrectMovesAllowed, guessedLetters: [])
            enableLetterButtons(_enable: true)
            updateUI()
        } else {
            enableLetterButtons (_enable: false)
        }

    }

    func updateUI() {
        var letters = [String] ()
        for letter in currentGame.formattedWord.characters {
            letters.append(String(letter))
        }
        let wordWithSpacing = letters.joined(separator: " ")
        correctWordLabel.text = wordWithSpacing
        scoreLabel.text = "Wins: \(totalWins), Losses:\(totalLosses)"
        treeImageView.image = UIImage (named: "Tree \(currentGame.incorrectMovesRemaining)")
        self.setNeedsDisplay()
    }

    func updateGameState(){
       var letters = [String] ()
        for letter in currentGame.word.characters {
            letters.append(String(letter))
        }
        let theAnswer = letters.joined(separator: " ")
        if currentGame.incorrectMovesRemaining == 0 {
            correctWordLabel.text = theAnswer
            Thread.sleep(forTimeInterval: 3)
            totalLosses += 1
        } else if currentGame.word == currentGame.formattedWord {
            totalWins += 1
        } else {
            updateUI()
        }
    }
}

创建一个变量来记录您猜错了多少次。那么你可以这样做:

使用 while 语句:

 while numberOfTimesGuessedWrong <= 7{

}
 //when you have guessed incorrectly 7 times, the compiler will move here:
wordLabel.text = "\(correctAnswer)"

所以当你猜错7次,第8次,它就会显示正确答案。