gpt4 book ai didi

ios - 如何检查每两张翻转卡片的数组中按钮的当前标题是否在卡片匹配游戏中匹配

转载 作者:行者123 更新时间:2023-12-01 17:03:09 26 4
gpt4 key购买 nike

我正在为这个 iOS 快速匹配纸牌游戏添加一些功能,我需要检查前 2 个按钮是否翻转匹配。他们有八张卡片的四种表情符号,这意味着有 4 对匹配。我无法找出如何检查卡片是否匹配以及当它们匹配时,我需要按钮的背景颜色不透明(不可见)。除了 chooseCard 函数中的浓度类中的空 if 语句外,其他一切都有效。那就是我需要帮助的地方。
这是所有代码,因此您可以查看与什么相关的内容:

class ViewController: UIViewController {

lazy var game = Concentration(numberOfPairsOfCards: (cardButtons.count + 1) / 2)

var flipCount = 0 {
// Property Observer
didSet { flipLabel.text = "Flips: \(flipCount)" }
}

@IBOutlet weak var flipLabel: UILabel!

@IBOutlet var cardButtons: [UIButton]!

@IBAction func touchCard(_ sender: UIButton) {
flipCount+=1
if let cardNumber = cardButtons.firstIndex(of: sender) {
game.chooseCard(at: cardNumber)
updateViewFromModel()
} else {
print ("chosen card was not in cardButtons")
}

}

var emojiChoices = ["👻", "🎃", "🙏🏾", "🦆"]

var emoji = [Int:String]()

func emoji(for card: Card) -> String {
if emoji[card.identifier] == nil, emojiChoices.count > 0 {
let randomIndex = Int(arc4random_uniform(UInt32(emojiChoices.count)))
emoji[card.identifier] = emojiChoices.remove(at: randomIndex)

}
return emoji[card.identifier] ?? "?"
}

func updateViewFromModel() {
for index in cardButtons.indices {
let button = cardButtons[index]
let card = game.cards[index]
if card.isFaceUp {
button.setTitle(emoji(for: card), for: UIControl.State.normal)
button.backgroundColor = UIColor.white
}
else {
button.setTitle("", for: UIControl.State.normal)
button.backgroundColor = UIColor.orange
}

}
}
}

进口基金会
class Concentration {

var cards = [Card]()

func chooseCard(at index: Int) {
if cards[index].isFaceUp {
cards[index].isFaceUp = false
}
else {
cards[index].isFaceUp = true
}

if cards[index].isFaceUp && cards[index].isMatched {

}
}

init(numberOfPairsOfCards: Int) {
for _ in 0..<numberOfPairsOfCards {
let card = Card()
cards += [card,card]
}

//TODO: Shuffle cards
cards.shuffle()
}
}

import Foundation

struct Card {
var isMatched = false
var isFaceUp = false
var identifier: Int

static var identifierFactory = 0

static func getUniqueIdentifier() -> Int {
Card.identifierFactory += 1
return Card.identifierFactory
}

init() {
self.identifier = Card.getUniqueIdentifier()
}
}

最佳答案

在您的集中类(class)中,您将检查 faceUp 卡片索引

将您的注意力从类更改为结构

struct Concentration { // instead of class Concentration

private var indexOfFaceUpCard: Int? {
get {
let faceUpCardIndices = cards.indices.filter { cards[$0].isFaceUp }
return faceUpCardIndices.count == 1 ? faceUpCardIndices.first : nil
} set {
for index in cards.indices {
cards[index].isFaceUp = (index == newValue)
}
}
}

然后你有变异的 chooseCard 方法
mutating func chooseCard(at index: Int)

在您的 chooseCard 方法中,您检查匹配
if !cards[index].isMatched {
if let matchIndex = indexOfFaceUpCard, matchIndex != index {
if cards[matchIndex] == cards[index] {
cards[matchIndex].isMatched = true
cards[index].isMatched = true
}
cards[index].isFaceUp = true

} else {
indexOfFaceUpCard = index
}
}

所以你的方法看起来像这样
mutating func chooseCard(at index: Int) {

if !cards[index].isMatched {
if let matchIndex = indexOfFaceUpCard, matchIndex != index {
if cards[matchIndex] == cards[index] {
cards[matchIndex].isMatched = true
cards[index].isMatched = true
}
cards[index].isFaceUp = true

} else {
indexOfFaceUpCard = index
}
}
}

更新您的卡片结构
struct Card: Hashable {
var isMatched = false
var isFaceUp = false
var identifier: Int

static var identifierFactory = 0

static func getUniqueIdentifier() -> Int {
Card.identifierFactory += 1
return Card.identifierFactory
}
static func ==(lhs: Card, rhs: Card) -> Bool {
return lhs.identifier == rhs.identifier
}

init() {
self.identifier = Card.getUniqueIdentifier()
}
}

关于ios - 如何检查每两张翻转卡片的数组中按钮的当前标题是否在卡片匹配游戏中匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61704677/

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