枚举的可迭代数组

Iterable Array of Enums

我正在尝试初始化一副纸牌。我的卡片结构中有卡片属性。我的方法是尝试创建一个 "enum states" 数组,然后遍历这些数组以初始化每张卡片。我在这样做时遇到了问题。

游戏Class

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)
        }
    }
}

卡片Class

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)
            }
        }
    }
}

我认为您的 Card struct 有点太复杂了。如果你改变你的表示,创建卡片会更容易。

让你的卡片代表不同的属性作为它们自己的 属性:

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))
            }
        }
    }
}

备注:

  • 您的枚举应以大写字符开头,您的枚举值应以小写字符开头。
  • 为每个属性使用单独的属性将使检查卡片之间的匹配属性变得更加容易。
  • 默认情况下,您会获得一个初始化所有属性的初始化程序。通过嵌套循环初始化它们,您将能够创建所有可能的卡片。
  • 将您的 allValues 属性更改为特定属性类型的 return 数组(例如 [CardShape])。

备选答案:

您可以使用 函数来创建属性组合列表,而不是使用嵌套数组。添加一个 init 到需要 [Property]Card,您可以用两行代码创建卡片:

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
            }
        }
    }
}

// 
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 + [[=13=]] }
    })
}


struct SetGame {
    let cards: [Card]

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

这是如何工作的:

  1. properties.map { [=22=].allValues }properties 数组的每个项目调用 allValues,创建一个 [[Property]][[.square, .triangle, .circle], [.red, .purple, .green], [.one, .two, .three], [.solid, .striped, .outlined]]
  2. 这将传递给 combinations,它创建一个包含这些属性的所有 81 种组合的序列:[[.square, .red, .one, .solid], ..., [.circle, .green, .three, .outlined]].
  3. map 在这个序列上是 运行,每个组合调用 Card.init,结果是 [Card] 有 81 张牌。