DEV-1096 - add a new "pipeline" concept (#67)
* 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
This commit is contained in:
parent
86ef6ad068
commit
d378759d58
18 changed files with 723 additions and 235 deletions
61
wrapped.go
61
wrapped.go
|
|
@ -1,53 +1,36 @@
|
|||
package rules
|
||||
|
||||
var wrappedRulesetStages = []string{
|
||||
StageMovementWrapBoundaries,
|
||||
StageStarvationStandard,
|
||||
StageHazardDamageStandard,
|
||||
StageFeedSnakesStandard,
|
||||
StageSpawnFoodStandard,
|
||||
StageEliminationStandard,
|
||||
StageGameOverStandard,
|
||||
}
|
||||
|
||||
type WrappedRuleset struct {
|
||||
StandardRuleset
|
||||
}
|
||||
|
||||
func (r *WrappedRuleset) Name() string { return GameTypeWrapped }
|
||||
|
||||
func (r *WrappedRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
nextState := prevState.Clone()
|
||||
|
||||
err := r.moveSnakes(nextState, moves)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.reduceSnakeHealth(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.maybeDamageHazards(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.maybeFeedSnakes(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.maybeSpawnFood(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = r.maybeEliminateSnakes(nextState)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nextState, nil
|
||||
func (r WrappedRuleset) Execute(bs *BoardState, s Settings, sm []SnakeMove) (bool, *BoardState, error) {
|
||||
return NewPipeline(wrappedRulesetStages...).Execute(bs, s, sm)
|
||||
}
|
||||
|
||||
func (r *WrappedRuleset) moveSnakes(b *BoardState, moves []SnakeMove) error {
|
||||
_, err := r.callStageFunc(MoveSnakesWrapped, b, moves)
|
||||
return err
|
||||
func (r *WrappedRuleset) CreateNextBoardState(prevState *BoardState, moves []SnakeMove) (*BoardState, error) {
|
||||
_, nextState, err := r.Execute(prevState, r.Settings(), moves)
|
||||
|
||||
return nextState, err
|
||||
}
|
||||
|
||||
func MoveSnakesWrapped(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
|
||||
if IsInitialization(b, settings, moves) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
_, err := MoveSnakesStandard(b, settings, moves)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
@ -65,6 +48,10 @@ func MoveSnakesWrapped(b *BoardState, settings Settings, moves []SnakeMove) (boo
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func (r *WrappedRuleset) IsGameOver(b *BoardState) (bool, error) {
|
||||
return GameOverStandard(b, r.Settings(), nil)
|
||||
}
|
||||
|
||||
func wrap(value, min, max int32) int32 {
|
||||
if value < min {
|
||||
return max
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue