From 46563f9115eb939bad90da2953719a2671450d93 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sun, 21 Jan 2024 04:16:48 -0600 Subject: [PATCH] unexport game state fields --- game/game.go | 120 ++++++++++++++++++++++++------------------------- game/item.go | 16 +++---- game/player.go | 24 +++++----- 3 files changed, 80 insertions(+), 80 deletions(-) diff --git a/game/game.go b/game/game.go index 1d7ab45..6d509be 100644 --- a/game/game.go +++ b/game/game.go @@ -8,28 +8,28 @@ import ( ) type Game struct { - RNG RNG - PP [2]Player - Shells []bool - Round uint - Group uint - Turn uint - HP int8 - Damage int8 - Reveal bool - Prev *bool + rng RNG + players [2]Player + shells []bool + round uint + group uint + turn uint + hp int8 + damage int8 + reveal bool + prev *bool // Backing storage for shells. - ShellArray [8]bool + shellArray [8]bool } // New creates a new game started at round 1. func New(dealer, challenger player.ID) *Game { g := &Game{ - RNG: NewRNG(), - PP: [2]Player{ - {ID: dealer}, - {ID: challenger}, + rng: NewRNG(), + players: [2]Player{ + {id: dealer}, + {id: challenger}, }, } g.StartRound() @@ -38,42 +38,42 @@ func New(dealer, challenger player.ID) *Game { // StartRound starts the next round of a game. func (g *Game) StartRound() { - g.HP = int8(g.RNG.Intn(3) + 2) - g.PP[0].StartRound(g.HP) - g.PP[1].StartRound(g.HP) - g.Round++ - g.Group = 0 + g.hp = int8(g.rng.Intn(3) + 2) + g.players[0].StartRound(g.hp) + g.players[1].StartRound(g.hp) + g.round++ + g.group = 0 g.StartGroup() } // StartGroup starts the next shell group of a round. func (g *Game) StartGroup() { - items := g.RNG.Intn(4) + 1 - g.PP[0].StartGroup(&g.RNG, items) - g.PP[1].StartGroup(&g.RNG, items) - shells := g.RNG.Intn(6) + 2 + items := g.rng.Intn(4) + 1 + g.players[0].StartGroup(&g.rng, items) + g.players[1].StartGroup(&g.rng, items) + shells := g.rng.Intn(6) + 2 for i := 0; i < shells/2; i++ { - g.ShellArray[i] = true + g.shellArray[i] = true } for i := shells / 2; i < shells; i++ { - g.ShellArray[i] = false + g.shellArray[i] = false } - g.Shells = g.ShellArray[:shells] - ShuffleSlice(&g.RNG, g.Shells) - g.Group++ - g.Turn = 0 - g.Prev = nil + g.shells = g.shellArray[:shells] + ShuffleSlice(&g.rng, g.shells) + g.group++ + g.turn = 0 + g.prev = nil g.NextTurn() } // NextTurn advances the turn. func (g *Game) NextTurn() { - g.Turn++ - g.Damage = 1 - g.Reveal = false + g.turn++ + g.damage = 1 + g.reveal = false cur := g.CurrentPlayer() - skip := cur.Cuffs == CuffedSkip - cur.Cuffs = cur.Cuffs.NextState() + skip := cur.cuffs == CuffedSkip + cur.cuffs = cur.cuffs.NextState() if skip { g.NextTurn() } @@ -81,12 +81,12 @@ func (g *Game) NextTurn() { // CurrentPlayer gets the index of the current player, either 0 or 1. func (g *Game) CurrentPlayer() *Player { - return &g.PP[g.Turn%2] + return &g.players[g.turn%2] } // Opponent returns the player who is not the current player. func (g *Game) Opponent() *Player { - return &g.PP[g.Turn%2^1] + return &g.players[g.turn%2^1] } // Apply uses an item by index for the current player. @@ -94,14 +94,14 @@ func (g *Game) Opponent() *Player { // Returns ErrWrongTurn if id does not correspond to the current player. func (g *Game) Apply(id player.ID, item int) error { cur := g.CurrentPlayer() - if cur.ID != id { + if cur.id != id { return ErrWrongTurn } - if item < 0 || item >= len(cur.Items) { + if item < 0 || item >= len(cur.items) { return errors.New("item index out of bounds") } - if cur.Items[item].Apply(g) { - cur.Items[item] = ItemNone + if cur.items[item].Apply(g) { + cur.items[item] = ItemNone } return nil } @@ -110,32 +110,32 @@ func (g *Game) Apply(id player.ID, item int) error { // This may cause the shotgun to be empty, but does not start the next group // if so. func (g *Game) PopShell() bool { - g.Prev = &g.Shells[0] - g.Shells = g.Shells[1:] - return *g.Prev + g.prev = &g.shells[0] + g.shells = g.shells[1:] + return *g.prev } // Peek returns the current turn's shell if it is revealed for the player with // the given ID, or nil otherwise. func (g *Game) Peek(id player.ID) *bool { - if len(g.Shells) == 0 || id != g.CurrentPlayer().ID || !g.Reveal { + if len(g.shells) == 0 || id != g.CurrentPlayer().id || !g.reveal { return nil } - return &g.Shells[0] + return &g.shells[0] } // Empty returns whether the shotgun is empty. func (g *Game) Empty() bool { - return len(g.Shells) == 0 + return len(g.shells) == 0 } // Winner returns the player who won, or nil if the round is not over. func (g *Game) Winner() *Player { - if g.PP[0].HP <= 0 { - return &g.PP[1] + if g.players[0].hp <= 0 { + return &g.players[1] } - if g.PP[1].HP <= 0 { - return &g.PP[0] + if g.players[1].hp <= 0 { + return &g.players[0] } return nil } @@ -146,7 +146,7 @@ func (g *Game) Winner() *Player { // Returns ErrWrongTurn if id does not correspond to the current player. func (g *Game) Shoot(id player.ID, self bool) error { cur := g.CurrentPlayer() - if cur.ID != id { + if cur.id != id { return ErrWrongTurn } target := g.Opponent() @@ -155,7 +155,7 @@ func (g *Game) Shoot(id player.ID, self bool) error { } live := g.PopShell() if live { - target.HP -= g.Damage + target.hp -= g.damage g.NextTurn() } else if !self { g.NextTurn() @@ -167,20 +167,20 @@ func (g *Game) Shoot(id player.ID, self bool) error { func (g *Game) DTO(id player.ID) serve.Game { return serve.Game{ Players: [2]serve.Player{ - g.PP[0].DTO(), - g.PP[1].DTO(), + g.players[0].DTO(), + g.players[1].DTO(), }, - Round: g.Round, - Damage: g.Damage, + Round: g.round, + Damage: g.damage, Shell: g.Peek(id), - Previous: g.Prev, + Previous: g.prev, } } // ShellCounts returns the number of live and blank shells. func (g *Game) ShellCounts() serve.ShellCounts { var counts serve.ShellCounts - for _, s := range g.Shells { + for _, s := range g.shells { if s { counts.Live++ } else { diff --git a/game/item.go b/game/item.go index c0ed303..2777887 100644 --- a/game/item.go +++ b/game/item.go @@ -20,15 +20,15 @@ func (i Item) Apply(g *Game) bool { case ItemNone: return false case ItemLens: - if g.Reveal { + if g.reveal { return false } - g.Reveal = true + g.reveal = true return true case ItemCig: cur := g.CurrentPlayer() - if cur.HP < g.HP { - cur.HP++ + if cur.hp < g.hp { + cur.hp++ return true } return false @@ -40,16 +40,16 @@ func (i Item) Apply(g *Game) bool { return true case ItemCuff: opp := g.Opponent() - if opp.Cuffs != Uncuffed { + if opp.cuffs != Uncuffed { return false } - opp.Cuffs = Cuffed + opp.cuffs = Cuffed return true case ItemKnife: - if g.Damage != 1 { + if g.damage != 1 { return false } - g.Damage = 2 + g.damage = 2 return true default: panic("shotgun: unknown item") diff --git a/game/player.go b/game/player.go index f8702db..14a41cc 100644 --- a/game/player.go +++ b/game/player.go @@ -8,35 +8,35 @@ import ( ) type Player struct { - ID player.ID - HP int8 - Items [8]Item - Cuffs CuffState + id player.ID + hp int8 + items [8]Item + cuffs CuffState } func (p *Player) StartRound(hp int8) { - p.HP = hp - clear(p.Items[:]) + p.hp = hp + clear(p.items[:]) } func (p *Player) StartGroup(rng *RNG, items int) { for i := 0; i < items; i++ { - k := slices.Index(p.Items[:], ItemNone) + k := slices.Index(p.items[:], ItemNone) if k < 0 { break } - p.Items[k] = NewItem(rng) + p.items[k] = NewItem(rng) } - p.Cuffs = Uncuffed + p.cuffs = Uncuffed } func (p *Player) DTO() serve.Player { r := serve.Player{ - HP: p.HP, + HP: p.hp, Items: make([]string, 0, 8), - Cuffs: p.Cuffs != Uncuffed, + Cuffs: p.cuffs != Uncuffed, } - for _, i := range p.Items { + for _, i := range p.items { s := i.String() if s == "" { continue