计算 Swift 5 中结构数组数组中的元素数
Counting number of elements in an array of array of struct in Swift 5
作为新手,我真的很苦恼如何解决计算结构数组中元素的问题。我尝试了几种不同的方法,但没有给我所需的结果。
所以我有一个名为 Card 的结构,定义如下:
struct Card: Hashable {
var dateGroup: DateGroup
var countryGroup: CountryGroup
var icon1: IconGroup
var icon2: IconGroup
init(dateGroup: DateGroup, countryGroup: CountryGroup, icon1: IconGroup, icon2: IconGroup) {
self.dateGroup = dateGroup
self.countryGroup = countryGroup
self.icon1 = icon1
self.icon2 = icon2
}
}
作为参考,Card 的元素使用以下代码定义为枚举:
enum DateGroup: String {
case DG1, DG2, DG3
static let allValues = [DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3]
}
enum CountryGroup: String {
case IT, NE, FS, US
static let allValues = [IT, IT, IT, IT, IT, IT, IT, IT, NE, NE, IT, NE, NE, NE, IT, IT, IT, NE, IT, NE, IT, IT, IT, NE, IT, IT, IT, IT, NE, IT, NE, NE, FS, FS, NE, IT, IT, NE, NE, IT, IT, NE, FS, FS, FS, NE, FS, NE, NE, NE, NE, FS, NE, NE, FS, NE, IT, NE, NE, FS, FS, NE, IT, NE, FS, NE, NE, NE, NE, FS, FS, FS, FS, FS, US, FS, FS, FS, FS, NE, FS, FS, FS, NE, US, IT]
}
enum IconGroup: String {
case Portrait, People, Still, Scape, Setting
static let allValues1 = [Portrait, People, People, Portrait, Portrait, People, People, People, People, Portrait, People, Portrait, People, People, People, Portrait, People, Portrait, People, Portrait, Portrait, People, People, Portrait, People, People, Portrait, People, Portrait, People, Portrait, People, Portrait, People, People, People, People, Still, People, Portrait, People, Portrait, Still, People, People, People, Portrait, Still, People, Portrait, Portrait, People, Still, Still, People, Still, People, Portrait, Portrait, People, People, People, People, Portrait, Portrait, People, Scape, Scape, Setting, People, People, Still, People, Still, Setting, Scape, People, People, People, Still, Still, Portrait, People, Portrait, People, People]
static let allValues2 = [Setting, Setting, Setting, Setting, Setting, Setting, Setting, Scape, Setting, Setting, Scape, Scape, Setting, Setting, Scape, Scape, Setting, Scape, Setting, Scape, Scape, Scape, Scape, Setting, Scape, Scape, Setting, Scape, Setting, Setting, Portrait, Setting, Setting, Setting, Scape, Portrait, Setting, Still, Scape, Setting, Scape, Setting, Still, Scape, Scape, Scape, Setting, Still, Scape, Portrait, Setting, Setting, Still, Still, Scape, Still, Scape, Portrait, Scape, Setting, Scape, Setting, Scape, Setting, Setting, Scape, Scape, Scape, Scape, Scape, Scape, Still, Scape, Still, Still, Scape, Setting, Setting, Scape, Still, Still, Setting, Setting, Setting, Setting, Setting]
}
我创建了一个名为 DeckOfCards 的 class,其功能是洗牌并向指定数量的玩家发牌。我最终完成了所有工作,并通过在控制台中打印结果进行了检查,并且全部检查完毕。
所以每个玩家都有 10 张牌,并且被定义为 Card 类型的数组(另外,玩家也存储在一个名为 newGame 的数组中)。所以 newGame[0] 是玩家 1,newGame[0][0] 是玩家手中的第一张牌。因此,我可以通过 newgame[0][0].dateGroup 引用该卡的日期组,例如。
我想要实现的是检查玩家是否有一套 5 张或更多张牌,其中两张牌的元素相同。我首先尝试对玩家手中的卡片元素进行计数。因此,例如,有多少卡片有 DG1 的日期组,DG2 等有多少,国家组有多少 IT 等。
我尝试过使用 joined() 然后进行计数,但没有成功。我正在考虑返回一个数组,其中包含每种可能的元素类型的计数,并从这个非常骨架的代码开始:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
//insert counting code here
return counts
}
所以我会得到一个数组,其中包含 [DG1: 3, DG2: 3, ....... IT: 4, NE: 5, .... Portrait: 6, People: 4、.....]
求助!有什么想法吗?
罗布
这是您要查找的函数:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
//insert counting code here
//=========================
for player in playerHand {
if(!counts.keys.contains(player.dateGroup.rawValue)) {
counts[player.dateGroup.rawValue] = 0;
}
counts[player.dateGroup.rawValue]! += 1;
}
//=========================
return counts
}
使用属性的 rawValue
和字典查找中的 default
总数 0
来计算总值:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
for card in playerHand {
counts[card.dateGroup.rawValue, default: 0] += 1
counts[card.countryGroup.rawValue, default: 0] += 1
counts[card.icon1.rawValue, default: 0] += 1
// Don't count icon2 if it is the same as icon1
if card.icon2 != card.icon1 {
counts[card.icon2.rawValue, default: 0] += 1
}
}
return counts
}
正在创建带有图标的卡片索引数组:
您可以创建一个玩家手牌索引数组,而不是创建带有匹配图标的牌数。首先将 .enumerated
添加到 playerHand
以获取索引,然后将 idx
附加到数组。这里我们使用 []
作为默认字典查找值,如果不存在则创建一个空数组。
func createIconCount(playerHand: [Card] ) -> [String : [Int]] {
var counts: [String: [Int]] = [:]
for (idx, card) in playerHand.enumerated() {
counts[card.dateGroup.rawValue, default: []].append(idx)
counts[card.countryGroup.rawValue, default: []].append(idx)
counts[card.icon1.rawValue, default: []].append(idx)
// Don't count icon2 if it is the same as icon1
if card.icon2 != card.icon1 {
counts[card.icon2.rawValue, default: []].append(idx)
}
}
return counts
}
作为新手,我真的很苦恼如何解决计算结构数组中元素的问题。我尝试了几种不同的方法,但没有给我所需的结果。
所以我有一个名为 Card 的结构,定义如下:
struct Card: Hashable {
var dateGroup: DateGroup
var countryGroup: CountryGroup
var icon1: IconGroup
var icon2: IconGroup
init(dateGroup: DateGroup, countryGroup: CountryGroup, icon1: IconGroup, icon2: IconGroup) {
self.dateGroup = dateGroup
self.countryGroup = countryGroup
self.icon1 = icon1
self.icon2 = icon2
}
}
作为参考,Card 的元素使用以下代码定义为枚举:
enum DateGroup: String {
case DG1, DG2, DG3
static let allValues = [DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG1, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG2, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3, DG3]
}
enum CountryGroup: String {
case IT, NE, FS, US
static let allValues = [IT, IT, IT, IT, IT, IT, IT, IT, NE, NE, IT, NE, NE, NE, IT, IT, IT, NE, IT, NE, IT, IT, IT, NE, IT, IT, IT, IT, NE, IT, NE, NE, FS, FS, NE, IT, IT, NE, NE, IT, IT, NE, FS, FS, FS, NE, FS, NE, NE, NE, NE, FS, NE, NE, FS, NE, IT, NE, NE, FS, FS, NE, IT, NE, FS, NE, NE, NE, NE, FS, FS, FS, FS, FS, US, FS, FS, FS, FS, NE, FS, FS, FS, NE, US, IT]
}
enum IconGroup: String {
case Portrait, People, Still, Scape, Setting
static let allValues1 = [Portrait, People, People, Portrait, Portrait, People, People, People, People, Portrait, People, Portrait, People, People, People, Portrait, People, Portrait, People, Portrait, Portrait, People, People, Portrait, People, People, Portrait, People, Portrait, People, Portrait, People, Portrait, People, People, People, People, Still, People, Portrait, People, Portrait, Still, People, People, People, Portrait, Still, People, Portrait, Portrait, People, Still, Still, People, Still, People, Portrait, Portrait, People, People, People, People, Portrait, Portrait, People, Scape, Scape, Setting, People, People, Still, People, Still, Setting, Scape, People, People, People, Still, Still, Portrait, People, Portrait, People, People]
static let allValues2 = [Setting, Setting, Setting, Setting, Setting, Setting, Setting, Scape, Setting, Setting, Scape, Scape, Setting, Setting, Scape, Scape, Setting, Scape, Setting, Scape, Scape, Scape, Scape, Setting, Scape, Scape, Setting, Scape, Setting, Setting, Portrait, Setting, Setting, Setting, Scape, Portrait, Setting, Still, Scape, Setting, Scape, Setting, Still, Scape, Scape, Scape, Setting, Still, Scape, Portrait, Setting, Setting, Still, Still, Scape, Still, Scape, Portrait, Scape, Setting, Scape, Setting, Scape, Setting, Setting, Scape, Scape, Scape, Scape, Scape, Scape, Still, Scape, Still, Still, Scape, Setting, Setting, Scape, Still, Still, Setting, Setting, Setting, Setting, Setting]
}
我创建了一个名为 DeckOfCards 的 class,其功能是洗牌并向指定数量的玩家发牌。我最终完成了所有工作,并通过在控制台中打印结果进行了检查,并且全部检查完毕。
所以每个玩家都有 10 张牌,并且被定义为 Card 类型的数组(另外,玩家也存储在一个名为 newGame 的数组中)。所以 newGame[0] 是玩家 1,newGame[0][0] 是玩家手中的第一张牌。因此,我可以通过 newgame[0][0].dateGroup 引用该卡的日期组,例如。
我想要实现的是检查玩家是否有一套 5 张或更多张牌,其中两张牌的元素相同。我首先尝试对玩家手中的卡片元素进行计数。因此,例如,有多少卡片有 DG1 的日期组,DG2 等有多少,国家组有多少 IT 等。
我尝试过使用 joined() 然后进行计数,但没有成功。我正在考虑返回一个数组,其中包含每种可能的元素类型的计数,并从这个非常骨架的代码开始:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
//insert counting code here
return counts
}
所以我会得到一个数组,其中包含 [DG1: 3, DG2: 3, ....... IT: 4, NE: 5, .... Portrait: 6, People: 4、.....]
求助!有什么想法吗?
罗布
这是您要查找的函数:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
//insert counting code here
//=========================
for player in playerHand {
if(!counts.keys.contains(player.dateGroup.rawValue)) {
counts[player.dateGroup.rawValue] = 0;
}
counts[player.dateGroup.rawValue]! += 1;
}
//=========================
return counts
}
使用属性的 rawValue
和字典查找中的 default
总数 0
来计算总值:
func createIconCount(playerHand: [Card] ) -> [String : Int] {
var counts: [String: Int] = [:]
for card in playerHand {
counts[card.dateGroup.rawValue, default: 0] += 1
counts[card.countryGroup.rawValue, default: 0] += 1
counts[card.icon1.rawValue, default: 0] += 1
// Don't count icon2 if it is the same as icon1
if card.icon2 != card.icon1 {
counts[card.icon2.rawValue, default: 0] += 1
}
}
return counts
}
正在创建带有图标的卡片索引数组:
您可以创建一个玩家手牌索引数组,而不是创建带有匹配图标的牌数。首先将 .enumerated
添加到 playerHand
以获取索引,然后将 idx
附加到数组。这里我们使用 []
作为默认字典查找值,如果不存在则创建一个空数组。
func createIconCount(playerHand: [Card] ) -> [String : [Int]] {
var counts: [String: [Int]] = [:]
for (idx, card) in playerHand.enumerated() {
counts[card.dateGroup.rawValue, default: []].append(idx)
counts[card.countryGroup.rawValue, default: []].append(idx)
counts[card.icon1.rawValue, default: []].append(idx)
// Don't count icon2 if it is the same as icon1
if card.icon2 != card.icon1 {
counts[card.icon2.rawValue, default: []].append(idx)
}
}
return counts
}