* add a new "pipeline" concept - added new Pipeline type which is a series of stages - added a global registry to facilitate plugin architecture - 100% test coverage * Refactor rulesets to provide and use Pipeline * fix copypasta comments * fix lint for unused method * include game over stages in ruleset pipelines * clean up unused private standard methods * remove unused private methods in squad ruleset * remove unused private methods in royale ruleset * refactor: pipeline clone + return next board state * YAGNI: remove unused Append * refactor: improve stage names * add no-op behavior to stages for initial state * refactor: no-op decision within stage functions * remove misleading comment that isn't true * dont bother checking for init in gameover stages * remove redundant test * refactor: provide a combined ruleset/pipeline type * fix: movement no-op for GameOver check IsGameOver needs to run pipeline, move snakes needs to no-op for that * add test coverage * refactor: improve stage names and use constants * add Error method Support error checking before calling Execute() * update naming to be American style * panic when overwriting stages in global registry * rename "Error" method and improve docs * use testify lib for panic assertion * remove redundant food stage * use ruleset-specific logic for game over checks * re-work Pipeline errors * rework errors again * add defensive check for zero length snake * use old logic which checks current state, not next * add warning about how PipelineRuleset checks for game over
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package rules
|
|
|
|
var constrictorRulesetStages = []string{
|
|
StageMovementStandard,
|
|
StageStarvationStandard,
|
|
StageHazardDamageStandard,
|
|
StageFeedSnakesStandard,
|
|
StageEliminationStandard,
|
|
StageSpawnFoodNoFood,
|
|
StageModifySnakesAlwaysGrow,
|
|
StageGameOverStandard,
|
|
}
|
|
|
|
type ConstrictorRuleset struct {
|
|
StandardRuleset
|
|
}
|
|
|
|
func (r *ConstrictorRuleset) Name() string { return GameTypeConstrictor }
|
|
|
|
func (r ConstrictorRuleset) Execute(bs *BoardState, s Settings, sm []SnakeMove) (bool, *BoardState, error) {
|
|
return NewPipeline(constrictorRulesetStages...).Execute(bs, s, sm)
|
|
}
|
|
|
|
func (r *ConstrictorRuleset) ModifyInitialBoardState(initialBoardState *BoardState) (*BoardState, error) {
|
|
_, nextState, err := r.Execute(initialBoardState, r.Settings(), nil)
|
|
return nextState, err
|
|
}
|
|
|
|
func (r *ConstrictorRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
|
_, nextState, err := r.Execute(prevState, r.Settings(), moves)
|
|
|
|
return nextState, err
|
|
}
|
|
|
|
func (r *ConstrictorRuleset) IsGameOver(b *BoardState) (bool, error) {
|
|
return GameOverStandard(b, r.Settings(), nil)
|
|
}
|
|
|
|
func RemoveFoodConstrictor(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
|
// Remove all food from the board
|
|
b.Food = []Point{}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func GrowSnakesConstrictor(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
|
// Set all snakes to max health and ensure they grow next turn
|
|
for i := 0; i < len(b.Snakes); i++ {
|
|
if len(b.Snakes[i].Body) <= 0 {
|
|
return false, ErrorZeroLengthSnake
|
|
}
|
|
b.Snakes[i].Health = SnakeMaxHealth
|
|
|
|
tail := b.Snakes[i].Body[len(b.Snakes[i].Body)-1]
|
|
subTail := b.Snakes[i].Body[len(b.Snakes[i].Body)-2]
|
|
if tail != subTail {
|
|
growSnake(&b.Snakes[i])
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|