implement conceding

This commit is contained in:
Branden J Brown 2024-01-23 20:45:00 -06:00
parent 2b9e617c10
commit e8786073aa
2 changed files with 51 additions and 0 deletions

View File

@ -163,6 +163,21 @@ func (g *Game) Shoot(id player.ID, self bool) error {
return nil return nil
} }
// Concede sets the player with the given ID to zero health. The returned
// Boolean indicates whether the game has ended. (It will be false if id does
// not correspond to either player.)
func (g *Game) 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
}
return true
}
// DTO returns the current game state as viewed by the given player. // DTO returns the current game state as viewed by the given player.
func (g *Game) DTO(id player.ID) serve.Game { func (g *Game) DTO(id player.ID) serve.Game {
return serve.Game{ return serve.Game{

View File

@ -405,6 +405,42 @@ func TestGameShoot(t *testing.T) {
} }
} }
func TestConcede(t *testing.T) {
dealer, chall := player.ID{1}, player.ID{2}
cases := []struct {
name string
p player.ID
winner player.ID
}{
{
name: "dealer",
p: dealer,
winner: chall,
},
{
name: "challenger",
p: chall,
winner: dealer,
},
{
name: "neither",
p: player.ID{3},
winner: player.ID{},
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
g := New(dealer, chall)
g.Concede(c.p)
if got := g.Winner(); deref(got).id != c.winner {
t.Errorf("wrong winner: %#v doesn't have id %#v", got, c.winner)
}
})
}
}
func TestGameShellCounts(t *testing.T) { func TestGameShellCounts(t *testing.T) {
cases := []struct { cases := []struct {
name string name string