82 lines
3.1 KiB
Go
82 lines
3.1 KiB
Go
package strategies
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.kleiax.de/homepage/field"
|
|
)
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
// STRATEGY INTERFACE //
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
|
|
type Strategy interface {
|
|
Init(f *field.Field)
|
|
ApplyAll() ([]field.ExternalChange, error)
|
|
ApplyNext() (field.ExternalChange, error)
|
|
Name() string
|
|
SearchProgressableCells() int
|
|
}
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
// VISUALITION INTERFACE //
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
|
|
type StrategyVisualization interface {
|
|
pointOut(f *field.Field) []field.Mark
|
|
}
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
// BASE STRUCTURE //
|
|
// ────────────────────────────────────────────────────────────────────────────── //
|
|
|
|
type Base struct {
|
|
name string
|
|
field *field.Field
|
|
changes []field.ExternalChange
|
|
}
|
|
|
|
func (b *Base) Init(f *field.Field) {
|
|
b.field = f
|
|
}
|
|
|
|
func (b *Base) ApplyAll() ([]field.ExternalChange, error) {
|
|
changesCopy := make([]field.ExternalChange, len(b.changes))
|
|
copy(changesCopy, b.changes)
|
|
b.changes = b.changes[:0]
|
|
if err := b.field.AddChanges(changesCopy); err != nil {
|
|
return nil, err
|
|
}
|
|
return changesCopy, nil
|
|
}
|
|
|
|
func (b *Base) ApplyNext() (field.ExternalChange, error) {
|
|
if len(b.changes) < 1 {
|
|
return field.ExternalChange{}, nil
|
|
}
|
|
changeCopy := b.changes[0]
|
|
b.changes = b.changes[1:]
|
|
if err := b.field.AddChange(&changeCopy); err != nil {
|
|
return field.ExternalChange{}, err
|
|
}
|
|
return changeCopy, nil
|
|
}
|
|
|
|
func (b *Base) queue(change field.ExternalChange) {
|
|
for _, existing := range b.changes {
|
|
if existing.Cell == change.Cell && existing.Action == change.Action &&
|
|
existing.Value == change.Value && existing.From == change.From {
|
|
return
|
|
}
|
|
}
|
|
b.changes = append(b.changes, change)
|
|
}
|
|
|
|
func (b *Base) getName() string {
|
|
return "Unknown"
|
|
}
|
|
|
|
func (b *Base) Name() string {
|
|
return fmt.Sprintf("Diese Strategie heißt: %s", b.getName())
|
|
}
|