New "feast" game mode (alpha)
This commit is contained in:
parent
ca4b6c5dce
commit
d7ee7b97fb
2 changed files with 146 additions and 0 deletions
41
feast.go
Normal file
41
feast.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package rules
|
||||
|
||||
import ()
|
||||
|
||||
type FeastRuleset struct {
|
||||
StandardRuleset
|
||||
}
|
||||
|
||||
func (r *FeastRuleset) CreateInitialBoardState(width int32, height int32, snakeIDs []string) (*BoardState, error) {
|
||||
initialBoardState, err := r.StandardRuleset.CreateInitialBoardState(width, height, snakeIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.fillBoardWithFood(initialBoardState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return initialBoardState, nil
|
||||
}
|
||||
|
||||
func (r *FeastRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
nextState, err := r.StandardRuleset.CreateNextBoardState(prevState, moves)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.fillBoardWithFood(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nextState, nil
|
||||
}
|
||||
|
||||
func (r *FeastRuleset) fillBoardWithFood(b *BoardState) error {
|
||||
unoccupiedPoints := r.getUnoccupiedPoints(b, true)
|
||||
b.Food = append(b.Food, unoccupiedPoints...)
|
||||
return nil
|
||||
}
|
||||
105
feast_test.go
Normal file
105
feast_test.go
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
package rules
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFeastRulesetInterface(t *testing.T) {
|
||||
var _ Ruleset = (*FeastRuleset)(nil)
|
||||
}
|
||||
|
||||
func TestFeastCreateInitialBoardState(t *testing.T) {
|
||||
tests := []struct {
|
||||
Height int32
|
||||
Width int32
|
||||
IDs []string
|
||||
ExpectedNumFood int
|
||||
Err error
|
||||
}{
|
||||
{1, 1, []string{}, 1, nil},
|
||||
{1, 1, []string{"one"}, 0, nil},
|
||||
{2, 2, []string{"one"}, 3, nil},
|
||||
{2, 2, []string{"one", "two"}, 2, nil},
|
||||
{11, 1, []string{"one", "two"}, 9, nil},
|
||||
{11, 11, []string{}, 121, nil},
|
||||
{11, 11, []string{"one", "two", "three", "four", "five"}, 116, nil},
|
||||
}
|
||||
|
||||
r := FeastRuleset{}
|
||||
for testNum, test := range tests {
|
||||
state, err := r.CreateInitialBoardState(test.Width, test.Height, test.IDs)
|
||||
require.Equal(t, test.Err, err)
|
||||
if err != nil {
|
||||
require.Nil(t, state)
|
||||
continue
|
||||
}
|
||||
require.NotNil(t, state)
|
||||
require.Equal(t, test.Width, state.Width)
|
||||
require.Equal(t, test.Height, state.Height)
|
||||
require.Equal(t, len(test.IDs), len(state.Snakes))
|
||||
for i, id := range test.IDs {
|
||||
require.Equal(t, id, state.Snakes[i].ID)
|
||||
}
|
||||
require.Len(t, state.Food, test.ExpectedNumFood, testNum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeastCreateNextBoardState(t *testing.T) {
|
||||
tests := []struct {
|
||||
prevState *BoardState
|
||||
moves []SnakeMove
|
||||
expectedError error
|
||||
expectedState *BoardState
|
||||
}{
|
||||
{
|
||||
&BoardState{
|
||||
Width: 3,
|
||||
Height: 3,
|
||||
Snakes: []Snake{
|
||||
{
|
||||
ID: "one",
|
||||
Body: []Point{{0, 0}, {1, 0}, {2, 0}},
|
||||
Health: 100,
|
||||
},
|
||||
{
|
||||
ID: "two",
|
||||
Body: []Point{{2, 2}, {1, 2}, {0, 2}},
|
||||
Health: 100,
|
||||
},
|
||||
},
|
||||
Food: []Point{},
|
||||
},
|
||||
[]SnakeMove{
|
||||
{ID: "one", Move: MoveDown},
|
||||
{ID: "two", Move: MoveUp},
|
||||
},
|
||||
nil,
|
||||
&BoardState{
|
||||
Width: 3,
|
||||
Height: 3,
|
||||
Snakes: []Snake{
|
||||
{
|
||||
ID: "one",
|
||||
Body: []Point{{0, 1}, {0, 0}, {1, 0}},
|
||||
Health: 99,
|
||||
},
|
||||
{
|
||||
ID: "two",
|
||||
Body: []Point{{2, 1}, {2, 2}, {1, 2}},
|
||||
Health: 99,
|
||||
},
|
||||
},
|
||||
Food: []Point{{0, 2}, {1, 1}, {2, 0}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
r := FeastRuleset{}
|
||||
for _, test := range tests {
|
||||
nextState, err := r.CreateNextBoardState(test.prevState, test.moves)
|
||||
require.Equal(t, test.expectedError, err)
|
||||
require.Equal(t, test.expectedState, nextState)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue