Merge branch 'master' of github.com:BattlesnakeOfficial/rules
This commit is contained in:
commit
60c0b149ba
3 changed files with 84 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
package rulesets
|
package rules
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MoveUp = "up"
|
MoveUp = "up"
|
||||||
|
|
|
||||||
12
standard.go
12
standard.go
|
|
@ -1,4 +1,4 @@
|
||||||
package rulesets
|
package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -183,6 +183,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++ {
|
||||||
|
|
@ -190,6 +197,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 {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package rulesets
|
package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -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 for move"))
|
||||||
|
}
|
||||||
|
|
||||||
|
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