DEV-1761: New rules API (#118)

* DEV-1761: Clean up Ruleset interface (#115)

* remove legacy ruleset types and simplify ruleset interface

* remove unnecessary settings argument from Ruleset interface

* decouple rules.Settings from client API and store settings as strings

* DEV 1761: Add new BoardState and Point fields (#117)

* add Point.TTL, Point.Value, GameState and PointState to BoardState

* allow maps to access BoardState.GameState,PointState

* add PreUpdateBoard and refactor snail_mode with it

* fix bug where an extra turn was printed to the console

* fix formatting

* fix lint errors

Co-authored-by: JonathanArns <jonathan.arns@googlemail.com>
This commit is contained in:
Rob O'Dwyer 2022-10-28 16:49:49 -07:00 committed by GitHub
parent 639362ef46
commit 82e1999126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 1349 additions and 1610 deletions

View file

@ -7,15 +7,19 @@ import (
"github.com/stretchr/testify/require"
)
func getWrappedRuleset(settings Settings) Ruleset {
return NewRulesetBuilder().WithSettings(settings).NamedRuleset(GameTypeWrapped)
}
func TestLeft(t *testing.T) {
boardState := &BoardState{
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 10}}},
},
}
@ -26,17 +30,18 @@ func TestLeft(t *testing.T) {
{ID: "topRight", Move: "left"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{10, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{9, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{10, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{9, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 10, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 9, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 10, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 9, Y: 10}}},
}
for i, snake := range nextBoardState.Snakes {
require.Equal(t, expectedSnakes[i].ID, snake.ID, snake.ID)
@ -51,10 +56,10 @@ func TestRight(t *testing.T) {
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 10}}},
},
}
@ -65,17 +70,18 @@ func TestRight(t *testing.T) {
{ID: "topRight", Move: "right"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{1, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{0, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{1, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{0, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 1, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 1, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 0, Y: 10}}},
}
for i, snake := range nextBoardState.Snakes {
require.Equal(t, expectedSnakes[i].ID, snake.ID, snake.ID)
@ -90,10 +96,10 @@ func TestUp(t *testing.T) {
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 10}}},
},
}
@ -104,17 +110,18 @@ func TestUp(t *testing.T) {
{ID: "topRight", Move: "up"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 1}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 1}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 0}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 0}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 1}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 1}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 0}}},
}
for i, snake := range nextBoardState.Snakes {
require.Equal(t, expectedSnakes[i].ID, snake.ID, snake.ID)
@ -129,10 +136,10 @@ func TestDown(t *testing.T) {
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 10}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 10}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 0}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 0}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 10}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 10}}},
},
}
@ -143,17 +150,18 @@ func TestDown(t *testing.T) {
{ID: "topRight", Move: "down"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "bottomLeft", Health: 10, Body: []Point{{0, 10}}},
{ID: "bottomRight", Health: 10, Body: []Point{{10, 10}}},
{ID: "topLeft", Health: 10, Body: []Point{{0, 9}}},
{ID: "topRight", Health: 10, Body: []Point{{10, 9}}},
{ID: "bottomLeft", Health: 10, Body: []Point{{X: 0, Y: 10}}},
{ID: "bottomRight", Health: 10, Body: []Point{{X: 10, Y: 10}}},
{ID: "topLeft", Health: 10, Body: []Point{{X: 0, Y: 9}}},
{ID: "topRight", Health: 10, Body: []Point{{X: 10, Y: 9}}},
}
for i, snake := range nextBoardState.Snakes {
require.Equal(t, expectedSnakes[i].ID, snake.ID, snake.ID)
@ -168,14 +176,14 @@ func TestEdgeCrossingCollision(t *testing.T) {
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "left", Health: 10, Body: []Point{{0, 5}}},
{ID: "left", Health: 10, Body: []Point{{X: 0, Y: 5}}},
{ID: "rightEdge", Health: 10, Body: []Point{
{10, 1},
{10, 2},
{10, 3},
{10, 4},
{10, 5},
{10, 6},
{X: 10, Y: 1},
{X: 10, Y: 2},
{X: 10, Y: 3},
{X: 10, Y: 4},
{X: 10, Y: 5},
{X: 10, Y: 6},
}},
},
}
@ -185,21 +193,22 @@ func TestEdgeCrossingCollision(t *testing.T) {
{ID: "rightEdge", Move: "down"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "left", Health: 0, Body: []Point{{10, 5}}, EliminatedCause: EliminatedByCollision, EliminatedBy: "rightEdge"},
{ID: "left", Health: 0, Body: []Point{{X: 10, Y: 5}}, EliminatedCause: EliminatedByCollision, EliminatedBy: "rightEdge"},
{ID: "rightEdge", Health: 10, Body: []Point{
{10, 0},
{10, 1},
{10, 2},
{10, 3},
{10, 4},
{10, 5},
{X: 10, Y: 0},
{X: 10, Y: 1},
{X: 10, Y: 2},
{X: 10, Y: 3},
{X: 10, Y: 4},
{X: 10, Y: 5},
}},
}
for i, snake := range nextBoardState.Snakes {
@ -215,11 +224,11 @@ func TestEdgeCrossingEating(t *testing.T) {
Width: 11,
Height: 11,
Snakes: []Snake{
{ID: "left", Health: 10, Body: []Point{{0, 5}, {1, 5}}},
{ID: "other", Health: 10, Body: []Point{{5, 5}}},
{ID: "left", Health: 10, Body: []Point{{X: 0, Y: 5}, {X: 1, Y: 5}}},
{ID: "other", Health: 10, Body: []Point{{X: 5, Y: 5}}},
},
Food: []Point{
{10, 5},
{X: 10, Y: 5},
},
}
@ -228,15 +237,16 @@ func TestEdgeCrossingEating(t *testing.T) {
{ID: "other", Move: "left"},
}
r := WrappedRuleset{}
r := getWrappedRuleset(Settings{})
nextBoardState, err := r.CreateNextBoardState(boardState, snakeMoves)
gameOver, nextBoardState, err := r.Execute(boardState, snakeMoves)
require.NoError(t, err)
require.False(t, gameOver)
require.Equal(t, len(boardState.Snakes), len(nextBoardState.Snakes))
expectedSnakes := []Snake{
{ID: "left", Health: 100, Body: []Point{{10, 5}, {0, 5}, {0, 5}}},
{ID: "other", Health: 9, Body: []Point{{4, 5}}},
{ID: "left", Health: 100, Body: []Point{{X: 10, Y: 5}, {X: 0, Y: 5}, {X: 0, Y: 5}}},
{ID: "other", Health: 9, Body: []Point{{X: 4, Y: 5}}},
}
for i, snake := range nextBoardState.Snakes {
require.Equal(t, expectedSnakes[i].ID, snake.ID, snake.ID)
@ -271,12 +281,12 @@ var wrappedCaseMoveAndWrap = gameTestCase{
Snakes: []Snake{
{
ID: "one",
Body: []Point{{0, 0}, {1, 0}},
Body: []Point{{X: 0, Y: 0}, {X: 1, Y: 0}},
Health: 100,
},
{
ID: "two",
Body: []Point{{3, 4}, {3, 3}},
Body: []Point{{X: 3, Y: 4}, {X: 3, Y: 3}},
Health: 100,
},
{
@ -301,12 +311,12 @@ var wrappedCaseMoveAndWrap = gameTestCase{
Snakes: []Snake{
{
ID: "one",
Body: []Point{{9, 0}, {0, 0}},
Body: []Point{{X: 9, Y: 0}, {X: 0, Y: 0}},
Health: 99,
},
{
ID: "two",
Body: []Point{{3, 5}, {3, 4}},
Body: []Point{{X: 3, Y: 5}, {X: 3, Y: 4}},
Health: 99,
},
{
@ -330,14 +340,10 @@ func TestWrappedCreateNextBoardState(t *testing.T) {
standardMoveAndCollideMAD,
wrappedCaseMoveAndWrap,
}
r := WrappedRuleset{}
rb := NewRulesetBuilder().WithParams(map[string]string{
ParamGameType: GameTypeWrapped,
})
r := getWrappedRuleset(Settings{})
for _, gc := range cases {
gc.requireValidNextState(t, &r)
// also test a RulesBuilder constructed instance
gc.requireValidNextState(t, rb.Ruleset())
// test a RulesBuilder constructed instance
gc.requireValidNextState(t, r)
// also test a pipeline with the same settings
gc.requireValidNextState(t, NewRulesetBuilder().PipelineRuleset(GameTypeWrapped, NewPipeline(wrappedRulesetStages...)))
}