From 93716a7f0f7cd5ff98838ac6bff92918a2d9f8f8 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sun, 7 Apr 2024 20:40:01 -0500 Subject: [PATCH] implement model for adrenaline --- game/game.go | 25 +++++++++++++++++-------- game/item.go | 6 +++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/game/game.go b/game/game.go index 3e3eb13..488ed73 100644 --- a/game/game.go +++ b/game/game.go @@ -36,6 +36,9 @@ type Match struct { // reveal indicates whether the current player has revealed the shell that // will fire next. reveal bool + // adrenaline indicates whether the current player has adrenaline in + // effect, requiring them to use an opponent's item. + adrenaline bool // prev is a pointer to the element of shellArray which was fired last this // game, or nil if none has been. prev *bool @@ -117,17 +120,22 @@ func (g *Match) Opponent() *playerState { // Apply uses an item by index for the current player. // If this causes the current game to end (a beer discharges the last shell), // returns ErrGameEnded but does not start the next game. -// Returns ErrWrongTurn if id does not correspond to the current player. +// Returns ErrWrongTurn if id does not correspond to the current player +// (with appropriate exception for adrenaline). func (g *Match) Apply(id player.ID, item int) error { - cur := g.CurrentPlayer() - if cur.id != id { + p := g.CurrentPlayer() + if g.adrenaline { + p = g.Opponent() + } + if p.id != id { return ErrWrongTurn } - if item < 0 || item >= len(cur.items) { + if item < 0 || item >= len(p.items) { return errors.New("item index out of bounds") } - if cur.items[item].apply(g) { - cur.items[item] = itemNone + if p.items[item].apply(g) { + p.items[item] = itemNone + g.adrenaline = false } if g.Empty() { return ErrGameEnded @@ -183,10 +191,11 @@ func (g *Match) MatchWinner() *playerState { // player if self is true. Advances the turn if appropriate. // Returns ErrRoundEnded if this causes the round to end. // Returns ErrGameEnded if this causes the game but not the round to end. -// Returns ErrWrongTurn if id does not correspond to the current player. +// Returns ErrWrongTurn if id does not correspond to the current player +// or if adrenaline is in effect. func (g *Match) Shoot(id player.ID, self bool) error { cur := g.CurrentPlayer() - if cur.id != id { + if cur.id != id || g.adrenaline { return ErrWrongTurn } target := g.Opponent() diff --git a/game/item.go b/game/item.go index c76722a..c9d5a8d 100644 --- a/game/item.go +++ b/game/item.go @@ -62,8 +62,12 @@ func (i item) apply(g *Match) bool { g.damage = 2 return true case itemAdrenaline: + if g.adrenaline { + // Adrenaline forbids using adrenaline. + return false + } g.action = actAdrenaline - // TODO(zeph): implement + g.adrenaline = true return true case itemPhone: g.action = actPhone