From 50a35ece037dd54fd3e0948d6d62b915630e35da Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Dec 2021 11:42:15 +0100 Subject: [PATCH] Structure sorting --- Sources/App/Model/Card/Card+Array.swift | 53 +++++++++++++ Sources/App/Model/CardOrders/CardOrder.swift | 74 ++++++++++++------- .../App/Model/CardOrders/GeierCardOrder.swift | 41 ++++++++++ .../Model/CardOrders/NormalCardOrder.swift | 25 +------ .../Model/CardOrders/SoloBlattCardOrder.swift | 41 ++++++++++ .../CardOrders/SoloEichelCardOrder.swift | 41 ++++++++++ .../CardOrders/SoloSchellnCardOrder.swift | 42 +++++++++++ .../App/Model/CardOrders/WenzCardOrder.swift | 41 ++++++++++ Sources/App/Model/GameType.swift | 14 ++-- 9 files changed, 317 insertions(+), 55 deletions(-) create mode 100644 Sources/App/Model/Card/Card+Array.swift create mode 100644 Sources/App/Model/CardOrders/GeierCardOrder.swift create mode 100644 Sources/App/Model/CardOrders/SoloBlattCardOrder.swift create mode 100644 Sources/App/Model/CardOrders/SoloEichelCardOrder.swift create mode 100644 Sources/App/Model/CardOrders/SoloSchellnCardOrder.swift create mode 100644 Sources/App/Model/CardOrders/WenzCardOrder.swift diff --git a/Sources/App/Model/Card/Card+Array.swift b/Sources/App/Model/Card/Card+Array.swift new file mode 100644 index 0000000..6991fce --- /dev/null +++ b/Sources/App/Model/Card/Card+Array.swift @@ -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.. { Set(trumpOrder) + } + + static func isTrump(_ card: Card) -> Bool { + trumps.contains(card) + } + + static func highCardIndex(cards: [Card]) -> Int { + let high: Card + if isTrump(cards[0]) { + high = sort(cards).first! + } else { + let suit = cards.first!.suit + high = cards.filter { $0.suit == suit } + .sorted { $0.symbol }.first! + } + return cards.firstIndex(of: high)! + } + static func consecutiveTrumps(_ cards: [Card]) -> Int { var count = 0 while cards.contains(trumpOrder[count]) { @@ -46,4 +50,20 @@ extension CardOrder { static func sort(_ cards: [Card]) -> [Card] { cards.sorted { sortIndex[$0]! < sortIndex[$1]! } } + + static func trumpCount(_ cards: [Card]) -> Int { + cards.filter { trumps.contains($0) }.count + } + + static func hasTrump(in cards: [Card]) -> Bool { + cards.contains { trumps.contains($0) } + } + + static func has(suit: Card.Suit, in cards: [Card]) -> Bool { + cards.contains { !isTrump($0) && $0.suit == suit } + } + + static func cards(with suit: Card.Suit, in cards: [Card]) -> [Card] { + cards.filter { !isTrump($0) && $0.suit == suit } + } } diff --git a/Sources/App/Model/CardOrders/GeierCardOrder.swift b/Sources/App/Model/CardOrders/GeierCardOrder.swift new file mode 100644 index 0000000..0f7fe8f --- /dev/null +++ b/Sources/App/Model/CardOrders/GeierCardOrder.swift @@ -0,0 +1,41 @@ +import Foundation + +struct GeierCardOrder: CardOrder { + + static let trumpCount = 4 + + static let cardOrder = [ + Card(.eichel, .ober), + Card(.blatt, .ober), + Card(.herz, .ober), + Card(.schelln, .ober), + Card(.herz, .ass), + Card(.herz, .zehn), + Card(.herz, .könig), + Card(.herz, .unter), + Card(.herz, .neun), + Card(.herz, .acht), + Card(.herz, .sieben), + Card(.eichel, .ass), + Card(.eichel, .zehn), + Card(.eichel, .könig), + Card(.eichel, .unter), + Card(.eichel, .neun), + Card(.eichel, .acht), + Card(.eichel, .sieben), + Card(.blatt, .ass), + Card(.blatt, .zehn), + Card(.blatt, .könig), + Card(.blatt, .unter), + Card(.blatt, .neun), + Card(.blatt, .acht), + Card(.blatt, .sieben), + Card(.schelln, .ass), + Card(.schelln, .zehn), + Card(.schelln, .könig), + Card(.schelln, .unter), + Card(.schelln, .neun), + Card(.schelln, .acht), + Card(.schelln, .sieben) + ] +} diff --git a/Sources/App/Model/CardOrders/NormalCardOrder.swift b/Sources/App/Model/CardOrders/NormalCardOrder.swift index 509c0af..57a6be5 100644 --- a/Sources/App/Model/CardOrders/NormalCardOrder.swift +++ b/Sources/App/Model/CardOrders/NormalCardOrder.swift @@ -1,15 +1,10 @@ -// -// File.swift -// -// -// Created by iMac on 03.12.21. -// - import Foundation struct NormalCardOrder: CardOrder { + + static let trumpCount = 14 - private static let cardOrder = [ + static let cardOrder = [ Card(.eichel, .ober), Card(.blatt, .ober), Card(.herz, .ober), @@ -41,18 +36,6 @@ struct NormalCardOrder: CardOrder { Card(.schelln, .könig), Card(.schelln, .neun), Card(.schelln, .acht), - Card(.schelln, .sieben), + 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 = Set(trumpOrder) - - static func trumpCount(_ cards: [Card]) -> Int { - cards.filter { trumps.contains(card) }.count - } } diff --git a/Sources/App/Model/CardOrders/SoloBlattCardOrder.swift b/Sources/App/Model/CardOrders/SoloBlattCardOrder.swift new file mode 100644 index 0000000..cb7c1b9 --- /dev/null +++ b/Sources/App/Model/CardOrders/SoloBlattCardOrder.swift @@ -0,0 +1,41 @@ +import Foundation + +struct SoloBlattCardOrder: CardOrder { + + static let trumpCount = 14 + + 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(.blatt, .ass), + Card(.blatt, .zehn), + Card(.blatt, .könig), + Card(.blatt, .neun), + Card(.blatt, .acht), + Card(.blatt, .sieben), + Card(.eichel, .ass), + Card(.eichel, .zehn), + Card(.eichel, .könig), + Card(.eichel, .neun), + Card(.eichel, .acht), + Card(.eichel, .sieben), + Card(.herz, .ass), + Card(.herz, .zehn), + Card(.herz, .könig), + Card(.herz, .neun), + Card(.herz, .acht), + Card(.herz, .sieben), + Card(.schelln, .ass), + Card(.schelln, .zehn), + Card(.schelln, .könig), + Card(.schelln, .neun), + Card(.schelln, .acht), + Card(.schelln, .sieben) + ] +} diff --git a/Sources/App/Model/CardOrders/SoloEichelCardOrder.swift b/Sources/App/Model/CardOrders/SoloEichelCardOrder.swift new file mode 100644 index 0000000..511525a --- /dev/null +++ b/Sources/App/Model/CardOrders/SoloEichelCardOrder.swift @@ -0,0 +1,41 @@ +import Foundation + +struct SoloEichelCardOrder: CardOrder { + + static let trumpCount = 14 + + 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(.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(.herz, .ass), + Card(.herz, .zehn), + Card(.herz, .könig), + Card(.herz, .neun), + Card(.herz, .acht), + Card(.herz, .sieben), + Card(.schelln, .ass), + Card(.schelln, .zehn), + Card(.schelln, .könig), + Card(.schelln, .neun), + Card(.schelln, .acht), + Card(.schelln, .sieben) + ] +} diff --git a/Sources/App/Model/CardOrders/SoloSchellnCardOrder.swift b/Sources/App/Model/CardOrders/SoloSchellnCardOrder.swift new file mode 100644 index 0000000..4f43903 --- /dev/null +++ b/Sources/App/Model/CardOrders/SoloSchellnCardOrder.swift @@ -0,0 +1,42 @@ +import Foundation + +struct SoloSchellnCardOrder: CardOrder { + + static let trumpCount = 14 + + 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(.schelln, .ass), + Card(.schelln, .zehn), + Card(.schelln, .könig), + Card(.schelln, .neun), + Card(.schelln, .acht), + Card(.schelln, .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(.herz, .ass), + Card(.herz, .zehn), + Card(.herz, .könig), + Card(.herz, .neun), + Card(.herz, .acht), + Card(.herz, .sieben) + ] + +} diff --git a/Sources/App/Model/CardOrders/WenzCardOrder.swift b/Sources/App/Model/CardOrders/WenzCardOrder.swift new file mode 100644 index 0000000..af6a591 --- /dev/null +++ b/Sources/App/Model/CardOrders/WenzCardOrder.swift @@ -0,0 +1,41 @@ +import Foundation + +struct WenzCardOrder: CardOrder { + + static let trumpCount = 4 + + static let cardOrder = [ + Card(.eichel, .unter), + Card(.blatt, .unter), + Card(.herz, .unter), + Card(.schelln, .unter), + Card(.herz, .ass), + Card(.herz, .zehn), + Card(.herz, .könig), + Card(.herz, .ober), + Card(.herz, .neun), + Card(.herz, .acht), + Card(.herz, .sieben), + Card(.eichel, .ass), + Card(.eichel, .zehn), + Card(.eichel, .könig), + Card(.eichel, .ober), + Card(.eichel, .neun), + Card(.eichel, .acht), + Card(.eichel, .sieben), + Card(.blatt, .ass), + Card(.blatt, .zehn), + Card(.blatt, .könig), + Card(.blatt, .ober), + Card(.blatt, .neun), + Card(.blatt, .acht), + Card(.blatt, .sieben), + Card(.schelln, .ass), + Card(.schelln, .zehn), + Card(.schelln, .könig), + Card(.schelln, .ober), + Card(.schelln, .neun), + Card(.schelln, .acht), + Card(.schelln, .sieben) + ] +} diff --git a/Sources/App/Model/GameType.swift b/Sources/App/Model/GameType.swift index dbd3427..3facbe7 100644 --- a/Sources/App/Model/GameType.swift +++ b/Sources/App/Model/GameType.swift @@ -59,20 +59,20 @@ enum GameType: Codable { gameClass.cost } - var sortingType: CardOrderType { + var sortingType: CardOrder.Type { switch self { case .wenz: - return .wenz + return WenzCardOrder.self case .geier: - return .geier + return GeierCardOrder.self case .soloEichel: - return .soloEichel + return SoloEichelCardOrder.self case .soloBlatt: - return .soloBlatt + return SoloBlattCardOrder.self case .soloSchelln: - return .soloSchelln + return SoloSchellnCardOrder.self default: - return .normal + return NormalCardOrder.self } } }