crib/game.go
2020-10-03 21:26:32 -07:00

87 lines
1.5 KiB
Go

package main
import (
"fmt"
"strings"
"gitlab.com/tslocum/joker"
)
// Game phases
const (
PhaseEnd = -1
PhaseSetup = 0
PhasePick = 1
PhasePeg = 2
PhaseScore = 3
)
type playerCard struct {
joker.Card
Player int
}
func (c playerCard) String() string {
return fmt.Sprintf("{%d}%s", c.Player, c.Card)
}
type playerCards []playerCard
func (c playerCards) String() string {
var s strings.Builder
for i := range c {
if i > 0 {
s.WriteRune(',')
}
s.WriteString(c[i].String())
}
return s.String()
}
func (c playerCards) Len() int {
return len(c)
}
func (c playerCards) Less(i, j int) bool {
return c[i].Value() < c[j].Value()
}
func (c playerCards) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c playerCards) Cards() joker.Cards {
var cards = make(joker.Cards, len(c))
for i, card := range c {
cards[i] = card.Card
}
return cards
}
func (c playerCards) PlayerCards(player int) int {
var i int
for _, card := range c {
if card.Player == player {
i++
}
}
return i
}
type gameState struct {
Phase int `json:"phase"`
Player int `json:"player"`
Dealer int `json:"dealer"`
Turn int `json:"turn"`
Starter joker.Card `json:"starter"`
Hand1 joker.Cards `json:"hand1"`
Hand2 joker.Cards `json:"hand2"`
Crib joker.Cards `json:"crib"`
Score1 int `json:"score1"`
Score2 int `json:"score2"`
ThrowPile playerCards `json:"throwpile"`
Status string `json:"status"`
}