Add solo gameplay ruleset.

This commit is contained in:
Brad Van Vugt 2020-05-17 14:29:30 -07:00
parent 71fc6bf503
commit fe2a415cac
2 changed files with 62 additions and 0 deletions

14
solo.go Normal file
View file

@ -0,0 +1,14 @@
package rules
type SoloRuleset struct {
StandardRuleset
}
func (r *SoloRuleset) IsGameOver(b *BoardState) (bool, error) {
for i := 0; i < len(b.Snakes); i++ {
if b.Snakes[i].EliminatedCause == NotEliminated {
return false, nil
}
}
return true, nil
}

48
solo_test.go Normal file
View file

@ -0,0 +1,48 @@
package rules
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSoloCreateNextBoardStateSanity(t *testing.T) {
boardState := &BoardState{}
r := SoloRuleset{}
_, err := r.CreateNextBoardState(boardState, []SnakeMove{})
require.NoError(t, err)
}
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,
},
}
r := SoloRuleset{}
for _, test := range tests {
b := &BoardState{
Height: 11,
Width: 11,
Snakes: test.Snakes,
Food: []Point{},
}
actual, err := r.IsGameOver(b)
require.NoError(t, err)
require.Equal(t, test.Expected, actual)
}
}