Add error cases to standard move resolution.
This commit is contained in:
parent
73085679f6
commit
9c13dea6c0
2 changed files with 81 additions and 0 deletions
10
standard.go
10
standard.go
|
|
@ -181,6 +181,13 @@ func (r *StandardRuleset) ResolveMoves(prevState *BoardState, moves []SnakeMove)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *StandardRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
|
func (r *StandardRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
|
||||||
|
if len(moves) < len(b.Snakes) {
|
||||||
|
return errors.New("not enough snake moves")
|
||||||
|
}
|
||||||
|
if len(moves) > len(b.Snakes) {
|
||||||
|
return errors.New("too many snake moves")
|
||||||
|
}
|
||||||
|
|
||||||
for _, move := range moves {
|
for _, move := range moves {
|
||||||
var snake *Snake
|
var snake *Snake
|
||||||
for i := 0; i < len(b.Snakes); i++ {
|
for i := 0; i < len(b.Snakes); i++ {
|
||||||
|
|
@ -188,6 +195,9 @@ func (r *StandardRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
|
||||||
snake = &b.Snakes[i]
|
snake = &b.Snakes[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if snake == nil {
|
||||||
|
return errors.New("snake not found for move")
|
||||||
|
}
|
||||||
|
|
||||||
// Do not move eliminated snakes
|
// Do not move eliminated snakes
|
||||||
if snake.EliminatedCause != NotEliminated {
|
if snake.EliminatedCause != NotEliminated {
|
||||||
|
|
|
||||||
|
|
@ -340,6 +340,77 @@ func TestMoveSnakes(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMoveSnakesWrongID(t *testing.T) {
|
||||||
|
b := &BoardState{
|
||||||
|
Snakes: []Snake{
|
||||||
|
{
|
||||||
|
ID: "one",
|
||||||
|
Body: []Point{{1, 1}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
moves := []SnakeMove{
|
||||||
|
{
|
||||||
|
ID: "not found",
|
||||||
|
Move: MoveUp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := StandardRuleset{}
|
||||||
|
err := r.moveSnakes(b, moves)
|
||||||
|
require.Equal(t, err, errors.New("snake not found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveSnakesNotEnoughMoves(t *testing.T) {
|
||||||
|
b := &BoardState{
|
||||||
|
Snakes: []Snake{
|
||||||
|
{
|
||||||
|
ID: "one",
|
||||||
|
Body: []Point{{1, 1}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "two",
|
||||||
|
Body: []Point{{2, 2}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
moves := []SnakeMove{
|
||||||
|
{
|
||||||
|
ID: "two",
|
||||||
|
Move: MoveUp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := StandardRuleset{}
|
||||||
|
err := r.moveSnakes(b, moves)
|
||||||
|
require.Equal(t, err, errors.New("not enough snake moves"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoveSnakesTooManyMoves(t *testing.T) {
|
||||||
|
b := &BoardState{
|
||||||
|
Snakes: []Snake{
|
||||||
|
{
|
||||||
|
ID: "one",
|
||||||
|
Body: []Point{{1, 1}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
moves := []SnakeMove{
|
||||||
|
{
|
||||||
|
ID: "one",
|
||||||
|
Move: MoveUp,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "two",
|
||||||
|
Move: MoveUp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
r := StandardRuleset{}
|
||||||
|
err := r.moveSnakes(b, moves)
|
||||||
|
require.Equal(t, err, errors.New("too many snake moves"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsKnownBoardSize(t *testing.T) {
|
func TestIsKnownBoardSize(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Width int32
|
Width int32
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue