require proof of turn to use items or shoot

This commit is contained in:
Branden J Brown 2024-01-21 00:02:22 -06:00
parent 18ba9fd568
commit 93aa4b49d0
1 changed files with 23 additions and 3 deletions

View File

@ -1,5 +1,11 @@
package game package game
import (
"errors"
"git.sunturtle.xyz/studio/shotgun/player"
)
type Game struct { type Game struct {
RNG RNG RNG RNG
PP [2]Player PP [2]Player
@ -57,14 +63,20 @@ func (g *Game) Opponent() *Player {
} }
// Apply uses an item by index for the current 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() cur := g.CurrentPlayer()
if cur.ID != id {
return ErrWrongTurn
}
if item < 0 || item >= len(cur.Items) { if item < 0 || item >= len(cur.Items) {
return return errors.New("item index out of bounds")
} }
if cur.Items[item].Apply(g) { if cur.Items[item].Apply(g) {
cur.Items[item] = ItemNone cur.Items[item] = ItemNone
} }
return nil
} }
// PopShell removes a shell from the shotgun. // 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 // 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 // player if self is true. Afterward, the round may be over, or the shotgun may
// be empty. // 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() target := g.Opponent()
if self { if self {
target = g.CurrentPlayer() target = g.CurrentPlayer()
@ -107,4 +124,7 @@ func (g *Game) Shoot(self bool) {
} else if !self { } else if !self {
g.Turn++ g.Turn++
} }
return nil
} }
var ErrWrongTurn = errors.New("not your turn")