Skip to content

Commit

Permalink
Rename "EliminatedByStarvation" > "EliminatedByOutOfHealth"
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanvugt committed Sep 10, 2020
1 parent 92592c2 commit f5aec61
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
6 changes: 4 additions & 2 deletions royale.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ func (r *RoyaleRuleset) damageOutOfBounds(b *BoardState) error {
if head == p {
// Snake is now out of bounds, reduce health
snake.Health = snake.Health - r.DamagePerTurn
if snake.Health <= 0 {
if snake.Health < 0 {
snake.Health = 0
snake.EliminatedCause = EliminatedByStarvation
}
if r.StandardRuleset.snakeIsOutOfHealth(snake) {
snake.EliminatedCause = EliminatedByOutOfHealth
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions royale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestRoyaleDamageOutOfBounds(t *testing.T) {
{
Snakes: []Snake{{Body: []Point{{0, 0}}}},
OutOfBounds: []Point{{0, 0}},
ExpectedEliminatedCauses: []string{EliminatedByStarvation},
ExpectedEliminatedCauses: []string{EliminatedByOutOfHealth},
ExpectedEliminatedByIDs: []string{""},
},
{
Expand All @@ -147,7 +147,7 @@ func TestRoyaleDamageOutOfBounds(t *testing.T) {
{Body: []Point{{3, 3}, {3, 4}, {3, 5}, {3, 6}}},
},
OutOfBounds: []Point{{3, 3}},
ExpectedEliminatedCauses: []string{NotEliminated, EliminatedByStarvation},
ExpectedEliminatedCauses: []string{NotEliminated, EliminatedByOutOfHealth},
ExpectedEliminatedByIDs: []string{"", ""},
},
}
Expand Down Expand Up @@ -177,14 +177,14 @@ func TestRoyaleDamagePerTurn(t *testing.T) {
{100, -100, 100, NotEliminated, errors.New("royale damage per turn must be greater than zero")},
{100, 1, 99, NotEliminated, nil},
{100, 99, 1, NotEliminated, nil},
{100, 100, 0, EliminatedByStarvation, nil},
{100, 101, 0, EliminatedByStarvation, nil},
{100, 999, 0, EliminatedByStarvation, nil},
{100, 100, 0, EliminatedByOutOfHealth, nil},
{100, 101, 0, EliminatedByOutOfHealth, nil},
{100, 999, 0, EliminatedByOutOfHealth, nil},
{2, 1, 1, NotEliminated, nil},
{1, 1, 0, EliminatedByStarvation, nil},
{1, 999, 0, EliminatedByStarvation, nil},
{0, 1, 0, EliminatedByStarvation, nil},
{0, 999, 0, EliminatedByStarvation, nil},
{1, 1, 0, EliminatedByOutOfHealth, nil},
{1, 999, 0, EliminatedByOutOfHealth, nil},
{0, 1, 0, EliminatedByOutOfHealth, nil},
{0, 999, 0, EliminatedByOutOfHealth, nil},
}

for _, test := range tests {
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestRoyalDamageNextTurn(t *testing.T) {
b.Snakes[0].Health = 15
n, err = r.CreateNextBoardState(b, m)
require.NoError(t, err)
require.Equal(t, EliminatedByStarvation, n.Snakes[0].EliminatedCause)
require.Equal(t, EliminatedByOutOfHealth, n.Snakes[0].EliminatedCause)
require.Equal(t, int32(0), n.Snakes[0].Health)
require.Equal(t, Point{9, 0}, n.Snakes[0].Body[0])
require.Equal(t, 20, len(r.OutOfBounds))
Expand Down
4 changes: 2 additions & 2 deletions squad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSquadAllowBodyCollisions(t *testing.T) {
{"R4", "red", EliminatedByCollision, "R4", EliminatedByCollision, "R4"}, // this is an error case but worth testing
{"R5", "red", EliminatedByCollision, "R4", NotEliminated, ""},
// Green Squad
{"G1", "green", EliminatedByStarvation, "x", EliminatedByStarvation, "x"},
{"G1", "green", EliminatedByOutOfHealth, "x", EliminatedByOutOfHealth, "x"},
// Yellow Squad
{"Y1", "yellow", EliminatedByCollision, "B4", EliminatedByCollision, "B4"},
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestSquadSharedElimination(t *testing.T) {
{"R3", "red", NotEliminated, "", EliminatedBySquad, ""},
{"R4", "red", EliminatedByCollision, "B1", EliminatedByCollision, "B1"},
// Green Squad
{"G1", "green", EliminatedByStarvation, "x", EliminatedByStarvation, "x"},
{"G1", "green", EliminatedByOutOfHealth, "x", EliminatedByOutOfHealth, "x"},
// Yellow Squad
{"Y1", "yellow", NotEliminated, "", NotEliminated, ""},
}
Expand Down
8 changes: 4 additions & 4 deletions standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
NotEliminated = ""
EliminatedByCollision = "snake-collision"
EliminatedBySelfCollision = "snake-self-collision"
EliminatedByStarvation = "starvation"
EliminatedByOutOfHealth = "out-of-health"
EliminatedByHeadToHeadCollision = "head-collision"
EliminatedByOutOfBounds = "wall-collision"

Expand Down Expand Up @@ -348,8 +348,8 @@ func (r *StandardRuleset) maybeEliminateSnakes(b *BoardState) error {
return errors.New("snake is length zero")
}

if r.snakeHasStarved(snake) {
snake.EliminatedCause = EliminatedByStarvation
if r.snakeIsOutOfHealth(snake) {
snake.EliminatedCause = EliminatedByOutOfHealth
continue
}

Expand Down Expand Up @@ -444,7 +444,7 @@ func (r *StandardRuleset) maybeEliminateSnakes(b *BoardState) error {
return nil
}

func (r *StandardRuleset) snakeHasStarved(s *Snake) bool {
func (r *StandardRuleset) snakeIsOutOfHealth(s *Snake) bool {
return s.Health <= 0
}

Expand Down
14 changes: 7 additions & 7 deletions standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func TestEatingOnLastMove(t *testing.T) {
ID: "two",
Body: []Point{{3, 1}, {3, 2}, {3, 3}},
Health: 0,
EliminatedCause: EliminatedByStarvation,
EliminatedCause: EliminatedByOutOfHealth,
},
},
Food: []Point{{9, 9}},
Expand Down Expand Up @@ -783,7 +783,7 @@ func TestRegressionIssue19(t *testing.T) {
ID: "eliminated",
Body: []Point{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}},
Health: 0,
EliminatedCause: EliminatedByStarvation,
EliminatedCause: EliminatedByOutOfHealth,
},
},
Food: []Point{{9, 9}},
Expand Down Expand Up @@ -811,7 +811,7 @@ func TestRegressionIssue19(t *testing.T) {
ID: "eliminated",
Body: []Point{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}},
Health: 0,
EliminatedCause: EliminatedByStarvation,
EliminatedCause: EliminatedByOutOfHealth,
},
},
Food: []Point{{9, 9}},
Expand Down Expand Up @@ -1121,7 +1121,7 @@ func TestReduceSnakeHealth(t *testing.T) {
require.Equal(t, b.Snakes[2].Health, int32(50))
}

func TestSnakeHasStarved(t *testing.T) {
func TestSnakeIsOutOfHealth(t *testing.T) {
tests := []struct {
Health int32
Expected bool
Expand All @@ -1140,7 +1140,7 @@ func TestSnakeHasStarved(t *testing.T) {
r := StandardRuleset{}
for _, test := range tests {
s := &Snake{Health: test.Health}
require.Equal(t, test.Expected, r.snakeHasStarved(s), "Health: %+v", test.Health)
require.Equal(t, test.Expected, r.snakeIsOutOfHealth(s), "Health: %+v", test.Health)
}
}

Expand Down Expand Up @@ -1372,7 +1372,7 @@ func TestMaybeEliminateSnakes(t *testing.T) {
[]Snake{
Snake{ID: "1", Body: []Point{{1, 1}}},
},
[]string{EliminatedByStarvation},
[]string{EliminatedByOutOfHealth},
[]string{""},
nil,
},
Expand Down Expand Up @@ -1546,7 +1546,7 @@ func TestMaybeEliminateSnakesPriority(t *testing.T) {
{ID: "6", Health: 1, Body: []Point{{2, 2}, {2, 3}, {2, 4}, {2, 5}}},
},
[]string{
EliminatedByStarvation,
EliminatedByOutOfHealth,
EliminatedByOutOfBounds,
EliminatedBySelfCollision,
EliminatedByCollision,
Expand Down

0 comments on commit f5aec61

Please sign in to comment.