Move to entity structs
parent
3a8985486a
commit
3e71ab6217
|
|
@ -0,0 +1,6 @@
|
|||
package entity
|
||||
|
||||
type Current struct {
|
||||
Bet Currency
|
||||
Turn int
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package entity
|
||||
|
||||
import "math/rand"
|
||||
|
||||
type Deck struct {
|
||||
Deck []Card
|
||||
Discard []Card
|
||||
}
|
||||
|
||||
func (d *Deck) Shuffle() {
|
||||
shuffle(d.Deck)
|
||||
}
|
||||
|
||||
func (d *Deck) Draw() Card {
|
||||
if len(d.Deck) == 0 {
|
||||
d.ShuffleInDiscard()
|
||||
}
|
||||
if len(d.Deck) == 0 {
|
||||
panic("cannot draw from an empty deck")
|
||||
}
|
||||
card := d.Deck[len(d.Deck)-1]
|
||||
d.Deck = d.Deck[:len(d.Deck)-1]
|
||||
return card
|
||||
}
|
||||
|
||||
func (d *Deck) ShuffleInDiscard() {
|
||||
if len(d.Discard) == 0 {
|
||||
return
|
||||
}
|
||||
for i := range d.Discard {
|
||||
d.Deck = append(d.Deck, d.Discard[i])
|
||||
}
|
||||
d.Discard = d.Discard[:0]
|
||||
shuffle(d.Deck)
|
||||
}
|
||||
|
||||
func shuffle(cards []Card) {
|
||||
if len(cards) == 0 {
|
||||
return
|
||||
}
|
||||
switches := rand.Intn(100) + 100
|
||||
for i := 0; i < switches; i++ {
|
||||
a := rand.Intn(len(cards))
|
||||
b := rand.Intn(len(cards))
|
||||
j := cards[a]
|
||||
cards[a] = cards[b]
|
||||
cards[b] = j
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,91 @@ package entity
|
|||
import "fmt"
|
||||
|
||||
type Game struct {
|
||||
Pot Currency
|
||||
Turn int
|
||||
Players Players
|
||||
Log string
|
||||
Deck Deck
|
||||
Current Current
|
||||
}
|
||||
|
||||
func (game Game) Equals(gameB Game) bool {
|
||||
return fmt.Sprint(game) != fmt.Sprint(gameB)
|
||||
}
|
||||
|
||||
func (game Game) Pot() Currency {
|
||||
var pot Currency
|
||||
for _, player := range game.Players {
|
||||
pot += player.Pot
|
||||
pot += player.Bet
|
||||
}
|
||||
return pot
|
||||
}
|
||||
|
||||
func (game Game) ActivePlayers() []*Player {
|
||||
players := make([]*Player, 0)
|
||||
for i := range game.Players {
|
||||
if game.Players[i].Active {
|
||||
players = append(players, &game.Players[i])
|
||||
}
|
||||
}
|
||||
return players
|
||||
}
|
||||
|
||||
func (game *Game) NextTurn() {
|
||||
if len(game.ActivePlayers()) < 2 {
|
||||
return
|
||||
}
|
||||
foo := func() {
|
||||
game.Current.Turn = (game.Current.Turn + 1) % len(game.Players)
|
||||
}
|
||||
foo()
|
||||
for !game.Players[game.Current.Turn].Active {
|
||||
foo()
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Game) NextPhase() {
|
||||
for i := range game.Players {
|
||||
game.Players[i].Checked = false
|
||||
game.Players[i].Pot += game.Players[i].Bet
|
||||
game.Players[i].Bet = 0
|
||||
}
|
||||
game.Current.Bet = 0
|
||||
}
|
||||
|
||||
func (game *Game) ChargeActivePlayers(v Currency) {
|
||||
for _, player := range game.ActivePlayers() {
|
||||
if player.Balance < v {
|
||||
player.Active = false
|
||||
} else {
|
||||
player.Balance -= v
|
||||
player.Pot += v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Game) Deal(n int) {
|
||||
for i := range game.Players {
|
||||
if game.Players[i].Active {
|
||||
for j := 0; j < n; j++ {
|
||||
card := game.Deck.Draw()
|
||||
game.Players[i].Hand.Push(card)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (game Game) IsPotRight() bool {
|
||||
potsRight := true
|
||||
for _, player := range game.ActivePlayers() {
|
||||
potsRight = potsRight && (player.Balance == 0 || player.Bet == game.Current.Bet)
|
||||
}
|
||||
return potsRight
|
||||
}
|
||||
|
||||
func (game Game) IsAllActivePlayersChecked() bool {
|
||||
allChecks := true
|
||||
for _, player := range game.ActivePlayers() {
|
||||
allChecks = allChecks && player.Checked
|
||||
}
|
||||
return allChecks
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package entity
|
||||
|
||||
type Hand struct {
|
||||
Public []Card
|
||||
Private []Card
|
||||
}
|
||||
|
||||
func (hand *Hand) Push(card Card) {
|
||||
if hand.Public == nil {
|
||||
hand.Public = make([]Card, 0, 1)
|
||||
}
|
||||
hand.Public = append(hand.Public, card)
|
||||
}
|
||||
|
||||
func (hand Hand) Len() int {
|
||||
return len(hand.Public) + len(hand.Private)
|
||||
}
|
||||
|
|
@ -3,13 +3,10 @@ package entity
|
|||
type Player struct {
|
||||
ID string
|
||||
Name string
|
||||
Card Card
|
||||
Hand Hand
|
||||
Balance Currency
|
||||
Pot Currency
|
||||
Bet Currency
|
||||
Active bool
|
||||
Checked bool
|
||||
}
|
||||
|
||||
func (p Player) Empty() bool {
|
||||
return p == (Player{})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
package entity
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlayerEmpty(t *testing.T) {
|
||||
var p Player
|
||||
if !p.Empty() {
|
||||
t.Fatal(p)
|
||||
}
|
||||
p.ID = "id"
|
||||
if p.Empty() {
|
||||
t.Fatal(p)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package rule
|
||||
|
||||
import "local/sandbox/cards/src/game/rule/operation"
|
||||
|
||||
type End operation.Bool
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package rule
|
||||
|
||||
import "local/sandbox/cards/src/game/rule/operation"
|
||||
|
||||
type Hand operation.Int
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package rule
|
||||
|
||||
import "local/sandbox/cards/src/game/rule/operation"
|
||||
|
||||
type Phase operation.Bool
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package rule
|
||||
|
||||
type Rule struct {
|
||||
Ends []End
|
||||
Phases []Phase
|
||||
Hands []Hand
|
||||
}
|
||||
|
|
@ -45,9 +45,9 @@ func TestMasterUpdate(t *testing.T) {
|
|||
}
|
||||
|
||||
game.Players.Add(entity.Player{
|
||||
ID: "hi",
|
||||
ID: "hi",
|
||||
Pot: 123,
|
||||
})
|
||||
game.Pot = 123
|
||||
err = gm.ReplaceGame(id, game)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
Loading…
Reference in New Issue