// Package game implements a controller for Buckshot Roulette matches. // // The terminology of this package is as follows: // - A match is a pairing of two players, the dealer and challenger. // It ends when the challenger wins three rounds or the dealer wins once. // - A round is an instance of gameplay. Both players start each round with // the same HP, and it continues until either player reaches zero. // - A game is a set of 2-8 shells and 1-4 items delivered to each player. // The challenger always moves first at the start of each game. package game import ( "errors" "git.sunturtle.xyz/studio/shotgun/player" "git.sunturtle.xyz/studio/shotgun/serve" ) type Match struct { // rng is the PRNG state used for this match. rng RNG // players are the players in the match. The first element is the dealer // and the second is the challenger. players [2]Player // shells is the list of remaining shells in the current game. It always // uses shellArray as its backing array. shells []bool // round is the round number. Once it reaches 3, the match is over. round uint // game is the game number. May be nice for metrics eventually. game uint // turn is the turn number. When even, it is the dealer's turn. turn uint // hp is the starting and max HP of both players in the current round. hp int8 // damage is the amount of damage a live shell will deal this turn. damage int8 // reveal indicates whether the current player has revealed the shell that // will fire next. reveal bool // prev is a pointer to the element of shellArray which was fired last this // game, or nil if none has been. prev *bool // shellArray is the backing storage for shells and prev. shellArray [8]bool } // New creates a new match started at round 1. func New(dealer, challenger player.ID) *Match { g := &Match{ rng: NewRNG(), players: [2]Player{ {id: dealer}, {id: challenger}, }, } g.NextRound() return g } // NextRound starts the next round of a match. func (g *Match) NextRound() { g.hp = int8(g.rng.Intn(3) + 2) g.players[0].StartRound(g.hp) g.players[1].StartRound(g.hp) g.round++ g.game = 0 g.NextGame() } // NextGame starts the next game of a round. func (g *Match) NextGame() { 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 } for i := shells / 2; i < shells; i++ { g.shellArray[i] = false } g.shells = g.shellArray[:shells] ShuffleSlice(&g.rng, g.shells) g.game++ g.turn = 0 g.prev = nil g.NextTurn() } // NextTurn advances the turn but not the match or round state. func (g *Match) NextTurn() { g.turn++ g.damage = 1 g.reveal = false cur := g.CurrentPlayer() skip := cur.cuffs == CuffedSkip cur.cuffs = cur.cuffs.NextState() if skip { g.NextTurn() } } // CurrentPlayer gets the current player. func (g *Match) CurrentPlayer() *Player { return &g.players[g.turn%2] } // Opponent returns the player who is not the current player. func (g *Match) Opponent() *Player { return &g.players[g.turn%2^1] } // Apply uses an item by index for the current player. // This may cause the current game to end, but does not start the next game // if so. // Returns ErrWrongTurn if id does not correspond to the current player. func (g *Match) Apply(id player.ID, item int) error { cur := g.CurrentPlayer() if cur.id != id { return ErrWrongTurn } 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 } return nil } // popShell removes a shell from the shotgun. // This may cause the shotgun to be empty, but does not start the next game // if so. func (g *Match) popShell() bool { 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 *Match) Peek(id player.ID) *bool { if len(g.shells) == 0 || id != g.CurrentPlayer().id || !g.reveal { return nil } return &g.shells[0] } // Empty returns whether the shotgun is empty. func (g *Match) Empty() bool { return len(g.shells) == 0 } // Winner returns the player who won the current round, or nil if the round is // not over. func (g *Match) Winner() *Player { if g.players[0].hp <= 0 { return &g.players[1] } if g.players[1].hp <= 0 { return &g.players[0] } return nil } // Shoot fires the shotgun, at the opponent if self is false and at the current // player if self is true. Afterward, the round or game may be over. // Returns ErrWrongTurn if id does not correspond to the current player. func (g *Match) Shoot(id player.ID, self bool) error { cur := g.CurrentPlayer() if cur.id != id { return ErrWrongTurn } target := g.Opponent() if self { target = g.CurrentPlayer() } live := g.popShell() if live { target.hp -= g.damage g.NextTurn() } else if !self { g.NextTurn() } return nil } // Concede sets the player with the given ID to zero health and ends the match. // The returned bool indicates whether the match has ended. (It will be false // if id does not correspond to either player.) func (g *Match) Concede(id player.ID) bool { switch id { case g.players[0].id: g.players[0].hp = 0 case g.players[1].id: g.players[1].hp = 0 default: return false } g.round = 3 return true } // DTO returns the current match state as viewed by the given player. func (g *Match) DTO(id player.ID) serve.Game { return serve.Game{ Players: [2]serve.Player{ g.players[0].DTO(), g.players[1].DTO(), }, Round: g.round, Damage: g.damage, Shell: g.Peek(id), Previous: g.prev, } } // ShellCounts returns the number of live and blank shells. func (g *Match) ShellCounts() serve.ShellCounts { var counts serve.ShellCounts for _, s := range g.shells { if s { counts.Live++ } else { counts.Blank++ } } return counts } var ErrWrongTurn = errors.New("not your turn")