2020-05-17 14:29:30 -07:00
|
|
|
package rules
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
func getSoloRuleset(settings Settings) Ruleset {
|
|
|
|
|
return NewRulesetBuilder().WithSettings(settings).NamedRuleset(GameTypeSolo)
|
2020-05-17 14:38:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 20:00:19 -07:00
|
|
|
func TestSoloName(t *testing.T) {
|
2022-10-28 16:49:49 -07:00
|
|
|
r := getSoloRuleset(Settings{})
|
2021-07-02 20:00:19 -07:00
|
|
|
require.Equal(t, "solo", r.Name())
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 14:29:30 -07:00
|
|
|
func TestSoloCreateNextBoardStateSanity(t *testing.T) {
|
|
|
|
|
boardState := &BoardState{}
|
2022-10-28 16:49:49 -07:00
|
|
|
r := getSoloRuleset(Settings{})
|
|
|
|
|
gameOver, _, err := r.Execute(boardState, []SnakeMove{})
|
2020-05-17 14:29:30 -07:00
|
|
|
require.NoError(t, err)
|
2022-10-28 16:49:49 -07:00
|
|
|
require.True(t, gameOver)
|
2020-05-17 14:29:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSoloIsGameOver(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
Snakes []Snake
|
|
|
|
|
Expected bool
|
|
|
|
|
}{
|
|
|
|
|
{[]Snake{}, true},
|
|
|
|
|
{[]Snake{{}}, false},
|
|
|
|
|
{[]Snake{{}, {}, {}}, false},
|
|
|
|
|
{[]Snake{{EliminatedCause: EliminatedByOutOfBounds}}, true},
|
|
|
|
|
{
|
|
|
|
|
[]Snake{
|
|
|
|
|
{EliminatedCause: EliminatedByOutOfBounds},
|
|
|
|
|
{EliminatedCause: EliminatedByOutOfBounds},
|
|
|
|
|
{EliminatedCause: EliminatedByOutOfBounds},
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
r := getSoloRuleset(Settings{})
|
2020-05-17 14:29:30 -07:00
|
|
|
for _, test := range tests {
|
|
|
|
|
b := &BoardState{
|
|
|
|
|
Height: 11,
|
|
|
|
|
Width: 11,
|
|
|
|
|
Snakes: test.Snakes,
|
|
|
|
|
Food: []Point{},
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
actual, _, err := r.Execute(b, nil)
|
2020-05-17 14:29:30 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, test.Expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-15 16:41:39 -07:00
|
|
|
|
|
|
|
|
// Checks that a single snake doesn't end the game
|
|
|
|
|
// also that:
|
|
|
|
|
// - snake moves okay
|
|
|
|
|
// - food gets consumed
|
|
|
|
|
// - snake grows and gets health from food
|
|
|
|
|
var soloCaseNotOver = gameTestCase{
|
|
|
|
|
"Solo Case Game Not Over",
|
|
|
|
|
&BoardState{
|
|
|
|
|
Width: 10,
|
|
|
|
|
Height: 10,
|
|
|
|
|
Snakes: []Snake{
|
|
|
|
|
{
|
|
|
|
|
ID: "one",
|
2022-10-28 16:49:49 -07:00
|
|
|
Body: []Point{{X: 1, Y: 1}, {X: 1, Y: 2}},
|
2022-03-15 16:41:39 -07:00
|
|
|
Health: 100,
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-10-28 16:49:49 -07:00
|
|
|
Food: []Point{{X: 0, Y: 0}, {X: 1, Y: 0}},
|
2022-03-15 16:41:39 -07:00
|
|
|
Hazards: []Point{},
|
|
|
|
|
},
|
|
|
|
|
[]SnakeMove{
|
|
|
|
|
{ID: "one", Move: MoveDown},
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
&BoardState{
|
|
|
|
|
Width: 10,
|
|
|
|
|
Height: 10,
|
|
|
|
|
Snakes: []Snake{
|
|
|
|
|
{
|
|
|
|
|
ID: "one",
|
2022-10-28 16:49:49 -07:00
|
|
|
Body: []Point{{X: 1, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 1}},
|
2022-03-15 16:41:39 -07:00
|
|
|
Health: 100,
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-10-28 16:49:49 -07:00
|
|
|
Food: []Point{{X: 0, Y: 0}},
|
2022-03-15 16:41:39 -07:00
|
|
|
Hazards: []Point{},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSoloCreateNextBoardState(t *testing.T) {
|
|
|
|
|
cases := []gameTestCase{
|
|
|
|
|
// inherits these test cases from standard
|
|
|
|
|
standardCaseErrNoMoveFound,
|
|
|
|
|
standardCaseErrZeroLengthSnake,
|
|
|
|
|
standardCaseMoveEatAndGrow,
|
|
|
|
|
standardMoveAndCollideMAD,
|
|
|
|
|
soloCaseNotOver,
|
|
|
|
|
}
|
2022-10-28 16:49:49 -07:00
|
|
|
r := getSoloRuleset(Settings{})
|
2022-03-15 16:41:39 -07:00
|
|
|
for _, gc := range cases {
|
2022-10-28 16:49:49 -07:00
|
|
|
// test a RulesBuilder constructed instance
|
|
|
|
|
gc.requireValidNextState(t, r)
|
2022-04-19 15:52:57 -07:00
|
|
|
// also test a pipeline with the same settings
|
|
|
|
|
gc.requireValidNextState(t, NewRulesetBuilder().PipelineRuleset(GameTypeSolo, NewPipeline(soloRulesetStages...)))
|
2022-03-15 16:41:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-21 14:26:56 -07:00
|
|
|
|
|
|
|
|
// Test a snake running right into the wall is properly eliminated
|
|
|
|
|
func TestSoloEliminationOutOfBounds(t *testing.T) {
|
2022-10-28 16:49:49 -07:00
|
|
|
r := getSoloRuleset(Settings{})
|
2022-07-21 14:26:56 -07:00
|
|
|
|
|
|
|
|
// Using MaxRand is important because it ensures that the snakes are consistently placed in a way this test will work.
|
|
|
|
|
// Actually random placement could result in the assumptions made by this test being incorrect.
|
|
|
|
|
initialState, err := CreateDefaultBoardState(MaxRand, 2, 2, []string{"one"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
_, next, err := r.Execute(initialState, []SnakeMove{{ID: "one", Move: "right"}})
|
2022-07-21 14:26:56 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, initialState)
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
ended, next, err := r.Execute(next, []SnakeMove{{ID: "one", Move: "right"}})
|
2022-07-21 14:26:56 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, initialState)
|
|
|
|
|
|
|
|
|
|
require.True(t, ended)
|
|
|
|
|
require.Equal(t, EliminatedByOutOfBounds, next.Snakes[0].EliminatedCause)
|
|
|
|
|
require.Equal(t, "", next.Snakes[0].EliminatedBy)
|
|
|
|
|
require.Equal(t, 1, next.Snakes[0].EliminatedOnTurn)
|
|
|
|
|
}
|