From 93aa4b49d0966fa3c1a937099755214c757efc3f Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sun, 21 Jan 2024 00:02:22 -0600 Subject: [PATCH] require proof of turn to use items or shoot --- game/game.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/game/game.go b/game/game.go index 43483ac..8a934ff 100644 --- a/game/game.go +++ b/game/game.go @@ -1,5 +1,11 @@ package game +import ( + "errors" + + "git.sunturtle.xyz/studio/shotgun/player" +) + type Game struct { RNG RNG PP [2]Player @@ -57,14 +63,20 @@ func (g *Game) Opponent() *Player { } // Apply uses an item by index for the current player. -func (g *Game) Apply(item int) { +// This may cause the group to end. +// 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 { + return ErrWrongTurn + } if item < 0 || item >= len(cur.Items) { - return + return errors.New("item index out of bounds") } if cur.Items[item].Apply(g) { cur.Items[item] = ItemNone } + return nil } // PopShell removes a shell from the shotgun. @@ -95,7 +107,12 @@ func (g *Game) Winner() *Player { // Shoot fires the shotgun, at the opponent if self is false and at the current // player if self is true. Afterward, the round may be over, or the shotgun may // be empty. -func (g *Game) Shoot(self bool) { +// 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 { + return ErrWrongTurn + } target := g.Opponent() if self { target = g.CurrentPlayer() @@ -107,4 +124,7 @@ func (g *Game) Shoot(self bool) { } else if !self { g.Turn++ } + return nil } + +var ErrWrongTurn = errors.New("not your turn")