Schafkopf-Server/Sources/App/Model/Game.swift

71 lines
1.8 KiB
Swift
Raw Normal View History

2021-12-01 22:49:54 +01:00
import Foundation
typealias Hand = [Card]
/// The number of tricks ("Stich") per game
let tricksPerGame = 8
struct Game: Codable {
let type: GameType
let numberOfDoubles: Int
/// The player(s) leading the game, i.e. they lose on 60-60
let leaders: [Int]
/// The remaining cards for all players
var cards: [Hand]
/// The total number of consecutive trump cards for one party (starts counting at 3)
let consecutiveTrumps: Int
var lastTrickWinner: Int
var currentActor: Int
/// The finished tricks, with each trick sorted according to the player order of the table
var completedTricks: [Trick]
init(type: GameType, doubles: Int, cards: [Hand], leaders: [Int], starter: Int) {
self.type = type
self.numberOfDoubles = doubles
self.cards = cards
self.leaders = leaders
2021-12-03 18:03:29 +01:00
self.consecutiveTrumps = Array(leaders.map { cards[$0] }.joined())
.consecutiveTrumps(for: type)
2021-12-01 22:49:54 +01:00
self.currentActor = starter
self.lastTrickWinner = starter
self.completedTricks = []
}
var isAtEnd: Bool {
completedTricks.count == tricksPerGame
}
var hasNotStarted: Bool {
!cards.contains { !$0.isEmpty }
}
var pointsOfLeaders: Int {
leaders.map(pointsOfPlayer).reduce(0, +)
}
func pointsOfPlayer(index: Int) -> Int {
completedTricks
.filter { $0.winnerIndex(forGameType: type) == index }
.map { $0.points }
.reduce(0, +)
}
/// The cost of the game, in cents
var cost: Int {
// TODO: Add läufer and schwarz, schneider
type.basicCost * costMultiplier
}
var costMultiplier: Int {
2 ^^ numberOfDoubles
}
}