diff --git a/src/entity/curernt.go b/src/entity/curernt.go new file mode 100644 index 0000000..67aac6a --- /dev/null +++ b/src/entity/curernt.go @@ -0,0 +1,6 @@ +package entity + +type Current struct { + Bet Currency + Turn int +} diff --git a/src/entity/deck.go b/src/entity/deck.go new file mode 100644 index 0000000..24b9108 --- /dev/null +++ b/src/entity/deck.go @@ -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 + } +} diff --git a/src/entity/game.go b/src/entity/game.go index a659381..465945c 100644 --- a/src/entity/game.go +++ b/src/entity/game.go @@ -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 +} diff --git a/src/entity/hand.go b/src/entity/hand.go new file mode 100644 index 0000000..4dbc8ba --- /dev/null +++ b/src/entity/hand.go @@ -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) +} diff --git a/src/entity/player.go b/src/entity/player.go index ae916af..e721890 100644 --- a/src/entity/player.go +++ b/src/entity/player.go @@ -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{}) -} diff --git a/src/entity/player_test.go b/src/entity/player_test.go deleted file mode 100644 index 04e2547..0000000 --- a/src/entity/player_test.go +++ /dev/null @@ -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) - } -} diff --git a/src/game/rule/end.go b/src/game/rule/end.go new file mode 100644 index 0000000..7644ef7 --- /dev/null +++ b/src/game/rule/end.go @@ -0,0 +1,5 @@ +package rule + +import "local/sandbox/cards/src/game/rule/operation" + +type End operation.Bool diff --git a/src/game/rule/hand.go b/src/game/rule/hand.go new file mode 100644 index 0000000..b4eddc2 --- /dev/null +++ b/src/game/rule/hand.go @@ -0,0 +1,5 @@ +package rule + +import "local/sandbox/cards/src/game/rule/operation" + +type Hand operation.Int diff --git a/src/game/rule/phase.go b/src/game/rule/phase.go new file mode 100644 index 0000000..367f362 --- /dev/null +++ b/src/game/rule/phase.go @@ -0,0 +1,5 @@ +package rule + +import "local/sandbox/cards/src/game/rule/operation" + +type Phase operation.Bool diff --git a/src/game/rule/rule.go b/src/game/rule/rule.go new file mode 100644 index 0000000..2bf390c --- /dev/null +++ b/src/game/rule/rule.go @@ -0,0 +1,7 @@ +package rule + +type Rule struct { + Ends []End + Phases []Phase + Hands []Hand +} diff --git a/src/game/storage_test.go b/src/game/storage_test.go index 8028c06..4705ad6 100644 --- a/src/game/storage_test.go +++ b/src/game/storage_test.go @@ -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)