Refactor Sudoku field and solver implementation

This commit is contained in:
2026-09-16 22:52:44 +02:00
parent 8a5ae8c640
commit f7f9c16184
24 changed files with 1574 additions and 603 deletions
+23 -18
View File
@@ -12,8 +12,8 @@ import (
type Strategy interface {
Init(f *field.Field)
ApplyAll() []field.ExternalChange
ApplyNext() field.ExternalChange
ApplyAll() ([]field.ExternalChange, error)
ApplyNext() (field.ExternalChange, error)
Name() string
SearchProgressableCells() int
}
@@ -40,35 +40,40 @@ func (b *Base) Init(f *field.Field) {
b.field = f
}
func (b *Base) ApplyAll() []field.ExternalChange {
func (b *Base) ApplyAll() ([]field.ExternalChange, error) {
changesCopy := make([]field.ExternalChange, len(b.changes))
copy(changesCopy, b.changes)
for _, change := range b.changes {
b.field.AddChange(&change)
}
b.changes = b.changes[:0]
return changesCopy
if err := b.field.AddChanges(changesCopy); err != nil {
return nil, err
}
return changesCopy, nil
}
func (b *Base) ApplyNext() field.ExternalChange {
func (b *Base) ApplyNext() (field.ExternalChange, error) {
if len(b.changes) < 1 {
return field.ExternalChange{}
return field.ExternalChange{}, nil
}
changeCopy := b.changes[0]
b.field.AddChange(&b.changes[0])
b.changes = b.changes[1:]
if err := b.field.AddChange(&changeCopy); err != nil {
return field.ExternalChange{}, err
}
return changeCopy, nil
}
return changeCopy
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 "Unkown"
return "Unknown"
}
func (b *Base) Name() string {