Structure sorting
This commit is contained in:
53
Sources/App/Model/Card/Card+Array.swift
Normal file
53
Sources/App/Model/Card/Card+Array.swift
Normal file
@ -0,0 +1,53 @@
|
||||
import Foundation
|
||||
|
||||
typealias Trick = [Card]
|
||||
|
||||
typealias Hand = [Card]
|
||||
|
||||
extension Array where Element == Card {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user