我正在初始化一副牌。我的卡片结构中有卡片属性。我的方法是尝试创建一个“枚举状态”数组,然后遍历这些数组来初始化每个卡。我这样做有困难。
游戏类

import Foundation

struct Set{
    var cards = [Card]()

    init(){
        let properties : [Any] =
            [cardShape.self, cardColor.self, cardNumber.self, cardShading.self]
        for prop in properties{
        // Not really sure how to iterate through this array...
        // Ideally it would be something like this.
        // Iterate through array, for property in array,
        // card.add(property)
        }
    }
}

卡片类
import UIKit
import Foundation

struct Card{
    var attributes : properties = properties()

    mutating func addProperty(value : Property){
        if value is cardShape{
            attributes.shape = value as! cardShape
        } else if value is cardColor{
            attributes.color = value as! cardColor
        } else if value is cardNumber{
            attributes.number = value as! cardNumber
        }else if value is cardShading{
            attributes.shading = value as! cardShading
        }else{
            print("error")
        }
    }
}

protocol Property{
    static var allValues : [Property] {get}
}

struct properties{
    var shape : cardShape = cardShape.none
    var color : cardColor = cardColor.none
    var number : cardNumber = cardNumber.none
    var shading : cardShading = cardShading.none
}

enum cardShape : String,Property{
    case Square = "■"
    case Triangle = "▲"
    case Circle = "●"
    case none
    static var allValues : [Property]{ return [cardShape.Square,cardShape.Triangle,cardShape.Circle]}
}

enum cardColor:Property  {
    case Red
    case Purple
    case Green
    case none

    static var allValues : [Property] {return [cardColor.Red,cardColor.Purple,cardColor.Green]}
}

enum cardNumber : Int,Property{
    case One = 1
    case Two = 2
    case Three = 3
    case none

    static var allValues : [Property] {return [cardNumber.One,cardNumber.Two,cardNumber.Three]}
}

enum cardShading: Property {
    case Solid
    case Striped
    case Outlined
    case none

    static var allValues : [Property] {return [cardShading.Solid,cardShading.Striped,cardShading.Outlined]}
}

总之,我的主要问题是创建一个枚举数组,然后循环遍历枚举状态来初始化具有特定属性状态的卡。

最佳答案

您需要确保覆盖所有属性组合,并确保每张卡都有四种属性中的一种。我建议使用嵌套循环:

for shape in cardShape.allValues {
    for color in cardColor.allValues {
        for number in cardNumber.allValues {
            for shading in cardShading.allValues {
                var card = Card()
                card.addProperty(shape)
                card.addProperty(color)
                card.addProperty(number)
                card.addProperty(shading)
                cards.append(card)
            }
        }
    }
}

我认为你的有点太复杂了。如果你改变你的表现,它将更容易创建卡。
让您的卡将不同的属性表示为自己的属性:
struct Card {
    let shape: CardShape
    let color: CardColor
    let number: CardNumber
    let shading: CardShading
}

然后使用嵌套循环创建卡:
for shape in CardShape.allValues {
    for color in CardColor.allValues {
        for number in CardNumber.allValues {
            for shading in CardShading.allValues {
                cards.append(Card(shape: shape, color: color, number: number, shading: shading))
            }
        }
    }
}

笔记:
枚举应以大写字符开头,枚举值应以小写字符开头。
对每个属性使用单独的属性可以更容易地检查卡之间的匹配属性。
默认情况下,您会得到一个初始化器来初始化所有属性。通过使用嵌套循环初始化它们,您将能够创建所有可能的卡。
更改Card属性以返回特定属性类型的数组(例如struct)。
备选答案:
您可以使用MartinR's allValues函数来创建属性组合列表,而不是使用嵌套数组。将[CardShape]添加到需要combinationsinit中,可以用两行代码创建卡:
struct Card {
    var shape = CardShape.none
    var color = CardColor.none
    var number = CardNumber.none
    var shading = CardShading.none

    init(properties: [Property]) {
        for property in properties {
            switch property {
            case let shape as CardShape:
                self.shape = shape
            case let color as CardColor:
                self.color = color
            case let number as CardNumber:
                self.number = number
            case let shading as CardShading:
                self.shading = shading
            default:
                break
            }
        }
    }
}

// https://stackoverflow.com/a/45136672/1630618
func combinations<T>(options: [[T]]) -> AnySequence<[T]> {
    guard let lastOption = options.last else {
        return AnySequence(CollectionOfOne([]))
    }
    let headCombinations = combinations(options: Array(options.dropLast()))
    return AnySequence(headCombinations.lazy.flatMap { head in
        lastOption.lazy.map { head + [$0] }
    })
}


struct SetGame {
    let cards: [Card]

    init(){
        let properties: [Property.Type] = [CardShape.self, CardColor.self, CardNumber.self, CardShading.self]
        cards = combinations(options: properties.map { $0.allValues }).map(Card.init)
    }
}

工作原理:
Card调用[Property]数组中的每个项,用properties.map { $0.allValues }创建allValues
这将传递给properties,它将创建一个包含所有81个属性组合的序列:[[Property]]
[[.square, .triangle, .circle], [.red, .purple, .green], [.one, .two, .three], [.solid, .striped, .outlined]]在这个序列上运行,对每个组合调用combinations,从而生成一个包含81张卡的[[.square, .red, .one, .solid], ..., [.circle, .green, .three, .outlined]]

09-11 18:31
查看更多