Schafkopf-Server/Sources/App/Model/Card/Card+Array.swift

62 lines
1.4 KiB
Swift
Raw Normal View History

2021-12-06 11:42:15 +01:00
import Foundation
typealias Trick = [Card]
typealias Hand = [Card]
extension Array where Element == Card {
2021-12-18 15:08:43 +01:00
var unplayable: [PlayableCard] {
map { $0.unplayable }
}
var playable: [PlayableCard] {
map { $0.playable }
}
2021-12-06 11:42:15 +01:00
var points: Int {
map { $0.points }
.reduce(0, +)
}
func highCardIndex(forGame game: GameType) -> Int {
game.sortingType.highCardIndex(cards: self)
}
func sortedCards(forGame game: GameType) -> [Card] {
sortedCards(order: game.sortingType)
}
func sortedCards(order: CardOrder.Type) -> [Card] {
order.sort(self)
}
func consecutiveTrumps(for game: GameType) -> Int {
game.sortingType.consecutiveTrumps(self)
}
func trumpCount(for game: GameType) -> Int {
game.sortingType.trumpCount(self)
}
func suitCount(_ suit: Card.Suit, in game: GameType) -> Int {
filter { card in
card.suit == suit && !game.sortingType.isTrump(card)
}.count
}
/**
Split cards into chunks to assign them to players.
- Note: The array must contain a multiple of the `size` parameter
*/
func split(intoChunksOf size: Int) -> [Hand] {
stride(from: 0, to: count, by: size).map { i in
Array(self[i..<i+4])
}
}
var canOfferWedding: Bool {
NormalCardOrder.trumpCount(self) == 1
}
}