DEV-1479 ensure snake elimination turn is set (#93)

* ensure snake elimination turn is set

- centralise elimination update logic to a single place to ensure consistency

* doc the method

* testing
This commit is contained in:
Torben 2022-07-21 14:26:56 -07:00 committed by GitHub
parent 663c377cc4
commit e1289af5fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 143 additions and 73 deletions

View file

@ -57,6 +57,7 @@ func (prevState *BoardState) Clone() *BoardState {
nextState.Snakes[i].Health = prevState.Snakes[i].Health
nextState.Snakes[i].Body = append([]Point{}, prevState.Snakes[i].Body...)
nextState.Snakes[i].EliminatedCause = prevState.Snakes[i].EliminatedCause
nextState.Snakes[i].EliminatedOnTurn = prevState.Snakes[i].EliminatedOnTurn
nextState.Snakes[i].EliminatedBy = prevState.Snakes[i].EliminatedBy
}
return nextState
@ -551,3 +552,13 @@ func getDistanceBetweenPoints(a, b Point) int {
func isSquareBoard(b *BoardState) bool {
return b.Width == b.Height
}
// EliminateSnake updates a snake's state to reflect that it was eliminated.
// - "cause" identifies what type of event caused the snake to be eliminated
// - "by" identifies which snake (if any, use empty string "" if none) eliminated the snake.
// - "turn" is the turn number that this snake was eliminated on.
func EliminateSnake(s *Snake, cause, by string, turn int) {
s.EliminatedCause = cause
s.EliminatedBy = by
s.EliminatedOnTurn = turn
}