refactore to new project structure

This commit is contained in:
2026-07-19 11:18:16 +02:00
parent 1ff0efb557
commit 9a69d4610c
12 changed files with 108 additions and 81 deletions
+10 -7
View File
@@ -1,30 +1,33 @@
package logic
import "git.kleiax.de/homepage/libs/sudoku/board"
import (
"git.kleiax.de/homepage/board"
"git.kleiax.de/homepage/logic/strategies"
)
// ────────────────────────────────────────────────────────────────────────────── //
// SOLVER STRUCTURE //
// ────────────────────────────────────────────────────────────────────────────── //
type Solver struct {
strategies []Strategy
strategies []strategies.Strategy
conf struct {
all bool
repeat bool
}
}
func (s *Solver) Add(strategy Strategy) {
func (s *Solver) Add(strategy strategies.Strategy) {
s.strategies = append(s.strategies, strategy)
}
func (s *Solver) initStragies(field *board.Field) {
func (s *Solver) InitStragies(field *board.Field) {
for _, strategy := range s.strategies {
strategy.Init(field)
}
}
func (s *Solver) run(i int) bool {
func (s *Solver) Run(i int) bool {
//TODO: clean und Fehlerauffangen
s.strategies[i].SearchProgressableCells()
numberOfChanges := s.strategies[i].ApplyAll()
@@ -34,10 +37,10 @@ func (s *Solver) run(i int) bool {
return true
}
func (s *Solver) search() {
func (s *Solver) Search() {
}
func (s *Solver) getSolutionPath() []board.Change {
func (s *Solver) GetSolutionPath() []board.ExternalChange {
return nil
}
+15 -13
View File
@@ -1,6 +1,6 @@
package strategies
import "git.kleiax.de/homepage/libs/sudoku/board"
import "git.kleiax.de/homepage/board"
// ────────────────────────────────────────────────────────────────────────────── //
// LAST_DIGIT STRUCTURE //
@@ -14,18 +14,20 @@ func (ld *LastDigit) SearchProgressableCells() int {
ld.field.ForEachPart(func(part board.Part) {
missingNumbers := part.GetMissingNumbers()
if len(missingNumbers) == 1 {
// var emptyCell *Cell
// part.forEachCell(func(cell *Cell) {
// if cell.number == 0 {
// emptyCell = cell
// }
// })
// change := Change{
// cell: emptyCell,
// action: ActionSetNumber,
// value: missingNumbers[0],
// }
// ld.changes = append(ld.changes, change)
var emptyCell *board.Cell
part.ForEachCell(func(cell *board.Cell) {
if cell.GetNumber() == 0 {
emptyCell = cell
}
})
change := board.ExternalChange{
Cell: emptyCell,
Action: board.ActionSetNumber,
Value: missingNumbers[0],
From: emptyCell.GetNumber(),
TriggerdBy: ld.getName(),
}
ld.changes = append(ld.changes, change)
}
})
//fmt.Printf("LastDigit Changes %d", len(ld.changes))
+12 -10
View File
@@ -1,6 +1,6 @@
package strategies
import "git.kleiax.de/homepage/libs/sudoku/board"
import "git.kleiax.de/homepage/board"
// ────────────────────────────────────────────────────────────────────────────── //
// NAKED_SINGLE STRUCTURE //
@@ -13,15 +13,17 @@ type NakedSingle struct {
func (ns *NakedSingle) SearchProgressableCells() int {
ns.field.ForEachPart(func(part board.Part) {
part.ForEachCell(func(cell *board.Cell) {
// candidates := cell.notes.numbers
// if len(candidates) == 1 {
// change := Change{
// cell: cell,
// action: ActionSetNumber,
// value: candidates[0],
// }
// ns.changes = append(ns.changes, change)
// }
candidates := cell.Notes.Get()
if len(candidates) == 1 {
change := board.ExternalChange{
Cell: cell,
Action: board.ActionSetNumber,
Value: candidates[0],
From: cell.GetNumber(),
TriggerdBy: ns.getName(),
}
ns.changes = append(ns.changes, change)
}
})
})
return len(ns.changes)
+15 -17
View File
@@ -3,7 +3,7 @@ package strategies
import (
"fmt"
"git.kleiax.de/homepage/libs/sudoku/board"
"git.kleiax.de/homepage/board"
)
// ────────────────────────────────────────────────────────────────────────────── //
@@ -14,11 +14,18 @@ type Strategy interface {
Init(f *board.Field)
ApplyAll() int
ApplyNext() bool
ApplyOne(n int) bool
Name() string
SearchProgressableCells() int
}
// ────────────────────────────────────────────────────────────────────────────── //
// VISUALITION INTERFACE //
// ────────────────────────────────────────────────────────────────────────────── //
type StrategyVisualization interface {
pointOut(f *board.Field) []board.Mark
}
// ────────────────────────────────────────────────────────────────────────────── //
// BASE STRUCTURE //
// ────────────────────────────────────────────────────────────────────────────── //
@@ -26,7 +33,7 @@ type Strategy interface {
type Base struct {
name string
field *board.Field
changes []board.Change //eigener Typ muss her
changes []board.ExternalChange
}
func (b *Base) Init(f *board.Field) {
@@ -34,9 +41,9 @@ func (b *Base) Init(f *board.Field) {
}
func (b *Base) ApplyAll() int {
// for _, change := range sb.changes {
// //change.do()
// }
for _, change := range b.changes {
b.field.AddChange(&change)
}
return len(b.changes)
}
@@ -44,24 +51,15 @@ func (b *Base) ApplyNext() bool {
if len(b.changes) < 1 {
return false
}
//sb.changes[0].do()
b.field.AddChange(&b.changes[0])
b.changes = b.changes[1:]
return true
}
func (b *Base) ApplyOne(n int) bool {
if len(b.changes) <= n || n < 0 {
return false
}
//sb.changes[n].do()
b.changes = append(b.changes[:n], b.changes[n+1:]...)
return true
}
func (b *Base) getName() string {
return "Unkown"
}
func (b *Base) Name() string {
return fmt.Sprintf("Die Strategie heißt: %s", b.getName())
return fmt.Sprintf("Diese Strategie heißt: %s", b.getName())
}