Sync push
This commit is contained in:
49
Sources/App/Model/CardOrders/CardOrder.swift
Normal file
49
Sources/App/Model/CardOrders/CardOrder.swift
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by iMac on 03.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum CardOrderType {
|
||||
|
||||
/// The sorting for most games, where heart is trump
|
||||
case normal
|
||||
|
||||
case wenz
|
||||
|
||||
case geier
|
||||
|
||||
case soloEichel
|
||||
|
||||
case soloBlatt
|
||||
|
||||
case soloSchelln
|
||||
}
|
||||
|
||||
protocol CardOrder {
|
||||
|
||||
static var trumpOrder: [Card] { get }
|
||||
|
||||
static var sortIndex: [Card : Int] { get }
|
||||
}
|
||||
|
||||
extension CardOrder {
|
||||
|
||||
static func consecutiveTrumps(_ cards: [Card]) -> Int {
|
||||
var count = 0
|
||||
while cards.contains(trumpOrder[count]) {
|
||||
count += 1
|
||||
}
|
||||
guard count >= 3 else {
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
static func sort(_ cards: [Card]) -> [Card] {
|
||||
cards.sorted { sortIndex[$0]! < sortIndex[$1]! }
|
||||
}
|
||||
}
|
58
Sources/App/Model/CardOrders/NormalCardOrder.swift
Normal file
58
Sources/App/Model/CardOrders/NormalCardOrder.swift
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by iMac on 03.12.21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct NormalCardOrder: CardOrder {
|
||||
|
||||
private static let cardOrder = [
|
||||
Card(.eichel, .ober),
|
||||
Card(.blatt, .ober),
|
||||
Card(.herz, .ober),
|
||||
Card(.schelln, .ober),
|
||||
Card(.eichel, .unter),
|
||||
Card(.blatt, .unter),
|
||||
Card(.herz, .unter),
|
||||
Card(.schelln, .unter),
|
||||
Card(.herz, .ass),
|
||||
Card(.herz, .zehn),
|
||||
Card(.herz, .könig),
|
||||
Card(.herz, .neun),
|
||||
Card(.herz, .acht),
|
||||
Card(.herz, .sieben),
|
||||
Card(.eichel, .ass),
|
||||
Card(.eichel, .zehn),
|
||||
Card(.eichel, .könig),
|
||||
Card(.eichel, .neun),
|
||||
Card(.eichel, .acht),
|
||||
Card(.eichel, .sieben),
|
||||
Card(.blatt, .ass),
|
||||
Card(.blatt, .zehn),
|
||||
Card(.blatt, .könig),
|
||||
Card(.blatt, .neun),
|
||||
Card(.blatt, .acht),
|
||||
Card(.blatt, .sieben),
|
||||
Card(.schelln, .ass),
|
||||
Card(.schelln, .zehn),
|
||||
Card(.schelln, .könig),
|
||||
Card(.schelln, .neun),
|
||||
Card(.schelln, .acht),
|
||||
Card(.schelln, .sieben),
|
||||
]
|
||||
|
||||
private static let sortIndex: [Card : Int] = {
|
||||
cardOrder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
|
||||
}()
|
||||
|
||||
static let trumpOrder: [Card] = Array(cardOrder[0..<8])
|
||||
|
||||
private static let trumps: Set<Card> = Set(trumpOrder)
|
||||
|
||||
static func trumpCount(_ cards: [Card]) -> Int {
|
||||
cards.filter { trumps.contains(card) }.count
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user