Add card dealing

This commit is contained in:
Christoph Hagen
2021-12-01 22:49:54 +01:00
parent 1ee3fbc0ab
commit cdf079698e
10 changed files with 499 additions and 8 deletions

View File

@ -0,0 +1,71 @@
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
self.consecutiveTrumps = Dealer.consecutiveTrumps(
in: leaders.map { cards[$0] }.joined(),
for: type)
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
}
}

View File

@ -0,0 +1,22 @@
import Foundation
/**
The phase of a table
*/
enum GamePhase: String, Codable {
/// The table is not yet full, so no game can be started
case waitingForPlayers = "waiting"
/// The players are specifying if they want to double ("legen")
case collectingDoubles = "doubles"
/// The game negotiation is ongoing
case bidding = "bidding"
/// The game is in progress
case playing = "play"
/// The game is over
case gameFinished = "done"
}

View File

@ -0,0 +1,16 @@
import Foundation
typealias Trick = [Card]
extension Trick {
func winnerIndex(forGameType type: GameType) -> Int {
let highCard = Dealer.sort(cards: self, using: type.sortingType).first!
return firstIndex(of: highCard)!
}
var points: Int {
map { $0.points }
.reduce(0, +)
}
}