gpt4 book ai didi

swift - 当所有猜测都用完后,如何让游戏显示单词?

转载 作者:行者123 更新时间:2023-11-30 12:13:52 31 4
gpt4 key购买 nike

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

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() 完成的。它通知系统 View 的内容需要重新绘制。在您的 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()
}
}
}

关于swift - 当所有猜测都用完后,如何让游戏显示单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674780/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com