crib/game.go

88 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-08-12 23:12:50 +00:00
package main
import (
"fmt"
2020-08-16 23:30:47 +00:00
"strings"
2020-08-12 23:12:50 +00:00
"gitlab.com/tslocum/joker"
)
2020-10-04 04:26:32 +00:00
// Game phases
2020-08-16 23:30:47 +00:00
const (
PhaseEnd = -1
PhaseSetup = 0
PhasePick = 1
PhasePeg = 2
PhaseScore = 3
)
2020-10-04 04:26:32 +00:00
type playerCard struct {
2020-08-12 23:12:50 +00:00
joker.Card
Player int
}
2020-10-04 04:26:32 +00:00
func (c playerCard) String() string {
2020-08-12 23:12:50 +00:00
return fmt.Sprintf("{%d}%s", c.Player, c.Card)
}
2020-10-04 04:26:32 +00:00
type playerCards []playerCard
2020-08-12 23:12:50 +00:00
2020-10-04 04:26:32 +00:00
func (c playerCards) String() string {
2020-08-16 23:30:47 +00:00
var s strings.Builder
for i := range c {
if i > 0 {
s.WriteRune(',')
}
s.WriteString(c[i].String())
}
return s.String()
}
2020-10-04 04:26:32 +00:00
func (c playerCards) Len() int {
2020-08-16 23:30:47 +00:00
return len(c)
}
2020-10-04 04:26:32 +00:00
func (c playerCards) Less(i, j int) bool {
2020-08-16 23:30:47 +00:00
return c[i].Value() < c[j].Value()
}
2020-10-04 04:26:32 +00:00
func (c playerCards) Swap(i, j int) {
2020-08-16 23:30:47 +00:00
c[i], c[j] = c[j], c[i]
}
2020-10-04 04:26:32 +00:00
func (c playerCards) Cards() joker.Cards {
2020-08-16 23:30:47 +00:00
var cards = make(joker.Cards, len(c))
for i, card := range c {
cards[i] = card.Card
}
return cards
}
2020-10-04 04:26:32 +00:00
func (c playerCards) PlayerCards(player int) int {
2020-08-16 23:30:47 +00:00
var i int
for _, card := range c {
if card.Player == player {
i++
}
}
return i
}
2020-10-04 04:26:32 +00:00
type gameState struct {
2020-08-12 23:12:50 +00:00
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"`
2020-08-15 20:25:20 +00:00
Crib joker.Cards `json:"crib"`
2020-08-12 23:12:50 +00:00
Score1 int `json:"score1"`
Score2 int `json:"score2"`
2020-10-04 04:26:32 +00:00
ThrowPile playerCards `json:"throwpile"`
2020-08-16 23:30:47 +00:00
Status string `json:"status"`
2020-08-12 23:12:50 +00:00
}